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-RArduino Uno R3
VCC3.3V
GNDGND
信号引脚数字信号引脚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表示关)。

整体运行界面:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值