Author: Jacob Mason Language: c
Description: stoplight Timestamp: 2013-06-13 04:10:31 +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
  37.         }
  38.         //final yellow lights before repeating the pattern
  39.         digitalWrite(last2, LOW);
  40.         digitalWrite(last1, LOW);
  41.         digitalWrite(YELLOW2, HIGH);
  42.         digitalWrite(YELLOW1, HIGH);
  43.         last2 = YELLOW2;
  44.         last1 = YELLOW1;
  45.  
  46. }
  47.  
  48. void light2(int state) {
  49.         digitalWrite(last2, LOW);
  50.         if (state == RED1) {
  51.                 digitalWrite(GREEN2, HIGH);
  52.                 last2 = GREEN2;
  53.                 delay(2000);
  54.         }
  55.         else if (state == YELLOW1) {
  56.                 digitalWrite(YELLOW2, HIGH);
  57.                 last2 = YELLOW2;
  58.         }
  59.         else if (state == GREEN1) {
  60.                 digitalWrite(RED2, HIGH);
  61.                 last2 = RED2;
  62.                 delay(2000);
  63.         }
  64. }
View raw paste Reply