Home
avatar

静静

01xArduino程序基础

使用C++编程,基本参考C++语法。

每一句结尾用分号,注释用//,全大写单词是特有字符,不要乱用。函数用{}套起来。

void setup() {
  // put your setup code here, to run once:
    // 这里的代码在开始的时候运行一次
    code doing something;  // 每一行代码用分号结尾 

}

void loop() {
  // put your main code here, to run repeatedly:
    // 这里的代码会循环重复运行

}

C++在线学习资料

https://www.bilibili.com/video/BV1et411b73Z

Arduino语句函数库参考

http://www.taichi-maker.com/homepage/reference-index/arduino-code-reference/

https://docs.arduino.cc/language-reference/

点亮LED小灯

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.
  打开LED一秒钟,然后关闭一秒钟,重复。
  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  大多数arduino都有一个可以控制的板载LED。
  在UNO, MEGA和ZERO上,它连接到数字引脚13,在MKR1000引脚6上。
  领导_BUILTIN设置为正确的LED引脚,与使用哪个板无关。
  如果您想知道板上LED连接到Arduino模型上的引脚,请查看板的技术规格:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
// 按复位键或单板上电时,设置功能运行一次
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  // 初始化数字引脚LED_BUILTIN作为输出。
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
// 循环函数会一直运行下去
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level) 打开LED (HIGH是电压等级)
  delay(1000);                      // wait for a second 等一秒  一秒是一千毫秒
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW 通过使电压低来关闭LED
  delay(1000);                      // wait for a second
}

编译好后点击上传

试着修改变量

int delaytime = 3000;
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  // 初始化数字引脚LED_BUILTIN作为输出。
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
// 循环函数会一直运行下去
void loop() {
  delaytime = delaytime +1000 // 随着循环递加
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level) 打开LED (HIGH是电压等级)
  delay(3000);                      // wait for 3 second 等一秒  一秒是一千毫秒
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW 通过使电压低来关闭LED
  delay(delaytime);                      // wait for delaytime second
}

pinMode函数和digitalWrite函数

所以原代码也可写为

int delaytime = 100;
int LED_pin = 13; 
// On the UNO, MEGA and ZERO it is attached to digital pin 13
// 连接到数字引脚13
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  // 初始化数字引脚LED_BUILTIN作为输出。
  pinMode(LED_pin, OUTPUT);
}

// the loop function runs over and over again forever
// 循环函数会一直运行下去
void loop() {// 随着循环递加
  digitalWrite(LED_pin, 5);  // turn the LED on (HIGH is the voltage level) 打开LED (HIGH是电压等级)
  delay(delaytime);                      // wait for 3 second 等一秒  一秒是一千毫秒
  digitalWrite(LED_pin, 0);   // turn the LED off by making the voltage LOW 通过使电压低来关闭LED
  delay(delaytime);                      // wait for delaytime second
}
Arduino