简体   繁体   中英

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. 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. 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) ). Thanks for your help and i hope i'll be able to help you in arduino soon!

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

is valid in C99/C11 but not valid in C89.

If you use a C89 compiler you have to define i outside the for loop clauses:

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.

You appear to have a statement randomSeed(analogRead(0)); floating in between your setup() and loop() function definitions.

Move it and any other IO operations to the end of the setup() function so you read after setting up the pin directions:

   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 ); 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.

In C (previous to C99), it's not permissible to define a new variable in the first expression of a for loop. Try declaring your variable i at the top of the function instead.

#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);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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