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

Here is my code :

int ledPin1 =  6;    // LED connected to digital pin 6
int ledPin2 =  5;    // LED connected to digital pin 5
int ledPin3 =  4;    // LED connected to digital pin 4
int ledPin4 =  2;    // Switch connected to pin 2
/* The setup() method runs once, when the sketch starts */

void setup()   {              
  /* initialize the digital pin as an output: */
  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  /* initialize the digital pin as an input: */
  pinMode(ledPin4, INPUT);  
}
/* the loop() method runs over and over again, */
/* as long as the Arduino has power */
if digitalRead(ledPin4, HIGH)
{
  digitalWrite(ledPin1, HIGH);     /* set the LED on */
  delay(2000);                     /* wait for 2 seconds */
  digitalWrite(ledPin2, HIGH);     /* set the LED on */
  delay(500);                      /* wait for 1/2 second */
  digitalWrite(ledPin1, LOW);      /* set the LED off */
                  
  digitalWrite(ledPin2, LOW);      /* set the LED off */
                  
  digitalWrite(ledPin3, HIGH);     /* set the LED on */
  delay(2000);                     /* wait for 2 seconds */
  digitalWrite(ledPin3, LOW);      /* set the LED off */

  digitalWrite(ledPin2, HIGH);     /* set the LED on */
  delay(500);                      /* wait for 1/2 seconds */
  digitalWrite(ledPin2, LOW);      /* set the LED off */
}
else
  digitalWrite(ledPin2, HIGH);     /* set the LED on */
  delay(500);                      /* wait for 1/2 seconds */
  digitalWrite(ledPin2, LOW);      /* set the LED off */
  }

I am getting error :

error: expected unqualified-id before 'if'

I would be glad if smo can help!
Logged

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

think you've missed a bit of code:

Code:
}
/* the loop() method runs over and over again, */
/* as long as the Arduino has power */
if digitalRead(ledPin4, HIGH)

shouldn't that be:

Code:
}

/* the loop() method runs over and over again, */
/* as long as the Arduino has power */
void loop()
{
  if digitalRead(ledPin4, HIGH)
Logged

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

and you will need a second closing curly brace at the end of the loop function
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if digitalRead(ledPin4, HIGH)

You need some parentheses around the condition clause:

Code:
if(digitalRead(ledPin4, HIGH))
Logged

Global Moderator
The Netherlands
Offline Offline
Sr. Member
*****
Karma: 1
Posts: 287
don't panic...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And another opening curly after else.
Quote
else {
 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 seconds */
 digitalWrite(ledPin2, LOW);      /* set the LED off */
 }

Try to get used to be very precise with {, }, (, ) and ;'s (more easy said than done, i know)

Have fun,

Jeroen
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6753
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and while it's not the error you're seeing to start with,
Code:
if(digitalRead(ledPin4, HIGH))
will need to be
Code:
if(digitalRead(ledPin4) == HIGH)
Logged

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

Hey guys thanks a lot , I appreciate everything !!! now it is much better the yellow light is blinking now ledpin2 !!! this is supposed to be a traffic light when the switch is on only yellow light when the switch is off , all the lights function as a traffic light , but unfortunately this I failed :S maybe I connected it wrong with the switch , so I am gonna check the cables and here is my latest code according to your modifications , thanks a lot really !

Code:
int ledPin1 =  6;    // LED connected to digital pin 6
int ledPin2 =  5;    // LED connected to digital pin 5
int ledPin3 =  4;    // LED connected to digital pin 4
int ledPin4 =  2;    // Switch connected to pin 2
/* The setup() method runs once, when the sketch starts */

void setup()   {              
 /* initialize the digital pin as an output: */
 pinMode(ledPin1, OUTPUT);  
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 /* initialize the digital pin as an input: */
 pinMode(ledPin4, INPUT);  
}
/* the loop() method runs over and over again, */
/* as long as the Arduino has power */
void loop()
{
if (digitalRead(ledPin4) == HIGH)
{
 digitalWrite(ledPin1, HIGH);     /* set the LED on */
 delay(2000);                     /* wait for 2 seconds */
 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 second */
 digitalWrite(ledPin1, LOW);      /* set the LED off */
                
 digitalWrite(ledPin2, LOW);      /* set the LED off */
                
 digitalWrite(ledPin3, HIGH);     /* set the LED on */
 delay(2000);                     /* wait for 2 seconds */
 digitalWrite(ledPin3, LOW);      /* set the LED off */

 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 seconds */
 digitalWrite(ledPin2, LOW);      /* set the LED off */
}
else {
 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 seconds */
 digitalWrite(ledPin2, LOW);      /* set the LED off */
 }
 }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Failed how?

Does the switch not affect the behavior of the lights at all?

You realize, I hope, that you are only reading the switch value every 5 seconds, so you'd need to hold the button down that long in order to be sure that the reading took place while the button was being held down.

That's not how a normal traffic control switch operates.
Logged

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

Hi guys latest update , I found out that the resistor was missing and I forgot delay in the last lines for yellow light , I completed the task all is working like a charm now and I am on the next task , thanks a lot for your valuable time I know I need a lot more to improve my programming smiley-sad
« Last Edit: November 19, 2009, 01:50:46 pm by Anarchy » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
else (digitalRead(ledPin4) == LOW);

In the if clause, you read the button state, and do something if the button is pressed. It either is, or it isn't.

The else is followed by a code block. The code block reads the state of the button again, and compares it to LOW. The result may, or may not, be true. It doesn't really matter, since you do nothing with the information.

The statement that follows the else is not in {}, so this ends the if/else statement.

Then, you have some code inside curly braces. You turn the LED on and wait. Then, you turn it off, and right back on. No wonder it does not blink.

Remove the string following the else statement, and add delays after turning the LED off, and the code should then work correctly.
Logged

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

latest code and all is working thanks a lot guys !

Code:
/*
  Blink
 
 Turns on an LED on for one second, then off for one second, repeatedly.
 
 The circuit:
 * LED connected from digital pin 13 to ground.
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 Created 1 June 2005
 By David Cuartielles
 
 http://arduino.cc/en/Tutorial/Blink
 
 based on an orginal by H. Barragan for the Wiring i/o board
 
 */

int ledPin1 =  6;    // LED connected to digital pin 6
int ledPin2 =  5;    // LED connected to digital pin 5
int ledPin3 =  4;    // LED connected to digital pin 4
int ledPin4 =  2;    // Switch connected to pin 2
/* The setup() method runs once, when the sketch starts */

void setup()   {              
 /* initialize the digital pin as an output: */
 pinMode(ledPin1, OUTPUT);  
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 /* initialize the digital pin as an input: */
 pinMode(ledPin4, INPUT);  
}
/* the loop() method runs over and over again, */
/* as long as the Arduino has power */
void loop()
{
if (digitalRead(ledPin4) == HIGH)
{
 digitalWrite(ledPin1, HIGH);     /* set the LED on */
 delay(2000);                     /* wait for 2 seconds */
 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 second */
 digitalWrite(ledPin1, LOW);      /* set the LED off */
                
 digitalWrite(ledPin2, LOW);      /* set the LED off */
                
 digitalWrite(ledPin3, HIGH);     /* set the LED on */
 delay(2000);                     /* wait for 2 seconds */
 digitalWrite(ledPin3, LOW);      /* set the LED off */

 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 seconds */
 digitalWrite(ledPin2, LOW);      /* set the LED off */
}
else
{
 digitalWrite(ledPin2, HIGH);     /* set the LED on */
 delay(500);                      /* wait for 1/2 seconds */
 digitalWrite(ledPin2, LOW);      /* set the LED off */
  delay(500);
 }
 }
Logged

Pages: [1]   Go Up
Jump to: