• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeArduinoIntroductory Kit with Clear Case for ArduinoExperiment 9 Traffic Lights
Previous Next

Experiment 9 Traffic Lights

Posted by: Alvin Jin , February 17, 2014

[vc_row][vc_column width=”1/1″][vc_column_text]The components used in this experiment are shown below:

QQ截图20140217143730

The components aer one red LED, one green LED, one YELLOW LED, and three 470 ohm resister, one 10k ohm resistor,one button ,six jumper wires, and one breadboard.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Schematics” tab_id=”1392618950-1-45″][vc_column_text]QQ截图20140217144233[/vc_column_text][/vc_tab][vc_tab title=”Wiring Diagram” tab_id=”1392618950-2-5″][vc_column_text]QQ截图20140217144424

3LEDs will blink, and when the button is pressed, they will stop blinking.[/vc_column_text][/vc_tab][vc_tab title=”Arduino code” tab_id=”1392619593358-2-5″][vc_column_text]Arduino code:
int redPin = 2;
int yellowPin = 3;
int greenPin = 4;
int buttonPin = 5;
int state = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buttonPin, INPUT);

}
void loop()
{
if (digitalRead(buttonPin))
{
if (state == 0) {
setLights(HIGH, LOW, LOW);
state = 1;
}
else if (state == 1) {
setLights(HIGH, HIGH, LOW);
state = 2;
}
else if (state == 2) {
setLights(LOW, LOW, HIGH);
state = 3;
}
else if (state == 3) {
setLights(LOW, HIGH, LOW);
state = 0;
}
delay(1000);
}
}
void setLights(int red, int yellow, int green)
{
digitalWrite(redPin, red);
digitalWrite(yellowPin, yellow);
digitalWrite(greenPin, green);
}[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: Introductory Kit with Clear Case for Arduino

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors