Offline
Newbie
Karma: 0
Posts: 32
|
 |
« on: July 22, 2012, 02:52:30 am » |
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
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #2 on: July 23, 2012, 11:16:52 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: July 23, 2012, 11:48:14 am » |
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
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #4 on: July 23, 2012, 01:50:12 pm » |
Yes done all of that using delay. Now I want to use millis() Here is a sample of the sketch I have been using #// 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
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: July 23, 2012, 02:27:58 pm » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #6 on: July 23, 2012, 03:32:03 pm » |
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
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: July 23, 2012, 03:41:00 pm » |
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.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #8 on: July 23, 2012, 04:11:38 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: July 23, 2012, 04:14:30 pm » |
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/blinkYou 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
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: July 23, 2012, 04:16:36 pm » |
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
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #11 on: July 23, 2012, 04:30:24 pm » |
Thanks. Ok got your point. Still thinking of how delay was working.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2884
I only know some basic electricity....
|
 |
« Reply #12 on: July 23, 2012, 04:54:49 pm » |
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 at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #13 on: July 23, 2012, 05:12:21 pm » |
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
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: July 23, 2012, 05:18:07 pm » |
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
|
|
|
|
|
|