Pages: [1]   Go Down
Author Topic: Expected initializer before void in function......  (Read 6027 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
byte myvar
void setup()
{
  myvar = 10
  Serial.begin (9600);
 }
void loop()
 {
   if (10 == HIGH)
   {
     Serial.println (myvar);
    }
   }
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to have a semicolon at the end of each expression.
Code:
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?
« Last Edit: May 24, 2009, 02:06:36 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
  }
}
« Last Edit: May 24, 2009, 03:59:44 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok,thanks
Logged

Pages: [1]   Go Up
Jump to: