Charles Tritt / Mbed 2 deprecated TrafficEx

Dependencies:   mbed

Fork of TimeoutEx by Charles Tritt

Committer:
CSTritt
Date:
Wed Oct 11 01:59:34 2017 +0000
Revision:
4:587825079f11
Parent:
3:439f58e558e5
Child:
5:307135bd050f
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:3151531e9a31 1 /*
CSTritt 4:587825079f11 2 Project: TrafficEx
CSTritt 0:3151531e9a31 3 File: main.cpp
CSTritt 0:3151531e9a31 4
CSTritt 4:587825079f11 5 Control a hypothetical traffic light using Timeouts. Display a Violation
CSTritt 4:587825079f11 6 message if light is run (using polling in main).
CSTritt 4:587825079f11 7
CSTritt 4:587825079f11 8 Car active high input (D6) is created by placing the photocell on the low
CSTritt 4:587825079f11 9 side of a voltage divider created using a series of 1 kOhm resistors (the
CSTritt 4:587825079f11 10 number depends on ambient conditions). Three worked well for me.
CSTritt 4:587825079f11 11
CSTritt 4:587825079f11 12 The red, yellow and green LEDs are connected to pins D2, D3 and D4,
CSTritt 4:587825079f11 13 respectively through 330 Ohm resistors. They are active high.
CSTritt 0:3151531e9a31 14
CSTritt 0:3151531e9a31 15 Created by Dr. C. S. Tritt
CSTritt 4:587825079f11 16 Last revised: 10/10/17 (v. 1.0)
CSTritt 0:3151531e9a31 17 */
CSTritt 0:3151531e9a31 18 #include "mbed.h"
CSTritt 0:3151531e9a31 19
CSTritt 4:587825079f11 20 const float grnTime = 3.0f; // Green duration.
CSTritt 4:587825079f11 21 const float ylwTime = 2.0f; // Yellow duration.
CSTritt 4:587825079f11 22 const float redTime = 4.0f; // Red duration.
CSTritt 4:587825079f11 23
CSTritt 4:587825079f11 24 // These functions change to the indicated color and schedule the next change.
CSTritt 4:587825079f11 25 void grnChange();
CSTritt 4:587825079f11 26 void ylwChange();
CSTritt 4:587825079f11 27 void redChange();
CSTritt 1:2438293c128c 28
CSTritt 4:587825079f11 29 Timeout changeLight; // Reuse for all colors.
CSTritt 0:3151531e9a31 30
CSTritt 4:587825079f11 31 // Red, yellow and green LEDs.
CSTritt 4:587825079f11 32 DigitalOut redLight(D2);
CSTritt 4:587825079f11 33 DigitalOut ylwLight(D3);
CSTritt 4:587825079f11 34 DigitalOut grnLight(D4);
CSTritt 0:3151531e9a31 35
CSTritt 4:587825079f11 36 // Analog signal used as digital input. Threshold is about 1.6 V.
CSTritt 4:587825079f11 37 DigitalIn car(D6);
CSTritt 0:3151531e9a31 38
CSTritt 0:3151531e9a31 39 int main() {
CSTritt 4:587825079f11 40
CSTritt 4:587825079f11 41 redLight = 1; // Start with red on. Seems safest.
CSTritt 4:587825079f11 42 grnLight = 0;
CSTritt 4:587825079f11 43 ylwLight = 0;
CSTritt 0:3151531e9a31 44
CSTritt 4:587825079f11 45 redChange(); // Call redChange to start cycling.
CSTritt 4:587825079f11 46
CSTritt 4:587825079f11 47 // redChange will return here almost immediately (after scheduling
CSTritt 4:587825079f11 48 // grnChange).
CSTritt 4:587825079f11 49
CSTritt 1:2438293c128c 50 while(true) { // Main loop.
CSTritt 4:587825079f11 51 // Display RYG status, stay on line. Okay to use printf in main.
CSTritt 4:587825079f11 52 printf("RYG: %d %d %d. ", (int) redLight, (int) ylwLight,
CSTritt 4:587825079f11 53 (int) grnLight);
CSTritt 4:587825079f11 54 // Display car status, stay on line.
CSTritt 4:587825079f11 55 printf("Car: %d.", (int) car);
CSTritt 4:587825079f11 56 if ((int) car && (int) redLight) {
CSTritt 4:587825079f11 57 printf(" Violation!\n"); // Note violation.
CSTritt 1:2438293c128c 58 } else {
CSTritt 4:587825079f11 59 printf("\n"); // Just advance a line.
CSTritt 4:587825079f11 60 }
CSTritt 0:3151531e9a31 61 wait(0.5); // Pause half a second.
CSTritt 0:3151531e9a31 62 }
CSTritt 1:2438293c128c 63 }
CSTritt 1:2438293c128c 64
CSTritt 4:587825079f11 65 void grnChange(){
CSTritt 4:587825079f11 66 redLight = 0; // Turn off red.
CSTritt 4:587825079f11 67 grnLight = 1; // Turn on green.
CSTritt 4:587825079f11 68 changeLight.attach(&ylwChange, grnTime); // Schedule change to yellow.
CSTritt 1:2438293c128c 69 }
CSTritt 1:2438293c128c 70
CSTritt 4:587825079f11 71 void ylwChange(){
CSTritt 4:587825079f11 72 grnLight = 0; // Turn off grn.
CSTritt 4:587825079f11 73 ylwLight = 1; // Turn on yellow.
CSTritt 4:587825079f11 74 changeLight.attach(&redChange, ylwTime); // Schedule change to red.
CSTritt 4:587825079f11 75 }
CSTritt 4:587825079f11 76
CSTritt 4:587825079f11 77 void redChange(){
CSTritt 4:587825079f11 78 ylwLight = 0; // Turn off yellow.
CSTritt 4:587825079f11 79 redLight = 1; // Turn on red.
CSTritt 4:587825079f11 80 changeLight.attach(&grnChange, redTime); // Schedule change to green.
CSTritt 0:3151531e9a31 81 }