Jump to content
43oh

Help with delay timer for relay, energia code.


Recommended Posts

Hello

 

Please note that I am a beginner :)

 

I am working on a project that needs a relay to be turned on for a preset time. I have written the following code and it works but obviously no other code can run at the same time. I have been trying to use millis() instead but with no success. Can someone give me a hint?

 

The timer span will be 1 sec - 999 hours or 1 sec - 99 hours. I have not decided yet, will it be some problems with max to be 999 hours? The timer do not need to be very accurate.

 

It says the millis() will overflow after about 50 days. If I use millis() somehow will this couse any problems if the timer is enabled while the millis overflows after 50 days?

 

onTime is the time in milliseconds / 10 ( one hundred of a second) that the relay should be on

  if (relayState != lastRelayState && onTime > 0) {  // See if the state of the relay has changed and if the timer is enabled.
    
    if (relayState == LOW) {  // If relay is activated
      
        onTimeCounter = 10;  // Reset the counter, this is crucial. Reset to 10 to compensate for 1 second delay.
        while (relayState == LOW && onTimeCounter < onTime) {  // Loop until timer is reached or the relay is diactivated externally.
          onTimeCounter++;
          delay(10);  //10ms per cycle
          relayState = digitalRead(relayPin); // Read the state of the relay if it has been diactivated externally.
        }
        
        if (onTimeCounter >= (onTime)-1) {  // When timer is reached switch off the relay
          
          changeRelayState();  // Switch of relay
          
        }  // End of counter reached
        
     }  // End of relay state active
  }  // End of relay state changed
 
  lastRelayState = relayState;  // Reset relay state

Best regards

Andreas

Link to post
Share on other sites

Hello Andreas,

 

first of all, try to avoid delay() in genreal. Do not even start with using it, than you don't have to rewrite everything in a few weeks to something without delay. If you use millis(), and one of your programm cycles is allways shorter then 1ms, then the overflow after a few days is no problem at all. Becuse than you can work with something like:

  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis = interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
 }

But when your cycle could be longer than one ms, than you have a problem. I would use it like this:

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
}

If you need max 999h, you can reset your chip after this time, and you will not have an overflow. Thats just my ideas.

 

The blink without delay example you know?

 

http://energia.nu/Tutorial_BlinkWithoutDelay.html

 

Greetings,

 

Sven

Link to post
Share on other sites

But when your cycle could be longer than one ms, than you have a problem. I would use it like this:

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
}
If you need max 999h, you can reset your chip after this time, and you will not have an overflow. Thats just my ideas.

 

The blink without delay example you know?

 

http://energia.nu/Tutorial_BlinkWithoutDelay.html

 

Greetings,

 

Sven

 

 

As long as you use unsigned integers, the test in the example above will work even when the milliseconds wrap around. No need to reset if it runs longer than 50 days. This is due to how unsigned integer math works. But be sure to test exactly like that (current time - start time > interval)!

 

Example (for a 1 byte integer)

start = 250

current = 10

current - start = -240 = 16 in unsigned 8 bit integer

 

I'd never use the test for equal, even if you're sure that the period is lower than one millisecond. I you miss it, it will be a very long wait.

Link to post
Share on other sites

I'm  trying to find out where in my brain I had a knot to think that it will be a problem if you work with > sign. Now after your explanation I do not even find the point where I thought this can be a problem.

Link to post
Share on other sites

I'm  trying to find out where in my brain I had a knot to think that it will be a problem if you work with > sign. Now after your explanation I do not even find the point where I thought this can be a problem.

There can be problems when doing the math in different orders.

 

For example when you calculate the end time based on start time and interval, and then compare it against the current time, you will run into issues:

 

e.g.: if (current time > start time + interval)

 

start = 250

interval = 16

end = 250 + 16 = 266 = 10 -> current time will be larger until it wraps around, i.e. the condition triggers immediately

 

It can get even worse when end is close to the wrap-around point (e.g. 255) and current already shot past it. In this case current will be smaller than end for a VERY long time.

 

I think the trick is, to always compare difference of start and current with the interval instead of trying to calculate and compare the end time.

Link to post
Share on other sites
  • 3 weeks later...

Hello again

 

Sorry for the delay :)

 

I have used your examples and it is working, thanks!

 

I have a question tho; what is the accuracy of the internal crystal (G2553) or where can I find it in the datasheet?

 

Couse if I set the timer for 24 hours, it is roughly 5 min off (5 min to much at 2 times I tested it). Is this normal?

 

Best regards

Andreas

 

 

Skickat fr

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...