[Arduino Experiment Notes] Robotic Arm (2) Switching to Control LED Lights


foreword

This chapter introduces how to control the LED light through buttons . In the previous chapter, we learned how to control the IO output level. In this chapter, we will learn how to read the level of the IO input.


hardware introduction

Looking at the independent buttons , there are four pins in total , two of which are short in distance , and the other two are long in distance .The two sets of pins with long spacing are connected together , while the initial state between the pins with short spacing is disconnected, when the button is pressed , the four pins are connected , which can be regarded as a wire .
insert image description here

Under normal circumstances, independent buttons only need to connect two pins to use. One of the pins is grounded , and the other pin is controlled by the IO port of the microcontroller .

For a detailed introduction of independent buttons , please refer to another blog "51 MCU Switch"


Physical wiring diagram

wiring diagram


Software Implementation

In order to read the input of the pin , we need to use a serial port tool . Fortunately, the serial port display window is integrated in the Arduino IDE , and we can easily print the pin status with a few lines of code .


Introduction to library functions

First, a few functions in the core library are introduced . Move to the official function manual for details .

  • digital pin read
    uint8_t digitalRead(uint8_t pin)
    • pin : pin number
    • Return value : pin level high or low
      • Low level: LOW
      • High level: HIGH

The serial port library ( Serial) belongs to the built-in core library and does not need to be included with #includepre-compiled instructions . The functions used in this section are as follows.

  • Serial port initialization
    Serial.begin(baud)
    • baud : The baud rate must be consistent with that in the serial port window .
  • serial port printing
    Serial.println(chara)
    • chara : Any type of data can be received .
    • Start a new line after printing .

code

// 2022.11.18
// Author:Qiu

#define KEY_PORT 2   // 2号引脚

void setup() {
    
    
  // 设置串口波特率
  Serial.begin(9600);
  // 将2号引脚设置为输入上拉
  pinMode(KEY_PORT, INPUT_PULLUP);
  // 将13号引脚设置为输出; 
  // #define LED_BUILTIN 13
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    
    
  // 读取2号引脚的状态
  boolean buttonState = digitalRead(KEY_PORT);
  // 在串口窗口打印引脚状态
  Serial.println(buttonState);
  // 判断控制灯:0-按下;1-没按
  if(buttonState){
    
    
    // 熄灭,写入低电平
    digitalWrite(LED_BUILTIN, LOW);
  }else{
    
    
    // 点亮,写入高电平
    digitalWrite(LED_BUILTIN, HIGH);
  }
  // 读取存在间隔,保证稳定
  delay(1);
}

Goal of the next article

We hope to have more advanced control of LED lights, for example, dynamically adjust its brightness instead of simply turning it on and off . See you next time!


Summarize

Through this chapter, you have learned how to read the high and low levels of the digital IO port , and completed the experiment of controlling the LED with the help of serial port related functions .This has laid a solid foundation for us to control the robotic arm in the future !

Guess you like

Origin blog.csdn.net/m0_46500149/article/details/132630314