-2

I am in the early stages of writing my program, but I have run into a problem. I am fairly new to Arduino so I'm not sure if there is something I am trying to do that doesn't work, but I am getting an error message

expected ; before ) token

on the line for(count <= 10).

I would really appreciate if someone could let me know why I am getting this error.

#include<EEPROM.h>
const int LED = 12;
const int SWITCH = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(LED, OUTPUT);           //LED is always outputting information
  pinMode(LED_BUILTIN, OUTPUT);   //Built in LED is always outputting information
  pinMode(SWITCH, INPUT_PULLUP);  //Switch inputs value when in/out of ground
}

void loop() {
  // put your main code here, to run repeatedly:
  int addr = 0;     //Declaring variables
  int count = 0;
  int seconds;

  if (digitalRead(SWITCH) == LOW) {
Serial.println("----Recording----");

    for (count <= 10) {
      while (digitalRead(SWITCH) == LOW) {
        count = count + 1;
        digitalWrite(LED, LOW);
        delay(50);
      }
      else {
        count = count + 1;
        digitalWrite(LED, HIGH);
        delay(50);
      }
    } else {
      digitalWrite(LED_BUILTIN, HIGH);        //Internal LED blinks
      delay (300);
      digitalWrite(LED_BUILTIN, LOW);
      delay(300);
    }
  }
}
5
  • 1
    try while (count <= 10) { or for ( ; count<=0; ) { - find some C tutorial to learn about correct syntax Nov 23 '18 at 2:33
  • The problem is with the syntax of your for loop. Nov 23 '18 at 2:36
  • I switched to a while loop but then I just get an error at the else statement...any other help you could provide?
    – Thomas
    Nov 23 '18 at 2:43
  • 1
    for and while blocks do not have an else. Only if blocks do. Look at some basic C examples.
    – Mark Smith
    Nov 23 '18 at 5:32
  • @Thomas, you really have to refer to the arduino language refference pages before you post a question ..... arduino.cc/reference/en/language/structure/control-structure/…
    – jsotola
    Nov 23 '18 at 6:22
2

Welcome to arduino.stackexchange.
Compiler informs you about the problems with your code. In your case there is a problem with for loop.
Remember, the for loop is one of basic concepts in c/c++ (and many others) programming language.
Syntax is:

for ( init-expression ; cond-expression ; loop-expression )   
    statement;  

Description of above:

init-expression:
Before any other element of the for statement, init-expression is executed only once. Control then passes to cond-expression.

cond-expression:
Before execution of each iteration of statement, including the first iteration. statement is executed only if cond-expression evaluates to true (nonzero).

loop-expression:
At the end of each iteration of statement. After loop-expression is executed, cond-expression is evaluated.

For example:

for(int count=0; count<10; count++){
    if (count%2) {
        digitalWrite(LED, LOW);
        }
    else{
        digitalWrite(LED, HIGH);
        }  
    }

Add some extra code when needed.

To see more description of for loop see here.

0

Also, only 'if' statements have 'else' clauses; loop statements do not. If you didn't get an error for the 'else' at first, it is because the compiler got lost in the for-loop with only one argument.

'while()' statements have a boolean expression inside the parentheses. Each time the boolean evaluates TRUE, the statement or block following the loop executes once, until the loop evaluates FALSE. Regardless of how many times it executes, even zero, the following code will be executed.

'for()' statements have three expressions. Any of the three may be absent but the places for all three must be shown.

  • The first expression is executed once at the beginning of the loop. It typically initializes a loop variable, but may do anything, or be absent.
  • The second expression is a boolean expression that works exactly like the only argument in a 'while()' loop (the loop is only executed if, and for as long as, the expression is TRUE).
  • The third expression is executed after all of the statements in the loop. It typically advances the loop variable, but may do anything.
  • Like the 'while()' loop, whatever follows the 'for()' loop will be executed regardless of whether the loop did anything.

Though any of the 'for()' loop's expressions may be left out, both semi-colons must still be present to make it clear which expressions were left out.

Note that 'for(;;)' is an idiom in C that means "forever" - i.e., an infinite loop. Some other construct needs to be executed within the loop to cause it to end, if it not meant to execute perpetually.

Not the answer you're looking for? Browse other questions tagged or ask your own question.