Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /* Assign values to represent colours.
  2.  * This allows us to abstract input pins etc to arrays.
  3.  */
  4. #define red    0
  5. #define yellow 1
  6. #define green  2
  7. #define cycle  4
  8.  
  9. #define num_states 3
  10.  
  11. /* Pin mapping for inputs and outputs.
  12.  * Thanks to the values assigned to colours above, this conveniently
  13.  * makes "inputs[red]" the red input button, "inputs[yellow]" the yellow
  14.  * input button, etc.
  15.  */
  16. const int inputs[4] = {2, 3, 4, 5}, outputs[3] = {6, 8, 7};
  17.  
  18. /* Button states for debouncing purposes.
  19.  * Similarly to above, "buttonState[redButton]" gives the state of the
  20.  * red input button, etc.
  21.  */
  22. int buttonState[4] = {LOW,LOW,LOW,LOW}, previousState[4] = {LOW,LOW,LOW,LOW};
  23.  
  24.  
  25. unsigned long cycleDelay = 1000, previousCycle = 0,
  26.               debounceDelay = 50, previousDebounce[4] = {0,0,0,0};
  27.  
  28. /* A variable to keep track of the current state and one to track whether the colours
  29.  * are currently cycling.
  30.  */
  31. int state;
  32. boolean cycling;
  33.  
  34. void setup() {
  35.   // set mode for input pins
  36.   for(int i=0; i<4; i++) {
  37.     pinMode(inputs[i], INPUT);
  38.   }
  39.  
  40.   // set mode for output pins and turn everything off
  41.   for(int i=0; i<3; i++) {
  42.     pinMode(outputs[i], OUTPUT);
  43.     digitalWrite(outputs[i], HIGH);
  44.   }
  45.  
  46.   // simulate button presses to have the lights do initially do something
  47.   handleButton(green);
  48.   handleButton(cycle);
  49. }
  50.  
  51. void loop() {
  52.   // debounce our four inputs
  53.   for(int i=0; i<4; i++) {
  54.     debounce(i);
  55.   }
  56.  
  57.   // similar to blink without delay, but only if we are currently cycling the lights
  58.   if(cycling && millis() - previousCycle >= cycleDelay) {
  59.     cycleLights();
  60.   }
  61. }
  62.  
  63. /** Debounce code taken from the Arduino website and abstracted to multiple buttons
  64.  *  See here for more details:
  65.  *  https://www.arduino.cc/en/Tutorial/Debounce/
  66.  */
  67. void debounce(int x) {
  68.   int reading = digitalRead(inputs[x]);
  69.   if(reading != previousState[x]) {
  70.     previousDebounce[x] = millis();
  71.   }
  72.   if((millis() - previousDebounce[x]) > debounceDelay) {
  73.     if(reading != buttonState[x]) {
  74.       buttonState[x] = reading;
  75.  
  76.       handleButton(x);
  77.     }
  78.   }
  79.   previousState[x] = reading;
  80. }
  81.  
  82. /* Handle a button press.
  83.  * x takes one of four possible vales: red, yellow, green or cycle; i.e. 0, 1, 2 or 3
  84.  */
  85. void handleButton(int x) {
  86.   if(x == cycle) {
  87.     cycleLights();
  88.     cycling = true;
  89.   } else {
  90.     cycling = false;
  91.     updateState(x);
  92.   }
  93. }
  94.  
  95. /* Modify the current state and adjust lights accordingly.
  96.  * x takes one of four possible vales: red, yellow, green or cycle; i.e. 0, 1, 2 or 3
  97.  */
  98. void updateState(int x) {
  99.   digitalWrite(outputs[state], HIGH);
  100.   state = x;
  101.   digitalWrite(outputs[state], LOW);
  102. }
  103.  
  104. /* Cycle which light is currently lit.
  105.  * The lights cycle red -> yellow -> green -> red
  106.  */
  107. void cycleLights() {
  108.   int newState = state+1;
  109.   if(newState >= num_states) state = 0;
  110.   updateState(newState);
  111.   previousCycle = millis();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement