LED and Relay to drive for time millis() then flash before timer runs out

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;
}


Hello
In short words:

  1. Button pressed and released -> LED On and Relais On
  2. Button pressed and released -> LED blinking and Relais On
  3. either after a time X the LED Off and Relais Off
  4. or Button pressed and released -> LED Off and Relais Off
  5. same player shoot again :slight_smile:

I gues the sketch needs three timers and little bit processing on a button object at least .

Yeah it was a little hard to put it in to words:

  1. Button pressed and released -> LED On and Relay On for time period (X secs)
  2. Timer reaches 5 secs before X finishes -> LED Flashes (indicating relay soon be to switched off)
  3. Button pressed and released -> Resets timer. LED returns solid and relay remains on. X starts again.
  4. time X expires -> the LED Off and Relays Off
  5. same player shoot again :slight_smile:

I may end up adding a second button to cancel all and revert back to initial state. One button to start timer/led/relay/reset timer and another button to turn off all if desired and start again

Finite state machine!

Ah this makes sense!

Will look in to this tomorrow and report any success

And take a look to OOP for using structs´s and array´s too.
That will reduce the coding work essential.

  if (millis() - startTime >= 10000UL) //when time has passed 10 secs cut off relay and LED
  {
    digitalWrite(relayPin, LOW);
    digitalWrite(LED_OUT, LOW);
  }
  else if (millis() - startTime >= 10000UL - 5000UL) //less than 5s to go, flash led
  {
    if (bitRead(millis(), 8))
      digitalWrite(LED_OUT, HIGH);
    else
      digitalWrite(LED_OUT, LOW);
  }

(Untested!)

Just uploaded and tested and was successful! YAY

I will look in to finite state coding tomorrow to see if that is a cleaner way to do it, however the addition of the code above has worked!

Many thanks!!

Finite state machines are very powerful and easy to use, but it takes a bit of getting used to. However, once you 'get' it, it's an incredibly useful tool to keep around in your box.

Hello
Yes, that´s true. Especcially when addtion I/O Pin´s with functions/methodes should be added to the functionality of the sketch.