stojt3:
So Nick, so with the code(the one I just posted last night) I just made if it works, it could read multiple button switch pressed even with the 50ms delay because the delay would only matter to the certain switch I just pressed right?
You mean on page 1?
No, I mean if you rework to test for elapsed time. Thinking about this a bit, if you want to test for multiple presses (like guitar chords) then you have to define what a multiple press is. They will never be exactly simultaneous. But perhaps that doesn't matter too much.
Look, this is what I am talking about:
// Example of debouncing multiple switches
// Author: Nick Gammon
// Date: 2 January 2013
// Released for public use.
const unsigned long debounceTime = 20; // milliseconds
const int numSwitches = 6; // how many switches, for arrays below
// where switches are connected
const byte switchPin [numSwitches] = { 2, 3, 4, 5, 6, 7 };
// when last changed state
unsigned long lastPressedTime [numSwitches];
// current state
byte switchState [numSwitches];
// have we changed state in any switch?
bool stateChange = false;
void setupSwitches ()
{
for (int i = 0; i < numSwitches; i++)
{
pinMode (switchPin [i], INPUT_PULLUP);
switchState [i] = HIGH; // not pressed
lastPressedTime [i] = 0;
} // end of for each switch
} // end of setupSwitches
void setup ()
{
Serial.begin (115200);
setupSwitches ();
} // end of setup
// check switches
void checkSwitches ()
{
unsigned long now = millis ();
for (int i = 0; i < numSwitches; i++)
{
byte currentState = digitalRead (switchPin [i]);
if ((currentState != switchState [i]) &&
(now - lastPressedTime [i] >= debounceTime))
{
switchState [i] = currentState;
lastPressedTime [i] = now;
stateChange = true; // a switch has changed state
} // end of changed state
} // end of for each switch
} // end of checkSwitches
void debugSwitchStatus ()
{
if (stateChange)
{
Serial.print ("Switch ");
for (int i = 0; i < numSwitches; i++)
{
Serial.print (i + 1);
Serial.print (" = ");
Serial.print ((switchState [i] == LOW) ? "ON " : "off ");
} // end of for
Serial.println ();
stateChange = false;
}
} // end of debugSwitchStatus
void loop ()
{
checkSwitches ();
debugSwitchStatus ();
} // end of loop
Example output:
Switch 1 = off 2 = ON 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = ON 3 = off 4 = ON 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = ON 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = ON 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = ON 5 = off 6 = ON
Switch 1 = off 2 = off 3 = off 4 = ON 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = ON 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = ON 2 = off 3 = ON 4 = off 5 = off 6 = off
Switch 1 = ON 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = ON
Switch 1 = off 2 = off 3 = ON 4 = off 5 = off 6 = ON
Switch 1 = off 2 = off 3 = ON 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = ON 4 = off 5 = off 6 = off
Switch 1 = off 2 = ON 3 = ON 4 = off 5 = off 6 = off
Switch 1 = off 2 = ON 3 = ON 4 = off 5 = ON 6 = off
Switch 1 = off 2 = ON 3 = ON 4 = ON 5 = ON 6 = off
Switch 1 = off 2 = ON 3 = ON 4 = off 5 = ON 6 = off
Switch 1 = off 2 = ON 3 = ON 4 = off 5 = off 6 = off
Switch 1 = off 2 = ON 3 = off 4 = off 5 = off 6 = off
Switch 1 = off 2 = off 3 = off 4 = off 5 = off 6 = off
No delays. Therefore it works fast. However the debounce time is used to consider whether or not to notice a state change in a particular switch.