How to stop a timer delay using millis() from executing when I click a button ?

Hello,
I made a sketch where I use millis() to start displaying "It is on" on the Serial monitor after a 3000 milliseconds. So what I would like to achieve is stop this from executing if a click a push button but I can not seem to find a solution for it. I need your help please.

here is my sketch with a debounced push button:

int relayButtonPin = 2;

boolean lastRelay5_pb = LOW;
boolean currentRelay5_pb = LOW;
boolean relay5PushBOn = false;
boolean relay5Working = false;


unsigned long interval = 3000;
unsigned long startClickTime;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(relayButtonPin, INPUT);
}

void loop() {

  // This is for the Relay 5 Push button connected to A2 ---------------------------------------
  currentRelay5_pb = debounce(lastRelay5_pb, relayButtonPin);
  if(lastRelay5_pb == LOW && currentRelay5_pb == HIGH){
    relay5PushBOn = !relay5PushBOn;
 
    startClickTime = millis();
    
  }
  lastRelay5_pb = currentRelay5_pb;

  
 
  if (millis() - startClickTime > interval)
  {

    Serial.println("It is on");
  }

}



// Debouncing function
boolean debounce(boolean last, int switchPin)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

Make the message display dependant on a boolean being false. Set the boolean to true when the message has been displayed

UKHeliBob:
Make the message display dependant on a boolean being false. Set the boolean to true when the message has been displayed

thanks for your reply UKHeliBob, well i couldnt exactly understand how you mean I should do it ?

This is what I had in mind

int relayButtonPin = 2;

boolean lastRelay5_pb = LOW;
boolean currentRelay5_pb = LOW;
boolean relay5PushBOn = false;
boolean relay5Working = false;
boolean okToPrint = true;

unsigned long interval = 3000;
unsigned long startClickTime;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(relayButtonPin, INPUT);
}

void loop()
{
  // This is for the Relay 5 Push button connected to A2 ---------------------------------------
  currentRelay5_pb = debounce(lastRelay5_pb, relayButtonPin);
  if (lastRelay5_pb == LOW && currentRelay5_pb == HIGH)
  {
    relay5PushBOn = !relay5PushBOn;
    startClickTime = millis();
  }
  lastRelay5_pb = currentRelay5_pb;
  if (millis() - startClickTime > interval)
  {
    if (okToPrint)
    {
      Serial.println("It is on");
      okToPrint = false;  //prevent further printing
    }
  }
}

// Debouncing function
boolean debounce(boolean last, int switchPin)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

If you want printing to start again for any reason then set okToPrint to true. I note that there are already several unused booleans in you sketch but I added one with a meaningful name to make it obvious what its purpose is

UKHeliBob:
This is what I had in mind

int relayButtonPin = 2;

boolean lastRelay5_pb = LOW;
boolean currentRelay5_pb = LOW;
boolean relay5PushBOn = false;
boolean relay5Working = false;
boolean okToPrint = true;

unsigned long interval = 3000;
unsigned long startClickTime;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(relayButtonPin, INPUT);
}

void loop()
{
  // This is for the Relay 5 Push button connected to A2 ---------------------------------------
  currentRelay5_pb = debounce(lastRelay5_pb, relayButtonPin);
  if (lastRelay5_pb == LOW && currentRelay5_pb == HIGH)
  {
    relay5PushBOn = !relay5PushBOn;
    startClickTime = millis();
  }
  lastRelay5_pb = currentRelay5_pb;
  if (millis() - startClickTime > interval)
  {
    if (okToPrint)
    {
      Serial.println("It is on");
      okToPrint = false;  //prevent further printing
    }
  }
}

// Debouncing function
boolean debounce(boolean last, int switchPin)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}



If you want printing to start again for any reason then set okToPrint to true. I note that there are already several unused booleans in you sketch but I added one with a meaningful name to make it obvious what its purpose is

ok i see, but about the false don't you think I should set it to false when the button is clicked ?

don't you think I should set it to false when the button is clicked ?

It is set to false when the message has been printed to stop it being printed again. If you want some other event to stop the printing then set it to false when that event occurs, but if you set it to false when the button becomes clicked then the message will never print because okToPrint will be false

UKHeliBob:
It is set to false when the message has been printed to stop it being printed again. If you want some other event to stop the printing then set it to false when that event occurs, but if you set it to false when the button becomes clicked then the message will never print because okToPrint will be false

thank you for the help, it works now :smiley: Karma++

void loop()
{
  // This is for the Relay 5 Push button connected to A2 ---------------------------------------
  currentRelay5_pb = debounce(lastRelay5_pb, relayButtonPin);
  if (lastRelay5_pb == LOW && currentRelay5_pb == HIGH)
  {
    relay5PushBOn = !relay5PushBOn;
    startClickTime = millis();
    okToPrint = !okToPrint;  //prevent further printing
  }
  lastRelay5_pb = currentRelay5_pb;
  if (millis() - startClickTime > interval)
  {
    if (okToPrint)
    {
      Serial.println("It is on");
      
    }
  }
}

it works now

That's good news. The technique is generally applicable if you want to control whether a section of code to execute or not

UKHeliBob:
That's good news. The technique is generally applicable if you want to control whether a section of code to execute or not

I had to start a new post because it has now nrf24L01 but the question is about the time delay

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.