Pages: [1]   Go Down
Author Topic: error: expected unqualified-id before 'if'  (Read 869 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You also have declared function loop() twice!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25718
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 17, 2010, 03:08:52 pm by AWOL » Logged

"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.

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25718
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://tinyurl.com/33hjn4p
Logged

"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.

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

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

Pages: [1]   Go Up
Jump to: