Hi
The project I am working on has hit a road block.
I am trying to have a Nano control a relay (via a push button) to engage USB power for a period of time. I have also included an LED for indication when the relay is energized.
I have included code that resets the timer if you push the button again.
What I want to do is have the LED flash for 5 seconds before cutting out the relay. Unless you push the button and reset the timer again and the LED remain solid.
I can get the push button to engage the relay (Digital Pin 9) for 10 seconds (just a test time, I would like 30 mins when finished) and the LED to come on. And have successfully integrated the time reset if the push button is depressed again.
have included a debounce code for the button
Just can not get the flash to work just before cut off.
Any help greatly appreciated.
const int buttonPin = 10; //button input
const int relayPin = 9; // relay output
const int LED_OUT = 12; // LED indicator output
int sensorState = LOW;
unsigned long startTime;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int oldState;
int buttonState;
int lastButtonState = LOW;
//
void setup()
{
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(LED_OUT, OUTPUT);
}
//
void loop()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
sensorState = ! sensorState;
}
}
}
sensorState = digitalRead(buttonPin); //if button high, turn on relay and LED
if (sensorState == HIGH)
{
if (oldState == LOW)
{
digitalWrite(relayPin, HIGH);
startTime = millis();
digitalWrite(LED_OUT, HIGH);
}
}
oldState = sensorState;
if (millis() - startTime >= 10000UL) //when time has passed 10 secs cut off relay and LED
{
digitalWrite(relayPin, LOW);
digitalWrite(LED_OUT, LOW);
}
else sensorState = LOW;
}