I've had a project running successfully for months on an Arduino Mega, and today I tried to swap the Mega for a Due to increase the speed. After making the change, the entire project is working successfully except for a strange problem with the pushbuttons.
I have 5 momentary pushbuttons that toggle different controls (they simply send a wireless signal to toggle different AC outlets). After moving the project to the Due, when 1 button is pressed, it ends up toggling 2 other buttons at the same time.
Originally I was using the internal pullups to detect when the buttons were low, but also I've tried connecting them to the Due's 3.3v to detect when they're high, and both times I experienced the exact same behavior.
Is there any reason why there would be this difference in pushbutton-reading behavior between the Mega and the Due? Could it be that the Due is more sensitive to noise? How could I fix this?
The wires connecting to the pushbuttons run about 8 feet. I never had a problem with this setup while running the sketch on the Mega.
Below is the relevant code that detects button presses:
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <RCSwitch.h>
#include <Servo.h>
#include <Event.h>
#include <Timer.h>
#include <dht.h>
#include "RTClib.h"
const byte nl1ButtonPin = 40, nl2ButtonPin = 42, nl3ButtonPin = 44, pumpButtonPin = 5, irButtonPin = 27,
unsigned long buttonPressTime1, buttonPressTime2, buttonPressTime3, buttonPressTime4, buttonPressTime5; // when the switch last changed state
byte oldButtonState1 = HIGH, oldButtonState2 = HIGH, oldButtonState3 = HIGH, oldButtonState4 = HIGH, oldButtonState5 = HIGH;
const unsigned long debounceTime = 100;
void setup() {
pinMode(nl1ButtonPin, INPUT_PULLUP); pinMode(nl2ButtonPin, INPUT_PULLUP); pinMode(nl3ButtonPin, INPUT_PULLUP); pinMode(pumpButtonPin, INPUT_PULLUP); pinMode(irButtonPin, INPUT_PULLUP);
}
void loop() {
byte buttonState1 = digitalRead(nl1ButtonPin);
if (buttonState1 != oldButtonState1)
{
if (millis () - buttonPressTime1 >= debounceTime)
{
buttonPressTime1 = millis (); // when we closed the switch
oldButtonState1 = buttonState1; // remember for next time
if (buttonState1 == LOW) {
Serial.println("button1 LOW!");
toggleLight1();
}
}
}
byte buttonState2 = digitalRead(nl2ButtonPin);
if (buttonState2 != oldButtonState2)
{
if (millis () - buttonPressTime2 >= debounceTime)
{
buttonPressTime2 = millis (); // when we closed the switch
oldButtonState2 = buttonState2; // remember for next time
if (buttonState2 == LOW) {
Serial.println("button2 LOW!");
toggleLight2();
}
}
}
byte buttonState3 = digitalRead(nl3ButtonPin);
if (buttonState3 != oldButtonState3)
{
if (millis () - buttonPressTime3 >= debounceTime)
{
buttonPressTime3 = millis (); // when we closed the switch
oldButtonState3 = buttonState3; // remember for next time
if (buttonState3 == LOW) {
Serial.println("button3 LOW!");
toggleLight3();
}
}
}
byte buttonState4 = digitalRead(pumpButtonPin);
if (buttonState4 != oldButtonState4)
{
if (millis () - buttonPressTime4 >= debounceTime)
{
buttonPressTime4 = millis (); // when we closed the switch
oldButtonState4 = buttonState4; // remember for next time
if (buttonState4 == LOW) {
Serial.println("button4 LOW!");
togglePump();
}
}
}
byte buttonState5 = digitalRead(irButtonPin);
if (buttonState5 != oldButtonState5)
{
if (millis () - buttonPressTime5 >= debounceTime)
{
buttonPressTime5 = millis (); // when we closed the switch
oldButtonState5 = buttonState5; // remember for next time
if (buttonState5 == LOW) {
Serial.println("button5 LOW!");
toggleHeater();
}
}
}
}
On the serial output, pressing one button shows 2 other buttons as low. So, for example, pressing button2 might show: "Button2 LOW!, Button4 LOW!, Button1 LOW!". There doesn't seem to be any clear patterns with which other 2 buttons get simultaneously pressed. Remember that this exact same code and hardware setup was working perfectly for months on the Mega, which makes me believe maybe it's something related to the Due (maybe 3.3v, maybe stronger PU resistors, etc).
debounceTime