Go Down

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

Student7

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!

dikkie

think you've missed a bit of code:

Code: [Select]

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


shouldn't that be:

Code: [Select]

}

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


dikkie

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

PaulS

Code: [Select]
if digitalRead(ledPin4, HIGH)

You need some parentheses around the condition clause:

Code: [Select]
if(digitalRead(ledPin4, HIGH))

Yot

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

westfw

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

Student7

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: [Select]

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 */
}
}

PaulS

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.

Student7

#8
Nov 19, 2009, 07:31 pm Last Edit: Nov 19, 2009, 07:50 pm by Anarchy Reason: 1
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 :(

PaulS

Code: [Select]
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.

Student7

latest code and all is working thanks a lot guys !

Code: [Select]

/*
 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);
}
}

Go Up