Go Down

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

slvrsky

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.

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

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.

Robin2

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

slvrsky


Robin2

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

slvrsky

Okay let me explain more. When I access the webserver and click the button attached to void handleswitch10, it makes 12 HIGH which also changes the interval to the 10 min setting. Now if lets say 2 minutes in, I'd like to change it to the 30 min setting. I click the 30 min button on the webserver attached to void handleswitch30, it makes 13 HIGH which also changes the interval to 30 min setting. Now this is where it is having an issue. After the setting attached to the 10 min setting times out it will turn 5 LOW. It should be waiting until the 30 min times out but it is not. It will always use the 1st time picked and never use the 2nd time picked. The only way I have found to get to work correctly is to press the OFF/RESET button attached to void handleswitchoff which puts all outputs low and I had a return function added to the end of that part of the script. I am not sure if I have things switched around for the timer incorrectly or its a limitation of the NODEMCU esp8266.

Robin2

That makes things a great deal clearer - thanks.

I think the problem is that in your (say) handleSwitch30() function you set Pin 13 HIGH but pin 12 is also HIGH and it is attended to first in loop() and the code for pin 12 sets pin 13 LOW.

You should be setting all the irrelevant pins LOW in the handleSwitch functions. And then you don't need any of the code in loop() like this
Code: [Select]
  if (digitalRead(13) == HIGH) {
    intervalMillis = 60000;
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    digitalWrite(2, LOW);
    digitalWrite(12, LOW);
  }

except the line intervalMillis = 6000; so you might as well put that in handleSwitch also

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

slvrsky

I had in a previous revision when the selected pin such as pin 13 was High, it makes all the other pins LOW but that didn't work either.

Robin2

I had in a previous revision when the selected pin such as pin 13 was High, it makes all the other pins LOW but that didn't work either.
Please post that version.

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

slvrsky

Here is that revision.

Robin2

Here is that revision.
First, it will be much easier to keep track of things if you post each revision with a slightly different name - I have called the version in Reply #24 timerD.ino

In that version why have you got code to set the pins LOW in loop() ?

Take those unnecessary lines out and try the program again and then report in detail what happens when you run it - using the style of explanation in Reply #20.

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

slvrsky

Okay, it is mostly working now. I switched the order in which the outputs turn on and off. I changed it so the old output goes off before the newly selected one comes on. That fixed the timer issue. Now the finial question I have. When the device first starts up on a reboot and I select any timer button it is like the timer doesn't start and it completes the rest of the script instantly. Then I click the timer button again and it will start the timer. It is only on the first button click after a reboot. Not sure what would cause that.

Attached is the fixed revision for the timer to work.

This is the serial output of the first button press after boot.
Code: [Select]
IP address: 192.168.137.193
HTTP server started



5 LOW
Relay on 10 trigger request
GPIO 5 on 10 via output 12
HIGH for 10
time interval has passed
5 LOW

Robin2

I thought I had posted another Reply asking you what the value of previousMillis is when the line
Code: [Select]
  if ((unsigned long)(currentMillis - previousMillis) >= intervalMillis) {
is first encountered. But I must be mixing you up with another Thread. Anyway, I reckon if you figure that out you will understand the problem.

I notice that you still have all the redundant code in loop() making things LOW that are already LOW. Clear that up NOW so you don't get in another tangle later.

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

slvrsky

Thanks for all your help. The programming is working fine now.

One weird thing I encountered, at my friends house (which is who this device is for) when the esp8266 is plugged into the wall using a new Samsung usb charger (1A 5v) the relay connected will bounce on and off for a long period of time when triggered. At my house and at work the device works properly like it should. I'm not sure if my friends house has "dirty" electricity or some interference causing the device to output erratic or what? It does this on all her outlets.

sidhabo

Not sure about your esp8266 but for Uno or Mega the external power should be 6V or more. I had problem with external 5V when relay was activated. The Uno  5V regulator was providing only 3.45 V ...
I've been  Using Arduino UNO and Mega2560 so far ....

Go Up