expected unqualified-id before 'for'

Hello!

I got the “expected unqualified-id before ‘for’” fault when I added some code tha(t I copied right out of a book:

for (int i = 0; i < 5; i++) {

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(100);

}

It highlights the first line (for…). The book and other tutorials don’t address this fault.

please help. Thank you,

Paul

What is the rest of the code in our sketch? Because the error message clearly states that whatever the problem is, … it precedes “for”.

If that is all the code that you entered in your sketch, then I think the compiler complains that you do not have the required Setup and Loop function section to envelop your inserted code. The entire for-loop on it’s own is insufficient. See the Code section in the following link:

http://arduino.cc/en/Tutorial/BareMinimum

need to see the entire file. Something related to the context of the code.

Thank you, Valen and stevech,

Here’s the complete code:

void setup() {

pinMode(13, OUTPUT);

pinMode(12, OUTPUT);

}

for (int i = 0; i < 5; i++) {

digitalWrite(13, HIGH );

delay(100);

digitalWrite(13, LOW);

delay(100);

}

That’s it so far. Below this I will add:

void loop() {

digitalWrite(12, HIGH);

delay(100);

digitalWrite(12, LOW);

delay(100);

My intention is to run the “for” loop [changing the value 5 to 1000 so that it repeats 1000 times] and change the delay to microseconds [delayMicroseconds(5, HIGH);], etc. The void loop for pin 12 will have delay values around 20. I just used the other values so I could “see” if things were working.

Another question: If I want to run delays quicker than 3 microseconds, the Leonardo won’t work well. Will the Due with its higher speed go to 1 microsecond or faster?

That’s about all I need for this project, except to play with the values to get the desired results.

I’m studying the “Beginning Arduino Programming” ebook by Brian Evans. It seems like a pretty good start. Would you suggest anything different to get grounded in the basic protocols? I’m still pretty fuzzy on the initial setup requirements.

Thanks again,

Paul

Your problem is that you have code that is not inside a function.

Your posted this as your code to date:

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

for (int i = 0; i < 5; i++) {
digitalWrite(13, HIGH );
delay(100);
digitalWrite(13, LOW);
delay(100);
}

So as a quick tutorial, you MUST define two functions in the Arduino ‘system’. The first is setup and the second is loop. Without these two functions, the compiler is not going to be happy. Setup is called only one time each time the processor is powered up or reset. ‘loop’ is called as soon as the last call to loop completes. ‘loop’ is essensialy inside an infinite loop like this:

for(;;){
  loop();
}

Now the code you have after the definition of setup (the loop of 5), is code that is not inside a function. C, unlike a language like Python, requires that all code exist inside a function. So if you only want to execute your loop 5 times ever (for each reset), then you might like something like this:

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);

  for (int i = 0; i < 5; i++) {
    digitalWrite(13, HIGH );
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }
}

void loop(void) {
  delay(1000);  // Do nothing over and over again
}

If you want to continue to send pulses you could do something like this:

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop(void){

  // Sent out five pulses on pin 13
  for (int i = 0; i < 5; i++) {
    digitalWrite(13, HIGH );
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }
  delay(1000); // Wait a second before doing it all again.
}

As for the speed, using digitalWrite is “very” slow. It has lots of work to do to figure out what port to use based on the pin number and the mask to use and other overheads. If you want to generate pulses as fast as possible, look into direct port manipulation. Google will be your friend.

Thank you guys for the help. I think I got this now. I am new at it and learning. Still studying.

Thanks again,

Paul