Hi there, I'm working on a project with the Mega 2560 that will ultimately use a bunch of ten switches and various LEDs being lit depending on which switch was pressed. (The project is actually more complicated than that but I've simplified things for this post. One of the simplifications is that I've reduced the number of switches and LEDs to two each.) For various reasons I'm using an interrupt for the recognizing that a switch was pressed and then polling the switches to determine which one was pressed.
The problem I'm having is that while the interrupt works just fine when either of the switches is pressed (they are wire-OR'd together into the interrupt pin), when (after debouncing) I then poll the switches to see which one was the one pressed, they both return a HIGH even though the switches are grounded and the ISR is set to activate on LOW only. (The inputs on the MEGA are set to INPUT_PULLUP). (Please see the circuit diagram below the code.) I've verified that the ISR only activates when a switch is pressed. What I want this code to do (and think it should be doing) is to flash the appropriate LED depending on which switch is pressed. What happens instead is that neither LED lights up. (I have however used other code and verified that the LED circuit works). My debug statements always return the following when either button is pressed:
Button 1: 1
Button 2: 1
Since the ISR only activates on LOW, I should think that one of the buttons would have to be LOW, get neither are.
Here is my simplified code:
volatile const int button1Pin = 41; // the number of the pushbutton pin
volatile const int button2Pin = 40; // the number of the pushbutton pin
const int led1Pin = 6; // the number of the LED pin
const int led2Pin = 5; // the number of the LED pin
// Statemachine states
const int noButtonPressed = 0; // Default state
const int buttonPressedStartDebouncing = 1;
const int buttonPressedDebouncingOngoing = 2;
const int buttonPressedDebouncingDone = 3;
const int buttonPressedFading = 4;
volatile int state = noButtonPressed;// statemachine state
volatile int reading; // number of the switch that was pressed
long debounceStartTime = 0; // the last time the output pin was toggled
long debounceDelay = 15; // the debounce time; increase if the output flickers
unsigned long currentTime = 0;// will store current system clock time
void setup() {
pinMode(2, INPUT_PULLUP); // Set INT 0 for input with pullup
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
Serial.begin (115200);
// enable ISR
attachInterrupt(0, buttonPress, LOW);
}
// ISR triggered in case of button press
void buttonPress() {
state = buttonPressedStartDebouncing;
}
void loop() {
switch (state) {
case (buttonPressedStartDebouncing):
{
debounceStartTime = millis();
state = buttonPressedDebouncingOngoing;
break;
}
case (buttonPressedDebouncingOngoing):
{
currentTime = millis();
if ((currentTime - debounceStartTime) > debounceDelay) {
reading = digitalRead(button1Pin);
Serial.print ("Button 1: ");
Serial.println (reading);
reading = digitalRead(button2Pin);
Serial.print ("Button 2: ");
Serial.println (reading);
if (digitalRead(button1Pin) == LOW)
{
digitalWrite(led1Pin, HIGH);
delay(30);
digitalWrite(led1Pin, LOW);
delay(30);
}
else if (digitalRead(button2Pin) == LOW)
{
digitalWrite(led2Pin, HIGH);
delay(30);
digitalWrite(led2Pin, LOW);
delay(30);
}
state = buttonPressedDebouncingDone;
}
break;
}
default:
{
break;
}
}
}
Below is a simplified version of the circuit showing just two switches and two LEDs:
Any help would be greatly appreciated! Thank you very much for your time.
BTW, please ignore my use of delay() - it is just a quick and dirty debug - the final project fades the LEDs on/off, but that code works so I removed it from this post.