LED ③ classic Arduino Study Notes

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1 Introduction

    Today we start at the same time to explain the theory and practice. For the vast majority of people have been microcontroller programming experience, most contact is the first LED lamp (suddenly remembered that era ignorant blogger freshman, junior brothers then showed us the skill of a light water I was really dazzle the one, mind very excited).

2. Digital IO Introduction

    Lesson mainly used in digital IO port, so before starting the experiment, we look first to understand Arduino digital IO ports.
    Digital signals are discrete signals represented 0,1, it is represented in the form of a binary signal. Arduino in the digital signal represented by high and low level, a high level to a digital signal, a low level digital signal is 0. Arduino on each pin with the pin number is numbered, including writing an analog input pin "A" number. These pins can be used to complete the function of input / output digital signal.
    Prior to use input or output functions need to () function mode configuration pins as input or output mode pinMode:

2.1 pinMode(pin,mode)

Method: the pinMode (pin, mode)
Parameters: Pin pin configured for the specified number, the parameter configuration mode for the specified mode.

image

    And we need to use the main next output LED mode, so we need to be configured to pinMode (pin, OUTPUT); later configured as an output, also you need to use digitalWrite () function so that the output pin is high or low.

2.2 digitalWrite(pin,value)

Method: digitalWrite (pin, value)
parameters: PIN number for the specified output pin. Parameter value is to be specified output level, using the specified output high HIGH, LOW using the specified output low.

  • Low output to 0V Arduino
  • High output for the current operating voltage the Arduino.

    In addition to the digital output signal pin, but also () function to read a digital signal with an externally input digitalRead.

2.3 digitalRead(pin)

Method: digitalRead (pin)
Parameters: pin reading the pin number for the specified state.
note:

  • When Arduino to 5v power supply, the input voltage range will -0.5v-1.5v identified as the low voltage, the input voltage range as high 3-5.5v recognition.
  • Excessive input voltage can damage the Arduino. In Arduino core library, OUTPUT is defined as 1, INPUT is defined as 0, HIGH is defined as 1, LOW 0 is defined.

3. The time control function

    Since many of our next test procedures are used in delay function, then here are a few:

  • Delay () ----- millisecond delay
  • delayMicroseconds () microsecond delay -----

4. The LED lighting

4.1 experimental equipment

  • LED module (here, the LED module belonging to common anode)

image

  • Mega2560 board

4.2 experimental content

4.2.1 single LED lights off

Experiment code :

/**
* @Desc 点亮熄灭单个LED
* @author 单片机菜鸟
* @Date  2016/12/2
* 注意:LED的一端接到5V,另外一端接到输出引脚,低电平亮
*/
 
#define LED 2  //定义2号引脚为LED
 
void setup() {
  // 将2号引脚设置为输出状态
  pinMode(LED,OUTPUT);
}
 
void loop() {
  digitalWrite(LED,LOW);//点亮LED
  delay(1000);//延时1s
  digitalWrite(LED,HIGH);//关闭LED
  delay(1000);//延时1s
}

Writing to mega2560 board, LED pin is connected to port 2, you will see the normal LED flashes.

4.2.2 Classical water lights

Experiment code :

/**
* @Desc 流水灯(8个LED依次亮灭)
* @author 单片机菜鸟
* @Date  2016/12/2
* 注意:LED的一端接到5V,另外一端接到输出引脚,低电平亮
*/
 
#define LED_START 2
#define LED_END  9
#define DELAY_TIME 500 //延时时间,可以调节然后就可以看到亮灭频率
 
void setup() {
  // 将2-9号引脚设置为输出状态
  for(int i=LED_START;i<=LED_END;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i,HIGH);//灭掉LED
  }
}
 
void loop() {
 
  //从引脚2到9引脚,逐个点亮LED,等待1S再熄灭LED
  for(int i=LED_START;i<=LED_END;i++){
    digitalWrite(i,LOW);//LED亮
    delay(DELAY_TIME);
    digitalWrite(i,HIGH);//LED灭
  }
 
  //从引脚9到2引脚,逐个点亮LED,等待1S再熄灭LED
  for(int i=LED_END;i>=LED_START;i--){
    digitalWrite(i,LOW);//LED亮
    delay(DELAY_TIME);
    digitalWrite(i,HIGH);//LED灭
  }
  
}

Not surprisingly, you should see the LED light off the water.

5. Summary

In this section we mainly learned:

  • Digital IO
  • A time delay function
  • Classic water lights

Guess you like

Origin blog.csdn.net/dpjcn1990/article/details/92831364