Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Debounced button module------------------------
- //8 buttons are read through a 74*165 shift register
- //8 LEDs are driven by a 74*541 buffer
- //can invert or toggle any of 8 outputs
- //pressing both invert & toggle lets you set hiZ
- //pins------------------------------------------
- const int shft = 8; //shift/!load
- const int clk = 9; //clock
- const int shftOut = 10; //output !qh
- const int invBtn = 11; //invert button
- const int tglBtn = 12; //toggle button
- typedef struct BUTTON {
- int outPin; //output pin to control
- int lastState; // the previous reading from the input pin
- int state;
- unsigned long tStamp; //last toggled time stamp
- int outState; //status of output pin
- bool toggle; //output toggles with each press
- bool invert; //invert output
- bool hiZ;
- } BUTTON;
- const int but_cnt = 8;
- BUTTON buttons[but_cnt]; // array of button structs
- const long debounceDelay = 50; // the debounce time
- void setup() {
- //shift reg for buttons 0-7
- pinMode(shft, OUTPUT);
- pinMode(clk, OUTPUT);
- pinMode(shftOut, INPUT);
- digitalWrite(shft, HIGH);
- pinMode(invBtn, INPUT_PULLUP);
- pinMode(tglBtn, INPUT_PULLUP);
- //--------outPin, lastState, state, tStamp, outState, toggle, invert
- buttons[0] = (BUTTON){7, LOW, LOW, 0, LOW, false, false, false}; //init button array
- buttons[1] = (BUTTON){6, LOW, LOW, 0, LOW, false, false, false};
- buttons[2] = (BUTTON){5, LOW, LOW, 0, LOW, false, false, false};
- buttons[3] = (BUTTON){4, LOW, LOW, 0, LOW, false, false, false};
- buttons[4] = (BUTTON){3, LOW, LOW, 0, LOW, false, false, false};
- buttons[5] = (BUTTON){2, LOW, LOW, 0, LOW, false, false, false};
- buttons[6] = (BUTTON){1, LOW, LOW, 0, LOW, false, false, false};
- buttons[7] = (BUTTON){0, LOW, LOW, 0, LOW, false, false, false};
- for (int i = 0; i < but_cnt; i++){ //setup pins
- pinMode(buttons[i].outPin, OUTPUT); //out 0
- digitalWrite(buttons[i].outPin, buttons[i].outState);
- }
- }
- void loop() {
- //check toggle & invert buttons-----------------
- int togglePressed = !digitalRead(tglBtn);
- int invertPressed = !digitalRead(invBtn);
- //latch button shift register--------------------
- digitalWrite(shft, LOW);
- delay(1);
- digitalWrite(shft, HIGH); //shift
- delay(1);
- //loop buttons-----------------------------------
- for (int i = 0; i < but_cnt; i++){
- int reading = digitalRead(shftOut); //read button
- digitalWrite(clk, LOW); //clock next bit out of shift reg
- delay(1);
- digitalWrite(clk, HIGH);
- //reset debounce timer if state changed
- if (reading != buttons[i].lastState) buttons[i].tStamp = millis();
- if ((millis() - buttons[i].tStamp) > debounceDelay) { //waited longer than delay
- if (reading != buttons[i].state) { //button has changed
- buttons[i].state = reading;
- if (togglePressed || invertPressed){ //one or both modifier buttons pressed
- if (reading){ //toggle settings
- if (!(togglePressed && invertPressed)){ // one or the other
- buttons[i].toggle = buttons[i].toggle ^ togglePressed;
- buttons[i].invert = buttons[i].invert ^ invertPressed;
- } else { //both = hiZ toggle
- buttons[i].hiZ = !buttons[i].hiZ;
- pinMode(buttons[i].outPin, buttons[i].hiZ ? INPUT:OUTPUT); //toggle hiZ output
- }
- }
- } else { //no modifier buttons pressed
- if (buttons[i].toggle){
- if (reading) buttons[i].outState = !buttons[i].outState;
- } else {
- buttons[i].outState = reading;
- }
- }
- }
- }
- int output = buttons[i].outState ^ buttons[i].invert;
- digitalWrite(buttons[i].outPin, output); //write output
- buttons[i].lastState = reading;
- }
- }
Add Comment
Please, Sign In to add comment