简体   繁体   English

Arduino编程错误

[英]Error in Arduino programming

I am receiving the following error: 我收到以下错误:

error: expected constructor, destructor, or type conversion before '(' token 错误:'('标记之前的预期构造函数,析构函数或类型转换

Here is the source code that I have written: 这是我编写的源代码:

   void setup() {
     pinMode(1,OUTPUT);
   [...]
     pinMode(13,INPUT);
   }
   int i = 1;
   bool pushed = digitalRead(13);
   bool val = 0;
   randomSeed(analogRead(0));
   void loop() {
     if (pushed == 1) {
       for (i = 1; i < 9; i++) {
         val = random(2);
         digitalWrite(i,val);
       }
     }
   }

The variables and the setup are OK; 变量和设置都可以; the error is located on the for line. 错误位于for行中。 Can anyone tell me how to fix this? 谁能告诉我该如何解决?

(edit : added the begining of the script, and sorry for the presentation (first question here) (编辑:添加了脚本的开头,对于演示文稿,我们感到抱歉(此处的第一个问题)

(edit : looks like the error is not in the "i" definition. I'm using an Arduino UNO SMD Edition, if that helps (and the arduino alpha 0022 linux version of the IDE) ) EDIT: okay guys, solved now. (编辑:看起来错误不在“ i”定义中。如果您有帮助(和IDE的arduino alpha 0022 linux版本),我正在使用Arduino UNO SMD版本。)编辑:好的,现在解决了。 It appears that my version of Arduino IDE was not completely downloaded, and that I put the randomSeed in the wrong place (it should be in the setup function.) (when i did put it in the setup function before updating, it shown an error message, saying /opt/arduino/lib/math.h was missing something (or something like that, i don't have the full message) ). 看来我的Arduino IDE版本没有完全下载,并且我将randomSeed放在错误的位置(应该在setup函数中。)(当我在更新之前将其放入setup函数时,它显示了错误消息,说/opt/arduino/lib/math.h缺少某些内容(或类似的内容,我没有完整的消息))。 Thanks for your help and i hope i'll be able to help you in arduino soon! 感谢您的帮助,我希望我能尽快在arduino为您提供帮助!

for (int i = 1; i < 9; i++)

is valid in C99/C11 but not valid in C89. 在C99 / C11中有效,但在C89中无效。

If you use a C89 compiler you have to define i outside the for loop clauses: 如果使用C89编译器,则必须在for循环子句之外定义i

int i;
for (i = 1; i < 9; i++)

Also in C89, all declarations have to follow the left brace of a block, you cannot freely mix declarations and statements. 同样在C89中,所有声明都必须遵循块的左大括号,您不能随意混合使用声明和语句。

You appear to have a statement randomSeed(analogRead(0)); 您似乎有一条语句randomSeed(analogRead(0)); floating in between your setup() and loop() function definitions. 浮动在setup()loop()函数定义之间。

Move it and any other IO operations to the end of the setup() function so you read after setting up the pin directions: 将其和任何其他IO操作移至setup()函数的末尾,以便设置引脚方向阅读:

   int i = 1;
   bool pushed; 
   bool val = 0;

   void setup() {
     pinMode(1,OUTPUT);
   [...]
     pinMode(13,INPUT);

     pushed = digitalRead(13);
     randomSeed(analogRead(0));
   }

   void loop() {
     if (pushed == 1) {
       for (i = 1; i < 9; i++) {
         val = random(2);
         digitalWrite(i,val);
       }
     }
   }

That will reading the value of pin 13 into pushed only once ( eg you are holding a button when powering it on ); 这样只会将针脚13的值读入一次(例如,在打开电源时按住按钮); depending what you want it to do you may want to move the read to the start of loop() so writes random values whenever the button is pressed. 根据您要执行的操作,您可能希望将读取移动到loop()的开头,以便在按下按钮时写入随机值。

In C (previous to C99), it's not permissible to define a new variable in the first expression of a for loop. 在C中(C99之前的版本),不允许在for循环的第一个表达式中定义新变量。 Try declaring your variable i at the top of the function instead. 请尝试在函数顶部声明变量i

#define pinMode1 1
#define pinMode2 13

bool pushed;
bool val = 0;

void setup() {
  // Declare OUTPUT pin.
  pinMode(pinMode1, OUTPUT);

  // Declare INPUT pin.
  pinMode(pinMode2, INPUT);

  // Set digitalRead().
  pushed = digitalRead(pinMode2);

  // Initializes the pseudo-random number generator.
  randomSeed(analogRead(0));
}

void loop() {
  if (pushed == 1) {
    for (int i = 1; i < 9; i++) {
      val = random(2);
      // Set i to HIGH or LOW.
      digitalWrite(i, val);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM