I finally have it working, despite one minor setback - the Micro I'm using doesn't have enough VCC to hold both relays on, unless I'm plugged into BOTH 12v on RAW and have the micro-usb plugged in. That's easy enough though.
The debounce code from earlier was the trick, but it didn't work as advertised. Here's the code from the example:
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
It didn't look right to me, and after adding some debug outs into the code and watching with the serial monitor, it turns out to be accurate.
In the line "lastDebounceTime = millis();" you set the debounce timer to current time. Then in the very next sentence, you're asking what Current Time - lastDebounceTime is. Well guess what? It's 0. Every time.
But if you put the comparison (millis - lastDebounceTime > delay) bit at the beginning, then execute your code, then set the lastDebounceTime with an 'else' statement, then it works very well.
Here's the complete working code, in case it helps someone else in the future:
/*
Modified to toggle relay on and off on alternating button pushes
Original Text Follows
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
* pushbutton attached to pin 5 from +5V
* 10K resistor attached to pin 5 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int driverButtonPin = 5; // the pin that the pushbutton is attached to
const int passengerButtonPin = 6;
const int driverRelayPin = 7; // the pin that the LED is attached to
const int passengerRelayPin = 8;
// Variables will change:
int driverButtonPushCounter = 0; // counter for the number of button presses
int passengerButtonPushCounter = 0;
int driverButtonState = 0; // current state of the button
int passengerButtonState = 0;
int driverLastButtonState = 0; // previous state of the button
int passengerLastButtonState = 0;
long driverDebounceTime = 0; //debounce timer
long passengerDebounceTime = 0;
long debounceDelay = 50; //increase if flickers
void setup() {
// initialize the button pin as a input:
pinMode(driverButtonPin, INPUT);
pinMode(passengerButtonPin, INPUT);
// initialize the LED as an output:
pinMode(driverRelayPin, OUTPUT);
pinMode(passengerRelayPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
int driverReading = digitalRead(driverButtonPin);
if (driverReading != driverLastButtonState) {
if ((millis() - driverDebounceTime) > debounceDelay) {
Serial.println("Step1");
driverButtonState = driverReading;
// compare the buttonState to its previous state
if (driverButtonState != driverLastButtonState) {
Serial.println("Step2");
// if the state has changed, increment the counter
if (driverButtonState == HIGH) {
Serial.println("Step3");
// if the current state is HIGH then the button
// wend from off to on:
driverButtonPushCounter++;
Serial.println("on");
Serial.print("driver: ");
Serial.println(driverButtonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
}
}
else {
driverDebounceTime = millis();
}
// read the pushbutton input pin:
int passengerReading = digitalRead(passengerButtonPin);
if (passengerReading != passengerLastButtonState) {
if ((millis() - passengerDebounceTime) > debounceDelay) {
Serial.println("Step1");
passengerButtonState = passengerReading;
// compare the buttonState to its previous state
if (passengerButtonState != passengerLastButtonState) {
Serial.println("Step2");
// if the state has changed, increment the counter
if (passengerButtonState == HIGH) {
Serial.println("Step3");
// if the current state is HIGH then the button
// wend from off to on:
passengerButtonPushCounter++;
Serial.println("on");
Serial.print("passenger: ");
Serial.println(passengerButtonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
}
}
else {
passengerDebounceTime = millis();
} // save the current state as the last state,
//for next time through the loop
driverLastButtonState = driverButtonState;
passengerLastButtonState = passengerButtonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (driverButtonPushCounter % 2 == 0) {
digitalWrite(driverRelayPin, HIGH);
} else {
digitalWrite(driverRelayPin, LOW);
}
if (passengerButtonPushCounter % 2 == 0) {
digitalWrite(passengerRelayPin, HIGH);
} else {
digitalWrite(passengerRelayPin, LOW);
}
}