Hi,
I'm trying to do something pretty simple, but having a little trouble finding the correct way to code it.
I'm a bit of a beginner, so bare with me.
I have two outputs (motors) that I switch between, using a momentary button.
I'd like to have the following feature added if possible:
If the momentary switch has not been pressed (to toggle between outputs) in over 10 minuets, have both outputs turn off. (hence "sleep" mode)
When the button is finally pressed, it will "wake up" and resume normal operation.
Here's what I have so far, It works fine except I don't know how to do proposed "sleep" portion in code.
Any help would be greatly appreciated. Thanks.
// constants won't change. They're used here to set pin numbers:
const int button = 2; // the input pin number of the pushbutton
const int motorApin = 12; // the pin number for motor "A"
const int motorBpin = 13; // the pin number for motor "B"
// Variables will change:
int MotorState1 = LOW; // the current state of the motor output pin
int MotorState2 = HIGH; // the current state of the motor output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(button, INPUT);
pinMode(motorApin, OUTPUT);
pinMode(motorBpin, OUTPUT);
// set initial LED state
digitalWrite(motorApin, MotorState1);
digitalWrite(motorBpin, MotorState1);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(button);
// 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) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
MotorState1 = !MotorState1;
MotorState2 = !MotorState2;
}
}
}
// set the LED:
digitalWrite(motorApin, MotorState2);
digitalWrite(motorBpin, MotorState1);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}