Description
In this practice we had to deal with LEDs (Light Emitting Diode) in order to perform a useful system; concretely, a program was developed to turn on and turn off the LEDs after a certain amount of time (moreover, we could do the same using switches/buttons). This task formed the minimum requirements to start experimenting with more complex ideas.
One of the main issues we found consisted in deciding how to put the button in the breadboard to turn on and turn off the LEDs. As you will see later in this entry, we have seen that when the button is switched on, the terminals on the same side become connected; otherwise, they are not connected. This fact can be observed on the following image, where terminals A and C are directly connected (as B and D). When the button is switched on, the current flows through all the nodes.
Image 1: Button functionality [1] |
The additional work we did consisted in creating a traffic light using three different LED: red, yellow and green. We thought it was a good way to show how LEDs blink and moreover it has an important application in everyday life, specially for those zones where people's concurrency is low.
This traffic light remains always in red until someone pushes a button. Then some actions are done in order to indicate that it is going to change its state (to green) soon. After that, the green light is on for a while and the same actions than before (later specified clearly) are done to indicate that it is going to change to red again. To summarize, it is not an automatic traffic light: in order to pass through the street, we have to ask for permission first.
It works as follows:
This traffic light remains always in red until someone pushes a button. Then some actions are done in order to indicate that it is going to change its state (to green) soon. After that, the green light is on for a while and the same actions than before (later specified clearly) are done to indicate that it is going to change to red again. To summarize, it is not an automatic traffic light: in order to pass through the street, we have to ask for permission first.
It works as follows:
- The traffic lights remain in red if the button is not pushed.
- When the button is pushed:
- Keep the red light on for 2 more seconds (for security reasons before crossing the street).
- Turn on/off alternatively the red and the yellow lights to indicate that the colour is going to change soon. This process is repeated five times and each light remains 200 ms on and 200 ms off for each iteration.
- Turn off the red and the yellow lights; turn on the green light for 3 seconds.
- Turn on/off alternatively the green and the yellow lights to indicate that the colour is going to change soon. This process in the same way as in step 2.
- Turn off the green and the yellow lights; turn on the red light indefinitely (return to the first step).
Circuit schema
Image 2: Traffic lights circuit schema |
Circuit photograph
Image 3: Traffic lights photograph |
Video
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /** * Traffic lights * Represents the traffic lights system we find in cities. * It is a variation in which the permission for a green * light is asked through a button. */ // Button and traffic lights ports const int BUTTON = 2; const int ledR = 13; const int ledY = 12; const int ledG = 8; // Time waited before starting to change from red to turn // on alternatively red and yellow const int waitForPass = 5000; // Time we keep the green light on const int timeGreen = 10000; // Time we keep a light on when alternates with another one const int alternTurnTime = 200; // Number of times that two lights are alternatively turned on/off const int Nblinks = 14; // Indicates whether the button is pushed (1) or not (0) int val = 0; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(ledR, OUTPUT); pinMode(ledY, OUTPUT); pinMode(ledG, OUTPUT); pinMode(BUTTON, INPUT); Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // Read the value of the button (to see if it is pushed or not) val = digitalRead(BUTTON); delay(10); // Print the value for debugging reasons Serial.println(val); // Turn the red light on digitalWrite(ledR, HIGH); // If the red button is pushed, make the transition to green. // Then return to red again. if (val == HIGH) { // Keep the red light on for 'waitForPass' time // Then turn this light off delay(waitForPass); digitalWrite(ledR, LOW); // Turn on alternatively both yellow and red lights turnLightsAlternatively(ledY, ledR); // Turn on the green light for a specified amount of time digitalWrite(ledG, HIGH); delay(timeGreen); digitalWrite(ledG, LOW); // Turn on alternatively both yellow and green lights // When finished, note that red light will be turned on // automatically when loop function restarts. turnLightsAlternatively(ledY, ledG); } } // Function to turn two lights alternatively void turnLightsAlternatively(const int light1, const int light2) { for (int i = 0; i < Nblinks; ++i) { digitalWrite(light1, HIGH); delay(alternTurnTime); digitalWrite(light1, LOW); digitalWrite(light2, HIGH); delay(alternTurnTime); digitalWrite(light2, LOW); } } |
Download the code here.
References
[1] Picture of the functionality of the button, retrieved on March, 3th from:http://www.varesano.net/blog/fabio
No comments:
Post a Comment