Hello World!
Well I have recently started learning with the Arduino. I think its brilliant!
I have done the blinking LEDs and other simple examples, which were really useful.
I believe the best way to learn it to experiment.
So here is me learning about going from simple to complex using the example of the humble traffic light.
Version 1 a single set of traffic lights that go.
Red
Red & Amber
Green
Amber
Red
and repeats ad infinitum.
After that I will complicate them by imagining ever more complex road junctions, crossings, car sensors, different sequences for rush hour etc.
Come with me, it should be fun. If I get stuck, or write rubbish sketches, feel free to jump in!
Traffic Lights 1
red LED & Resistor to digital pin 13
amber LED & Resistor to digital pin 12
green LED & Resistor to digital pin 11
/*
*Do what you like with this code as long as you set
*it free and allow others to do the same. i.e.
-------------------------------------------------
*This work is licensed under the Creative Commons
*Attribution-ShareAlike 3.0 Unported License.
*To view a copy of this license, visit
*http://creativecommons.org/licenses/by-sa/3.0/
*or send a letter to Creative Commons, 444 Castro Street,
*Suite 900, Mountain View, California, 94041, USA.
----------------------------------------
*A set of Traffic Lights.
*I am in the UK so it will be a UK set of traffic lights.
*Should be easy to customise to any configuration in the world!
*Come with me, it should be fun.
*I intend to comment every line to start with
*so it should be easy to follow.
*/
int red = 13; //first of all associate a pin with the red light, in this case 13
int amber = 12; // associate pin 12 with the amber light
int green = 11; //and finally the green light with pin 11
int maindelay = 1500; //The timings of the lights are based around this number.
/*
*The maindelay is an arbitary delay time,
*smaller is faster, larger is slower.
*I use it so the proportions stay the same.
*i.e. the wait is 4 times longer than the change
*/
void setup() {
pinMode(red,OUTPUT); //set the red pin as an output
pinMode(amber,OUTPUT); //set the amber pin as an output
pinMode(green,OUTPUT); //set the green pin as an output
digitalWrite(red,HIGH); //Switch the red light on so we have something to start with
}
void loop(){
digitalWrite(amber,HIGH); //Amber on, get ready to go
delay(maindelay); //Wait
digitalWrite(green,HIGH); //Green on, go
digitalWrite(red,LOW); //Red off as green is on
digitalWrite(amber,LOW); //amber off, it's finished with
delay(maindelay*4); //give traffic time to pass
digitalWrite(amber,HIGH); //amber on, stop if safe to do so
digitalWrite(green,LOW); //green off, it's finished with
delay(maindelay); //wait
digitalWrite(amber,LOW); //amber off
digitalWrite(red,HIGH); //red on, stop
delay(maindelay*4); //Time for whatever the traffic has been stopped for to happen.
/*And we are back where we started.
*That's the end of this sequence.
*The light is back at red and the
*sequence can begin again.
*/
}
Well. I hope it works for you. I'll be back in a few days with another set of lights. I am thinking of a crossroads where North-South goes while East-West waits, and vice versa.
See you soon
Bookworm.