delay() in only one method

brian515:
I'm relatively new to Arduino, but sorry if this is a basic question.

Is it possible to get a delay function to only affect one method and not stop my whole program from executing? (ie if I want one method to run every few seconds, but I need the rest of the program to continually execute)

A while ago on the old forum (http://arduino.cc/forum/index.php/topic,48946.msg349959.html#msg349959) I tried to explain this sort of thing in English rather than code - maybe it will help here.

Imagine you yourself want to physically ring a bell every 30 seconds. There are two ways you could do it. In the first way you ring the bell, then set an alarm clock for 30 seconds time, go to sleep, wake up when the alarm goes off, ring the bell and repeat. You are not able to do anything in that 30 seconds waiting time except wait for the alarm to go off.

The second way you would ring the bell then look at your clock and note what time it is. Then you start getting on with doing other stuff, but look at the clock periodically to see if 30 seconds has gone by yet. If not, just carry on doing other stuff and glancing at the clock. If 30 seconds has gone by, ring the bell and make a note of the new time, then carry on as before.

The first method is equivalent to to using delay() in Arduino code, the second is equivalent to using millis() to note down when you do something and wait for a time interval to pass before you do it again.

millis() tells you how many milliseconds it's been since the Arduino was powered on or reset. So you can note down what millis() returns now, do some stuff, and when you look at millis() again subtract the first reading and it will tell you how much time has gone by.

Andrew