Pages: [1] 2   Go Down
Author Topic: Blinking without delay  (Read 1984 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I am having  trouble with a timer. I need to energize a Mosfet for a time period (Interval * 1000) milliseconds. I have the interval stored in memory. I am looking at the "Blink without delay" example but I need something where I can push a button, it will count to a specified number while the controller is running in the background and when the timer is up switch off. The problem I had with the "Blink W/O delay" code is that it runs on an even count where   depending on a count of 1 thousand it is either on or off. my timer is activated by an if then statement.

I need to set millis() to zero and subtract current millis() from the (interval * 1000) and once it equals zero ta-dah! I do not know how to set millis() to zero however

Thank you

Bill
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The key thing to take away from the blink without delay is that it works on time differences; there is never any need to reset millis().
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem I had with the "Blink W/O delay" code is that it runs on an even count where depending on a count of 1 thousand it is either on or off. my timer is activated by an if then statement.
That's how you use millis(), it isn't a "problem."

I do not know how to set millis() to zero however
You don't.  Instead you track how much millis() has changed since the last time you check it.  That's how you use it.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is what I have so far.  "interval" integer from 1 to 12. If the time is right it needs to bring a certain pin high for a give amount of time. I am taking a snapshot of millis() giving it a number, adding it to the interval and comparing it to the current millis. It's not working. Any suggestions? Thanks!


Code:
if(((now.hour() == Breakfast) && (now.minute() == 0) && (now.second() == 0)) || ((now.hour() == Dinner) && (now.minute() == 0)  && (now.second() == 0))) { 
   
   unsigned long AutoMillis = millis();
 
   while(AutoMillis + (interval * OzMenu) < millis ) {
     if(automatic == true){ // Need t o add feedback
     digitalWrite(motorpin, HIGH);
     }
   }
     previousMillis = currentMillis;   
  //Feed the dog by rotating motor
 }


 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(AutoMillis + (interval * OzMenu) < millis ) {
millis is a function pointer, and hence a constant.
It doesn't make sense to compare it against an arithmetic expression.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's not working. Any suggestions?
Yes. Explain what "it's not working" means. What does that snippet do that you don't want? What does it not do that you do want?

Where are Breakfast and Dinner defined? Where are they valued? Where are previousMillis and currentMillis defined? How are they defined? Where are they valued?

When posting code, it's much better to post all of your code than to post the section where you think the error is.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Breakfast and dinner are integers. They are from 1 to 23 in terms of hours.

Basically the think never turns off. Even past it's time.  Hypothetically lets say that millis() was 10,000. My interval was 8, multiplied by 1000 that gives 8000. So when millis() = 10,000 the output pin is high I add 10,000 to 8,000 (The duration) and then try to compare it the absolute up to date value of millis(). When the absolute value of millis() does in fact equal to 18,000 the output pin goes low.

I cannot figure out how to capture the current value of millis() to compare it to the value at which it should shout off.

Thanks for the replies I really appreciate it smiley

Bill
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I cannot figure out how to capture the current value of millis() to compare it to the value at which it should shout off.
Try calling it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would I go about calling it and not resetting the previous bookmark I took for millis()?
I'm really confused.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to call it here:
Code:
while(AutoMillis + (interval * OzMenu) < millis ) {

thus
Code:
while(AutoMillis + (interval * OzMenu) < millis () ) {
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 4384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
to compare it to the value at which it should shout off.
You don't want to turn something off 'at' any value because if you happen to miss that value it will never turn off.  You want to turn off when 'equal to or greater' than the value (or perhaps 'equal to or less' than the value, depending on your logic).

Don
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the latest. The code compiles, the integers are all declared. If I comment out all the timing elements it works. I just cannot shut it off. however when I re-insert the code dealing with the timing aspect. Poof! it does not work.
Code:
else if((MiscMenu == 5) &&(buttonstate == LOW) && (TopMenuSelect == 0) ){
        if(automatic == false){
          unsigned long AutoMillis = millis();
          while(AutoMillis + (1 * OzMenu) < millis()) {
             if(automatic == false){ // Need t o add feedback
             digitalWrite(motorpin, HIGH);
             }//close actual feed
          }//close while
        }//close if automatic = true
        }//Close Else if statement
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posting code snippets is just wasting time.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Swannanoa, New Zealand
Offline Offline
Full Member
***
Karma: 1
Posts: 202
New To Arduino (and C)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

psppb

I think the responders are suggesting the following.
Use a long variable.
When you energise the Mosfet, make this variable = millis()
check that the variable is greater or equal (>=) to millis() + delay *1000
Either move on, or turn off the mosfet.

I have snipped some of mine that does basically the same, although in mine the heat gets turned off, and the pump runs for 5 secs.
I also set another variable as to what state the pump and heater are in.
You could also set your mosfet state (or read the pin) to know if you should be turning it off.

Code:

void HeatOff()
{
    // Heat is OFF without delay, BUT we set the ControlChangeTimer.
   
    digitalWrite(HeatOut, LOW);              // Turn Off the HeatOut pin
    HeatState = false;
    ControlChangeTime = millis();
    Serial.print(millis(),DEC);
    Serial.print("   Heat is Off\n");
}

void PumpOff()
{
  // Pump is not OFF until after the delay period, which allows water to circulate.

    if (millis() >= (ControlChangeTime + (PumpDelay *1000)))  // PumpDelay stored as secs
    { 
      digitalWrite(PumpOut, LOW);              // Turn Off the PumpOut pin
      PumpState = false;
      ControlChangeTime = millis();
      Serial.print(millis(),DEC);
      Serial.print("   Pump is Off\n");
    }
}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13018
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use an unsigned long variable.

Quote
check that the variable is greater or equal (>=) to millis() + delay *1000

Fails when millis wraps.  The trick to doing it correctly is to use subtraction.

Quote
void PumpOff()
{
  // Pump is not OFF until after the delay period, which allows water to circulate.

    if ( (ControlChangeTime - millis()) >= (PumpDelay *1000) )  // PumpDelay stored as secs
Logged

Pages: [1] 2   Go Up
Jump to: