Author: Jacob Mason Language: c
Description: stoplight Timestamp: 2013-06-13 03:57:55 +0000
View raw paste Reply
  1. /* Stoplight program
  2.    Written by Jacob Mason
  3.    6/12/2013
  4. */
  5.  
  6. //preprossessor directives
  7. #define RED1 5
  8. #define YELLOW1 4
  9. #define GREEN1 3
  10.  
  11. #define RED2 11
  12. #define YELLOW2 9
  13. #define GREEN2 8
  14.  
  15. //declarations
  16. int light1State = 0; //keep track of the state for light1
  17. int light1Array[3] = {RED1, YELLOW1, GREEN1};
  18. int last1 = RED1;
  19. int last2 = GREEN2;
  20.  
  21. void setup() {
  22.         pinMode(RED1, OUTPUT);
  23.         pinMode(YELLOW1, OUTPUT);
  24.         pinMode(GREEN1, OUTPUT);
  25.         pinMode(RED2, OUTPUT);
  26.         pinMode(YELLOW2, OUTPUT);
  27.         pinMode(GREEN2, OUTPUT);
  28. }
  29.  
  30. void loop() {
  31.         for (int i = 0; i < 3; i++) {
  32.                 delay(2000);
  33.                 digitalWrite(last1, LOW); //turn off the last light
  34.                 digitalWrite(light1Array[i], HIGH); //turn on the next light in the array
  35.                 last1 = light1Array[i]; //store the light that is currently on so it can be turned off later
  36.                 light2(light1Array[i]); //determine which light to turn on (based on light 1) for traffic light 2
  37.         }
  38. }
  39.  
  40. void light2(int state) {
  41.         digitalWrite(last2, LOW);
  42.         if (state == RED1) {
  43.                 digitalWrite(GREEN2, HIGH);
  44.                 last2 = GREEN2;
  45.                 delay(2000);
  46.         }
  47.         else if (state == YELLOW1) {
  48.                 digitalWrite(YELLOW2, HIGH);
  49.                 last2 = YELLOW2;
  50.         }
  51.         else if (state == GREEN1) {
  52.                 digitalWrite(RED2, HIGH);
  53.                 last2 = RED2;
  54.                 delay(2000);
  55.         }
  56. }
View raw paste Reply