Arduino开发之Blink LED
环境搭建:
1. Arduino UNO R3开发板,
2. Arduino IDE。
目前最新版是1.8.3。可以在https://www.arduino.cc/en/Main/Software下载并安装。
安装好之后,桌面会有如下图标。
示例开发:
1.连接设备。
本例中我们以DFR0021-R LED为例,基于Arduino Uno R3和Arduino IDE开发。
DFR0021-R的引脚和Arduino Uno开发板的连接方式如下,
DFR0021-R | Arduino Uno R3 |
VCC | 3.3V |
GND | GND |
信号引脚 | 数字信号引脚6 |
2. 编码。
连接好之后,用数据线连接Arduino开发板和电脑。同时打开Arduino IDE。输入下述代码。
const int ledPin = 6;// the number of the LED pin
// Variables will change :
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
Serial.println(ledState);
digitalWrite(ledPin, ledState);
}
}
然后保存文件。
选择Arduino Uno开发板。
编译上传大到开发板。
3.运行。
选择COM口信息,
然后选择端口监视工具,查看程序运行信息。
串口监视信息,
上面的数据就是Red LED 返回当前状态(1表示开,0表示关)。
整体运行界面: