Go Down

Topic: How to cancel Millis timer once started (Read 748 times) previous topic - next topic

slvrsky

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.

UKHeliBob

It sounds like what you need to do is to change the timing period without changing the start time
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

sidhabo

Check out UKHeliBob's sticky at top of this subject ;)
I've been  Using Arduino UNO and Mega2560 so far ....

slvrsky

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.

Robin2

#4
Oct 09, 2018, 08:06 pm Last Edit: Oct 09, 2018, 08:08 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

slvrsky

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

slvrsky

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

slvrsky

sorry, here it is

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

@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
Two or three hours spent thinking and reading documentation solves most programming problems.

slvrsky

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

slvrsky

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.

Code: [Select]
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

Robin2

I think this piece of code
Code: [Select]
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
Code: [Select]
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
Code: [Select]
if (digitalRead(12)== HIGH){
      intervalMillis = 30000;

it seems a very strange way of doing things.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

slvrsky

Thanks for the response. I will try that.

Go Up