How to cancel Millis timer once started

Hello, I am trying to get my timer to work properly. I have tried searching a way to stop a Millis timer once started but have found nothing that works. The timer I am building will control 1 relay output. Each timer has its own output for an LED status light. The issue I am having is when I pick 60 mins and after a little bit and want to change it to another time such as 90 mins, it will shut the relay off after the 1st time I picked. The timer works correctly if you let it time out before picking another time. Is there something I am missing? I also set all the times for 1 min currently for testing. The code is attached as a text file due to the size of the coding.

Thanks for any help given.

timer.txt (8.82 KB)

It sounds like what you need to do is to change the timing period without changing the start time

Check out UKHeliBob's sticky at top of this subject :wink:

Will it change the time interval once the timer has started? I played around with that but I couldn't get it to change until the timer completed.

You seem to have a few variables like interval120 each of which is for a different time.

Perhaps you should give those variables a more general name (or maybe you really only need one of them) and just change the value when you want to change the interval.

What I have done a few times is to have the specific timing variables (like interval120) but not use them directly. Instead I use them to put the appropriate value in a working variable. Then it causes no confusion of the value of the working variable is changed.

...R

I think I know what you're getting at. I will see if I can get it figured out. Thank you

I have tried using the interval as X and have each button change that value. It still does the same thing. It acts as if the request needs to be finished by the previous interval before it will change it.

slvrsky:
I have tried using the interval as X

You have to post the latest version of your program if you want us to help with it.

...R

sorry, here it is

timer.txt (8.42 KB)

Judging by the libraries used this code is running on an ESP8266 but there are references in the code to digital pins 12, 13 and 14. What board is the code running on ?

@slvrsky - please change the variable name 'x' to something longer and preferably something meaningful (maybe intervalMillis). It is very difficult to find single-character variables in a program.

And that variable should be declared global so that it retains its value - just like the variable previousMillis

And please attach your file as a .ino file to save us having to rename it.

...R

I am using a nodemcu esp8266. Robin2, I will work on that today.

Here is the .ino file. Just a rundown on how I am getting this issue. On the webserver page for the esp8266, if you click a time such as 10 min, wait a couple seconds then click 30 min you should get a serial readout like I attached. It will use the time set for the 10 min button and put output 5 low, and never completes the timer for 30 mins. The only way I have gotten around this is when I want to change the timer, I click the OFF/RESET button then the new time.

5 LOW
Relay on 10 trigger request
GPIO 5 on 10 via output 12
HIGH for 10
5 LOW
Relay on 30 trigger request
GPIO 5 on 30 via output 13
HIGH for 30
10 min has passed5 LOW

timer.ino (8.33 KB)

I think this piece of code

if ((unsigned long)(currentMillis - previousMillis) >= intervalMillis) { 
      Serial.print ("10 min has passed");
      digitalWrite(5, LOW);
      digitalWrite(12, LOW);
      delay(10);
      Serial.print("5 LOW");
    previousMillis = currentMillis;
    }

should not be inside another IF. It should be directly in loop(). That means you only need one copy of it and you should change the message to something more general like

Serial.print(intervalMilllis);
Serial.print(" millisecs have passed");

And you could probably write all the relevant pins LOW

Separately. if the only purpose of writing (say) pin 12 HIGH is to enable it to be tested in

if (digitalRead(12)== HIGH){
      intervalMillis = 30000;

it seems a very strange way of doing things.

...R

Thanks for the response. I will try that.

Each output pin will be connected to an external LED to indicate which time set is on, that’s why I did it that way instead of using boolean as a type of toggle for it. Also adding that IF statement right into the loop triggered the output timer looping and not changing the time.

All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low
time interval has passed5 LOW
All outputs low

timer.ino (7.37 KB)

I did try adding a larger number to the global area of the code, I changed unsigned long intervalMillis = 999999999; and it didn't do the loop as before. It seemed to be working but it still does the same issue as the last way I had it with changing the output to low using the 1st timer picked and not the last.

slvrsky:
I did try adding a larger number to the global area of the code, I changed unsigned long intervalMillis = 999999999; and it didn’t do the loop as before. It seemed to be working but it still does the same issue as the last way I had it with changing the output to low using the 1st timer picked and not the last.

That does not make any sense unless you post the version of the program that illustrates it - the complete program.

…R

sorry, uploaded

timer.ino (7.4 KB)

Now that I can see your program I have looked again carefully at your Reply #16 but I’m afraid it means nothing to me.

You need to explain this in a lot more detail

it still does the same issue as the last way I had it with changing the output to low using the 1st timer picked and not the last.

Remember that I don’t have your project in my head they way you have so you need to explain every little thing. Also, I read a lot of Threads so I don’t retain in my mind things that you wrote in a Reply yesterday.

…R