#define DEBOUNCE 10 // how many ms to debounce, 5+ ms is usually plenty //define the buttons that we'll use. byte buttons[] = {4, 5, 6, 7, 8, 9, 10, 11}; //determine how big the array up above is, by checking the size #define NUMBUTTONS sizeof(buttons) //track if a button is just pressed, just released, or 'currently pressed' byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS]; byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS]; void setup() { byte i; Serial.begin(9600); //set up serial port Serial.print("Button checker with "); Serial.print(NUMBUTTONS, DEC); Serial.println(" buttons"); // Make input & enable pull-up resistors on switch pins //outputs settings pinMode(A0, OUTPUT);//output 1 digitalWrite(A0, LOW); pinMode(A1, OUTPUT);//output 2 digitalWrite(A1, LOW); pinMode(A2, OUTPUT);//output 3 digitalWrite(A2, LOW); pinMode(A3, OUTPUT);//output 4 digitalWrite(A3, LOW); pinMode(A4, OUTPUT);//output 5 digitalWrite(A4, LOW); pinMode(A5, OUTPUT);//output 6 digitalWrite(A5, HIGH); pinMode(2, OUTPUT);//output 7 digitalWrite(2, LOW); pinMode(3, OUTPUT);//output 8 digitalWrite(3, LOW); for (i=0; i< NUMBUTTONS; i++) { pinMode(buttons[i], INPUT); digitalWrite(buttons[i], HIGH); } } void loop() { byte thisSwitch=thisSwitch_justPressed(); switch(thisSwitch) { case 0: Serial.println("switch 1 just pressed"); smazto(); digitalWrite(A0, HIGH); //output 1 break; case 1: Serial.println("switch 2 just pressed"); smazto(); digitalWrite(A1, HIGH); //output 2 break; case 2: Serial.println("switch 3 just pressed"); smazto(); digitalWrite(A2, HIGH); //output 3 break; case 3: Serial.println("switch 4 just pressed"); smazto(); digitalWrite(A3, HIGH); //output 4 break; case 4: Serial.println("switch 5 just pressed"); smazto(); digitalWrite(A4, HIGH); //output 5 break; case 5: Serial.println("switch 6 just pressed"); smazto(); digitalWrite(A5, HIGH); //output 6 break; case 6: Serial.println("switch 7 just pressed"); smazto(); digitalWrite(2, HIGH); //output 7 break; case 7: Serial.println("switch 8 just pressed"); smazto(); digitalWrite(3, HIGH); //output 8 break; } } void check_switches() { static byte previousstate[NUMBUTTONS]; static byte currentstate[NUMBUTTONS]; static long lasttime; byte index; if (millis() < lasttime) { // we wrapped around, lets just try again lasttime = millis(); } if ((lasttime + DEBOUNCE) > millis()) { // not enough time has passed to debounce return; } // ok we have waited DEBOUNCE milliseconds, lets reset the timer lasttime = millis(); for (index = 0; index < NUMBUTTONS; index++) { justpressed[index] = 0; //when we start, we clear out the "just" indicators justreleased[index] = 0; currentstate[index] = digitalRead(buttons[index]); //read the button if (currentstate[index] == previousstate[index]) { if ((pressed[index] == LOW) && (currentstate[index] == LOW)) { // just pressed justpressed[index] = 1; } else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) { justreleased[index] = 1; // just released } pressed[index] = !currentstate[index]; //remember, digital HIGH means NOT pressed } previousstate[index] = currentstate[index]; //keep a running tally of the buttons } } byte thisSwitch_justPressed() { byte thisSwitch = 255; check_switches(); //check the switches & get the current state for (byte i = 0; i < NUMBUTTONS; i++) { current_keystate[i]=justpressed[i]; if (current_keystate[i] != previous_keystate[i]) { if (current_keystate[i]) thisSwitch=i; } previous_keystate[i]=current_keystate[i]; } return thisSwitch; } void smazto(){ //outputs settings digitalWrite(A0, LOW);//output 1 digitalWrite(A1, LOW);//output 2 digitalWrite(A2, LOW);//output 3 digitalWrite(A3, LOW);//output 4 digitalWrite(A4, LOW);//output 5 digitalWrite(A5, LOW);//output 6 digitalWrite(2, LOW);//output 7 digitalWrite(3, LOW);//output 8 }