arduino 编程基础

0. setup() 与 loop() 函数

  • void setup():
    • // put your setup code here, to run once:
    • 仅执行一次;
  • void loop():
    • // put your main code here, to run repeatedly
    • 会重复执行多次;

1. Blink demo

  • setup:

    pinMode(LED_BUILTIN, OUTPUT);
            // pinMode 设置引脚模式
            // LED_BUILTIN:arduino 开发板上自带的 LED,将其指定为输出;
  • loop:

    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(3000);                       // 暂停三秒,也即 LED_BUILTIN 高电平持续三秒,也即亮三秒;
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);

2. 常用函数

  • millis() ⇒ 获取当前时间

    unsigned long currentMillis = millis();