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 */
}
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 !
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 */
}
}
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.
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 :(
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.
latest code and all is working thanks a lot guys !
/*
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);
}
}