Go Down

Topic: Time functions (Read 4 times) previous topic - next topic

Ramigrafx

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.

Krodal

#1
Jul 22, 2012, 10:36 am Last Edit: Jul 22, 2012, 12:03 pm by Krodal Reason: 1
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

Ramigrafx

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

PaulS

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.

Ramigrafx

#4
Jul 23, 2012, 08:50 pm Last Edit: Jul 23, 2012, 09:23 pm by AWOL Reason: 1
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: [Select]

#// 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.
#

Go Up