After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(13, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(12, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(11, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(11, LOW); // Turns LED on pin 11 off
delay(0);
}[/code]
Double lane code below
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5
int var = 0;
void setup() {
// declare pins 11,12,13,5,6,7 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop(){{delay(1000);
if (var == 0)
digitalWrite(5, HIGH);
delay(0);
var = 1;
if (var == 1)
digitalWrite(13, HIGH);
delay(0);
var = 0; }
{ delay(7000);
if (var == 0)
digitalWrite(13, LOW);
delay(0);
var = 1;
if (var == 1)
digitalWrite(12, HIGH);
delay(0);
var = 2;}
{
delay(5000);
if (var == 2)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(12, LOW);
delay(0);
var = 3;
}
{ delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(5, HIGH);
delay(0);
var = 3;
}{
delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(5, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, HIGH);
delay(0);
var = 4;}
{ delay(5000);
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(6, HIGH);
delay(0);
var = 2; }
{ delay(3000);
if (var == 2)
digitalWrite(6, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(3000);
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(000);
if (var == 3)
digitalWrite(11, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite (13, HIGH);
delay(0);
var = 3; }
}[/code]">
After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(13, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(12, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(11, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(11, LOW); // Turns LED on pin 11 off
delay(0);
}[/code]
Double lane code below
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5
int var = 0;
void setup() {
// declare pins 11,12,13,5,6,7 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop(){{delay(1000);
if (var == 0)
digitalWrite(5, HIGH);
delay(0);
var = 1;
if (var == 1)
digitalWrite(13, HIGH);
delay(0);
var = 0; }
{ delay(7000);
if (var == 0)
digitalWrite(13, LOW);
delay(0);
var = 1;
if (var == 1)
digitalWrite(12, HIGH);
delay(0);
var = 2;}
{
delay(5000);
if (var == 2)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(12, LOW);
delay(0);
var = 3;
}
{ delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(5, HIGH);
delay(0);
var = 3;
}{
delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(5, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, HIGH);
delay(0);
var = 4;}
{ delay(5000);
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(6, HIGH);
delay(0);
var = 2; }
{ delay(3000);
if (var == 2)
digitalWrite(6, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(3000);
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(000);
if (var == 3)
digitalWrite(11, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite (13, HIGH);
delay(0);
var = 3; }
}[/code]">
After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(13, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(12, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(11, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(11, LOW); // Turns LED on pin 11 off
delay(0);
}[/code]
Double lane code below
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5
int var = 0;
void setup() {
// declare pins 11,12,13,5,6,7 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop(){{delay(1000);
if (var == 0)
digitalWrite(5, HIGH);
delay(0);
var = 1;
if (var == 1)
digitalWrite(13, HIGH);
delay(0);
var = 0; }
{ delay(7000);
if (var == 0)
digitalWrite(13, LOW);
delay(0);
var = 1;
if (var == 1)
digitalWrite(12, HIGH);
delay(0);
var = 2;}
{
delay(5000);
if (var == 2)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(12, LOW);
delay(0);
var = 3;
}
{ delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(5, HIGH);
delay(0);
var = 3;
}{
delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(5, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, HIGH);
delay(0);
var = 4;}
{ delay(5000);
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(6, HIGH);
delay(0);
var = 2; }
{ delay(3000);
if (var == 2)
digitalWrite(6, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(3000);
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(000);
if (var == 3)
digitalWrite(11, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite (13, HIGH);
delay(0);
var = 3; }
}[/code]">
The Engineering Projects
A lot of Engineering projects and tutorials for the students to help them in their final year projects and semester projects.
Hello all!
I'm a new member to the arduino community.
I received my duemilanove yesterday in the mail and she is indeed a beauty.
This morning I started familiarizing myself with coding and decided "why not make my own?".
After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(13, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(12, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(11, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(11, LOW); // Turns LED on pin 11 off
delay(0);
}[/code]
Double lane code below
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5
int var = 0;
void setup() {
// declare pins 11,12,13,5,6,7 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop(){{delay(1000);
if (var == 0)
digitalWrite(5, HIGH);
delay(0);
var = 1;
if (var == 1)
digitalWrite(13, HIGH);
delay(0);
var = 0; }
{ delay(7000);
if (var == 0)
digitalWrite(13, LOW);
delay(0);
var = 1;
if (var == 1)
digitalWrite(12, HIGH);
delay(0);
var = 2;}
{
delay(5000);
if (var == 2)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(12, LOW);
delay(0);
var = 3;
}
{ delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(5, HIGH);
delay(0);
var = 3;
}{
delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(5, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, HIGH);
delay(0);
var = 4;}
{ delay(5000);
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(6, HIGH);
delay(0);
var = 2; }
{ delay(3000);
if (var == 2)
digitalWrite(6, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(3000);
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(000);
if (var == 3)
digitalWrite(11, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite (13, HIGH);
delay(0);
var = 3; }
}[/code]
Says:
[quote=Watters post_id=156 time=1487944138 user_id=78]
Hello all!
I'm a new member to the arduino community.
I received my duemilanove yesterday in the mail and she is indeed a beauty.
This morning I started familiarizing myself with coding and decided "why not make my own?".
After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11
void setup() {
// declare pin 11,12,13 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(13, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(12, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(11, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(11, LOW); // Turns LED on pin 11 off
delay(0);
}[/code]
Double lane code below
[code]// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5
int var = 0;
void setup() {
// declare pins 11,12,13,5,6,7 to be outputs:
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
void loop(){{delay(1000);
if (var == 0)
digitalWrite(5, HIGH);
delay(0);
var = 1;
if (var == 1)
digitalWrite(13, HIGH);
delay(0);
var = 0; }
{ delay(7000);
if (var == 0)
digitalWrite(13, LOW);
delay(0);
var = 1;
if (var == 1)
digitalWrite(12, HIGH);
delay(0);
var = 2;}
{
delay(5000);
if (var == 2)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(12, LOW);
delay(0);
var = 3;
}
{ delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(5, HIGH);
delay(0);
var = 3;
}{
delay(3000);
if (var == 3)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(5, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, HIGH);
delay(0);
var = 4;}
{ delay(5000);
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 2;
if (var == 2)
digitalWrite(7, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(6, HIGH);
delay(0);
var = 2; }
{ delay(3000);
if (var == 2)
digitalWrite(6, LOW);
delay(0);
var = 3;
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(3000);
if (var == 3)
digitalWrite(5, HIGH);
delay(0);
var = 4;
if (var == 4)
digitalWrite(11, HIGH);
delay(0);
var = 3; }
{delay(000);
if (var == 3)
digitalWrite(11, LOW);
delay(0);
var = 2;
if (var == 2)
digitalWrite (13, HIGH);
delay(0);
var = 3; }
}[/code][/quote]
Couple of corrections
delay(2500); actually delays for 2.5 seconds
delay(0); is not needed
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
*snip*
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
*snip*
}
Those red curly brackets are not needed
Please note that I'm not trying to discourage you or anything, I hope it didn't come out that way anyway.
Now a challenge for you:
Add a second lane, and change your light durations: Green for 7, Yellow for 3, Red for 10.
Time NS lane EW lane
0S Green Red
7S Yellow Red
10S Red Green
17S Red Yellow
20S Green Red
etc...
Try doing this without using delay();
And a couple of hints to get you started:
You can use If Then statements to get the Red Lights to light up while the opposite green or yellow lights are lit
I will recommend to view this tutorial: [url=http://www.theengineeringprojects.com/2015/09/traffic-signal-control-project-using-arduino.html]Traffic Signal Control using Arduino[/url]
Reply
delay(2500); actually delays for 2.5 seconds
delay(0); is not needed
void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
*snip*
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
*snip*
}
Those red curly brackets are not needed
Please note that I'm not trying to discourage you or anything, I hope it didn't come out that way anyway.
Now a challenge for you:
Add a second lane, and change your light durations: Green for 7, Yellow for 3, Red for 10.
Time NS lane EW lane
0S Green Red
7S Yellow Red
10S Red Green
17S Red Yellow
20S Green Red
etc...
Try doing this without using delay();
And a couple of hints to get you started:
You can use If Then statements to get the Red Lights to light up while the opposite green or yellow lights are lit
I will recommend to view this tutorial:
[url=http://www.theengineeringprojects.com/2015/09/traffic-signal-control-project-using-arduino.html]Traffic Signal Control using Arduino