Arduino 中使用的语法是完全 C++ 还是偶尔有一些特殊的语法

问题描述

// the loop function runs over and over again forever
void loop() {
  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

我是 Arduino 新手,有非常基本的 C 经验,但也开始学习 C++,因为我听说 Arduino 基于 C++。 我现在的关键问题是上面的代码如何在没有“while”或“for”关键字的情况下实现循环(永远运行代码)?或者是否有一个名为“loop”的特殊函数可以发挥魔力?

解决方法

因为在主函数中有


int main(void)
{
   /*  .... */
   setup();
   /*.......*/
   for(;;) loop();

主要对您隐藏。没有魔法,setuploop 是从“main”调用的普通函数。一切都很简单。

Arduino 只是 C++ 和 g++

arduino 中的 main 函数被声明为 weak,您可以编写自己的 main 函数来替换原来的函数,如本例所示:

enter image description here

第一个草图用我的草图替换了 Arduino 主程序(它什么都不做),它在代码大小中清晰可见。