Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Assign values to represent colours.
- * This allows us to abstract input pins etc to arrays.
- */
- #define red 0
- #define yellow 1
- #define green 2
- #define cycle 4
- #define num_states 3
- /* Pin mapping for inputs and outputs.
- * Thanks to the values assigned to colours above, this conveniently
- * makes "inputs[red]" the red input button, "inputs[yellow]" the yellow
- * input button, etc.
- */
- const int inputs[4] = {2, 3, 4, 5}, outputs[3] = {6, 8, 7};
- /* Button states for debouncing purposes.
- * Similarly to above, "buttonState[redButton]" gives the state of the
- * red input button, etc.
- */
- int buttonState[4] = {LOW,LOW,LOW,LOW}, previousState[4] = {LOW,LOW,LOW,LOW};
- unsigned long cycleDelay = 1000, previousCycle = 0,
- debounceDelay = 50, previousDebounce[4] = {0,0,0,0};
- /* A variable to keep track of the current state and one to track whether the colours
- * are currently cycling.
- */
- int state;
- boolean cycling;
- void setup() {
- // set mode for input pins
- for(int i=0; i<4; i++) {
- pinMode(inputs[i], INPUT);
- }
- // set mode for output pins and turn everything off
- for(int i=0; i<3; i++) {
- pinMode(outputs[i], OUTPUT);
- digitalWrite(outputs[i], HIGH);
- }
- // simulate button presses to have the lights do initially do something
- handleButton(green);
- handleButton(cycle);
- }
- void loop() {
- // debounce our four inputs
- for(int i=0; i<4; i++) {
- debounce(i);
- }
- // similar to blink without delay, but only if we are currently cycling the lights
- if(cycling && millis() - previousCycle >= cycleDelay) {
- cycleLights();
- }
- }
- /** Debounce code taken from the Arduino website and abstracted to multiple buttons
- * See here for more details:
- * https://www.arduino.cc/en/Tutorial/Debounce/
- */
- void debounce(int x) {
- int reading = digitalRead(inputs[x]);
- if(reading != previousState[x]) {
- previousDebounce[x] = millis();
- }
- if((millis() - previousDebounce[x]) > debounceDelay) {
- if(reading != buttonState[x]) {
- buttonState[x] = reading;
- handleButton(x);
- }
- }
- previousState[x] = reading;
- }
- /* Handle a button press.
- * x takes one of four possible vales: red, yellow, green or cycle; i.e. 0, 1, 2 or 3
- */
- void handleButton(int x) {
- if(x == cycle) {
- cycleLights();
- cycling = true;
- } else {
- cycling = false;
- updateState(x);
- }
- }
- /* Modify the current state and adjust lights accordingly.
- * x takes one of four possible vales: red, yellow, green or cycle; i.e. 0, 1, 2 or 3
- */
- void updateState(int x) {
- digitalWrite(outputs[state], HIGH);
- state = x;
- digitalWrite(outputs[state], LOW);
- }
- /* Cycle which light is currently lit.
- * The lights cycle red -> yellow -> green -> red
- */
- void cycleLights() {
- int newState = state+1;
- if(newState >= num_states) state = 0;
- updateState(newState);
- previousCycle = millis();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement