Project 1 LED Blink
Project 1 LED Blink
Description:
we start from simple projects. For this lesson we will perform “Arduion blinks
LED”, which is the basic practice for starter. We provide a test code to control
LED to perform blinking effect. In the code, you could set distinct flashing
scene by changing the time of lighting on and off. Power on GND and VCC, the
LED will light on when signal end S is high level, on the contrary, LED will turn
off when signal end S is low level.
Specifications:
材料:
接线:
GND -- GND
SIG -- D13
code:
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the
voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the
voltage LOW
delay(1000); // wait for a second
}
Test Result:
Upload test code successfully, white LED starts blinking, lights on for 1000ms,
lights off for 1000ms, alternately.
Code Explanation
The code looks long and clutter, but most of which are comment. The
grammar of Arduino is based on C.
Comments generally have two forms of expression:
// the setup function runs once when you press reset or power the
board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
According to comments, we will find that author define the D13 pin mode as
digital output in setup() function. Setup() is the basic function of Arduino. It
will execute once in the running of program, usually as definition pin, define
and ensure the variables.
Loop() is the necessary function of Arduino, it can run and loop all the time
after “setup()” executes once
In the loop()function, author uses
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(): set the output voltage of pin to high or low level. We make D13
output high level, then the LED lights on.
delay(1000); // wait for a second
Delay function is used for delaying time, 1000ms is 1s, unit is ms
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
Similarly, we make D13 output low level, LED will turn off.
delay(1000); // wait for a second
Delay for 1s, light on LED--keep on 1s--light off LED--stay on 1s, iterate the
process. LED flashes with 1-second interval. What if you want to make LED
flash rapidly? You only need to modify the value of delay block. Reducing the
delay value implies that the time you wait is shorter, that is, flashing rapidly.
Conversely, you could make LED flash slowly.