Go Down

Topic: error: expected unqualified-id before 'if' (Read 1 time) previous topic - next topic

EagerGeek

Hi, I'm new to Arduino, and I get, error: expected unqualified-id before 'if' when I try to compile the following code:

Quote

/*Program to make a robot follow a light at varying speeds based on the brightness of the light.
Uses a microcontroller, battery, two photoresistors and two servos.*/
int sensorpina = 14;                //left
int sensorpinb = 15;                //right
int servopina = 0;                  //left
int servopinb = 1;                  //right

int val = 0;                        //left
int bval = 0;                       //right

void setup()
{
 pinMode(servopina, OUTPUT);       //left
 pinMode(servopinb, OUTPUT);       //right
}

void loop()
{
 val = analogRead(sensorpina);     //left
 Serial.println(val);              //left
 val = analogRead(sensorpinb);     //right
 Serial.println(bval);             //right
}

void loop()
{
if (sensorpina < sensorpinb);       //meaning the light is to the right of the robot, this is the error line
{                                   //turn right
 digitalWrite(servopina, HIGH);    //left on
 digitalWrite(servopinb, LOW);     //right off
}
else if (sensorpina > sensorpinb);  //meaning the light is to the left of the robot
{                                   //turn right
 digitalWrite(servopina, LOW);     //left off
 digitalWrite(servopinb, HIGH);    //right on
}
else if (sensorpina == sensorpinb)  //meaning the light is ahead of the robot
{                                   //go straight
 analogWrite(servopina, val/4);    //left
 analogWrite(servopinb, bval/4);   //right
}
}



I know I made several obvious mistakes, so don't tell me what I already know about how bad it is, just tell me how to fix it and really anything to make it better.

Hans K

I suggest you remove the semi-colon after the if's. It should be
if (something) { do something; }

Professor Chaos

You also have declared function loop() twice!

AWOL

#3
Sep 17, 2010, 10:08 pm Last Edit: Sep 17, 2010, 10:08 pm by AWOL Reason: 1
Are they really servos, as in R/C servos?
They don't normally like simple digital writes, or, for that matter, analogue ones.
Have a look at the servo library
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

EagerGeek

Where's the servo library? (where can I find it)

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

EagerGeek

Thanks, I'll try that. [smiley=dankk2.gif]

Go Up