Go Down

Topic: Expected initializer before void in function...... (Read 6913 times) previous topic - next topic

D.E Arduino

When I try to compile the following code in Arduino 0015,I get the error "Expected initializer before void in function void loop()".How can I get the code to work?
Code: [Select]
byte myvar
void setup()
{
 myvar = 10
 Serial.begin (9600);
}
void loop()
{
  if (10 == HIGH)
  {
    Serial.println (myvar);
   }
  }

mem

#1
May 24, 2009, 09:06 am Last Edit: May 24, 2009, 09:06 am by mem Reason: 1
You need to have a semicolon at the end of each expression.
Code: [Select]
byte myvar;  // semicolon needed here
void setup()
{
 myvar = 10;   // and here
   Serial.begin (9600);
}
void loop()
{
 if (10 == HIGH)  
 {
   Serial.println (myvar);
 }
}

This code will compile but wont print anything because
 if (10 == HIGH)  
will never be true, what did you want to do here?

D.E Arduino

Thanks.By 10 I meant for it to be pin 10,oops forgot to define the input.

mem

#3
May 24, 2009, 10:59 am Last Edit: May 24, 2009, 10:59 am by mem Reason: 1
Arduino digital pins are inputs by default, although it does no harm to explicitly declare inputs in setup.

Your loop code could look like this:

void loop()
{
 if (digitalRead(10) == HIGH)
 {
   Serial.println (myvar);
   delay(100); // add a delay to reduce the number of times this prints.
 }
}


Go Up