/* Blink Turns an LED on for one second, then off for one second, repeatedly. */
// define variables here
// variables should be defined before setup()
// You must includevoid setup()
andvoid loop()
in every Arduino sketch, or the program won't compile! // the setup function runs once when you press reset or power the board
// setup()函数只运行一次,用来启动Arduino控制器,将运行中不改变的数值和属性固化到芯片中 void setup() { // initialize digital pin LED_BUILTIN as an output.
// pinMode(pin,mode):将指定的引脚配置为输入或输出
// - pin:所需要设置的引脚号
// - mode:INPUT/OUTPUT(pinMode也可以是INPUT_PULLUP,使用引脚内置的上拉电阻) pinMode(LED_BUILTIN, OUTPUT); }
// loop()函数循环执行,直到按下reset键或者移除电源 void loop() {
// digitalWrite(pin,HIGH/LOW):数字引脚输出,HIGH表示高电平(5v),LOW表示低电平(0v)
// delay(num):暂停执行程序num毫秒
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
总结:
1、Arduino程序的基本结构:
// define variables before setup() void setup(){ //将运行中不变的数值和属性固化到芯片中 }
void loop(){ //需要循环执行的代码 }
- setup():setup()函数只运行一次,用来启动Arduino控制器,将运行中不改变的数值和属性固化到芯片中。The setup function runs once when you press reset or power the board.
- loop() : loop()函数循环执行,直到按下reset键或者移除电源。
2、Blink.ino中用到的几个函数:
① pinMode(pin,mode) : 将指定的引脚配置为输入或输出
- pin : 所需要设置的引脚号
- mode : INPUT/OUTPUT(pinMode也可以是INPUT_PULLUP,使用引脚内置的上拉电阻)
pinMode(LED_BUILTIN, OUTPUT);
② digitalWrite(pin,HIGH/LOW) : 数字引脚输出,HIGH表示高电平(5v),LOW表示低电平(0v)
digitalWrite(LED_BUILTIN, HIGH);
③ delay(num) : 暂停执行程序num毫秒
delay(1000);