Pages: [1] 2 3 4   Go Down
Author Topic: Time functions  (Read 3101 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have written a sketch using digitalWrite and delay to control a sequence of events on my Arduino. I have been using it successfully for several months.The problem is that if I alter the value of the delay then all following events are then moved forwards or backwards.
I have been looking at millis, but don't quite understand how to incorporate this into my sketch. Could someone please point me in the right direction.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1010
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The millis returns the number of milliseconds.
http://arduino.cc/en/Reference/millis
The millis() could rollover, there is example code how to prevent problems with that.

Suppose the current time is: unsigned long startmillis = millis();
If you need to do something after 5 minutes, that time is: unsigned long somethingmillis = startmillis + (5L * 60L * 1000L);
In the loop() you could have a delay of 1 seconds, to test every second if millis() already reached the time that something has to be done.

links:
http://arduino.cc/playground/Main/LibraryList#Timing
http://arduino.cc/playground/Code/DelaySeconds
http://arduino.cc/playground/Code/MultiBlink
http://arduino.cc/playground/Code/Timer
« Last Edit: July 22, 2012, 05:03:00 am by Krodal » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a simple code which switches valves on and off. However I now want to use millis() so that I can have both events overlapping or at the same time. I have looked at the references that I was thankfully given , but I am still baffled. These references are to a flashing led on pin 13 worked by a switch. I don't see the connection. With digitalWrite it is either High or low
The abreviated sketch that I am currently using and understand is as follows

* sketch_april4_valve_timings.ino (1.02 KB - downloaded 10 times.)
Logged

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

Quote
but I am still baffled.
We'll give you a watch (millis()), a pad of paper (some variables in the Arduino sketch), and a set of switches (the pins on the Arduino). We'll give you the job of turning the two relays on and off at the proper time - turn one on; turn it off 55 minutes later; turn the other one on 85 minutes later; turn the second one off 55 minutes later; repeat 6 hours later. How would YOU (not the Arduino) do it?

To start, you'd start by turning a switch on, and writing down what you did and when you did it. Periodically (every pass through loop()), you'd see if it was time to turn that switch off.

You make your decision about whether it is time to do something based on two things - when something was last done, and what was last done.

Hopefully, this will help you get started.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes done all of that using delay. Now I want to use millis()

Here is a sample of the sketch I have been using
Code:
#// here I want to enter the values of a,b,c,f, so that they will be entered automatically below

  unsigned int a = 55;        // size of first drop in ms
  unsigned int b = 85;        // time between 1st and 2nd drops
  unsigned int c = 55;        // size of second drop in ms
  
  unsigned int f = 6000;      // 5 sec before restarting

void setup() {                
  // initialize the digital pins as an output.

  pinMode(6, OUTPUT);  
  pinMode(7, OUTPUT);
  
  
    
}

void loop() {
  digitalWrite(6, HIGH);    // Turns ON Relay 3  on digital pin 6 (blue)
  delay(a);                 // size of drop in ms best size for trajectory
  digitalWrite(6, LOW);     // Turns OFF Relay 3 on digital pin 6
  delay(b);                 // time between 1st and 2nd drops
  digitalWrite(7, HIGH);    // Turns ON Relay 2 on digital pin 7 (yellow)
  delay(c);                 // size of drop in ms
  digitalWrite(7, LOW);     // Turns OFF Relay 2 on digital pin 7
 
 

  delay(f);                 // Delay before recycling

}

Moderator edit: Code tags added.
#
« Last Edit: July 23, 2012, 02:23:58 pm by AWOL » Logged

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

Code:
unsigned int f = 6000; // 5 sec before restarting
Can you explain your constant there please.

The thing about delay is it doesn't allow you to do other things at the same time.
If you follow the method described in reply #3, you'd be able to do things like

Put kettle on
Check clock
Get cup out
Check clock
Get tea out
Check clock
Put tea in teapot
Check clock.

And so on.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, it should be 6 seconds, before the whole cycle starts again. I am trying to understand millis(). It appears that the cycle goes round and round checking to see if it is time to do something. I am trying to write the sketch at the moment with my limited knowledge
Logged

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

There's an example in the IDE called "blink without delay"
Once you've stripped out the comments, you're left with about three or four crucial lines of C.
Try to work a couple of cycles through, using a sheet of paper and a pencil.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. I will incorporate it with the other information I have gathered. I am just trying to find ou how to reset the counter to zero once all of the actions have taken place
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes done all of that using delay. Now I want to use millis()

delay() is blocking. millis() is not blocking.

PaulS was trying to help you.

http://www.gammon.com.au/blink

You do not, repeat not, have to "reset the counter to zero once all of the actions have taken place".

Look, if you time an egg by looking at the clock on the wall, and then when seeing when 5 minutes are up (by comparing the time now to the time 5 minutes ago) do you then reach up and reset the clock to 12 am? No, you don't.
Logged

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

Quote
Thanks. I will incorporate it with the other information I have gathered. I am just trying to find ou how to reset the counter to zero once all of the actions have taken place
Don't even think about it. If someone invites you to dinner, and says meet me at 6:00, you don't feel the urge to reset your watch, do you? After dinner, you don't feel the urge to reset your watch, do you?

Suppose that person said "Let's do this again tomorrow!". You would have no trouble figuring out when to meet again, would you? Or, how many hours/minutes/seconds/milliseconds away that was, would you?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. Ok got your point. Still thinking of how delay was working.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4019
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Delay is like leaving the room for some time then coming back in to pick up and carry on.

if ( millis() - startTime >= whenThisTaskIsDone ) // is "checking the clock"

You can check the clock on task after task inside loop() and every time loop() runs it will.

Remember, at 16 MHz Arduino can usually run through unblocked loops in microseconds.

Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still studying all of the examples. One thing I would like to know. Why is everything dealing with blinking LEDs, when I only want the event to happen once then stop
Logged

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

Quote
Why is everything dealing with blinking LEDs, when I only want the event to happen once then stop
What is it that you want to happen once and then stop? Turning the relay on? Or turning it back off at a later time? How does that differ from blinking an LED?

The whole point of blink without delay is to show you how to do something (it really doesn't matter what) at some specific time without the need to use delay.

But, if you are welded to the idea of using delay(), then go ahead and use it.
Logged

Pages: [1] 2 3 4   Go Up
Jump to: