Understanding the BlinkWithoutDelay Program

I am very new to programming and don't understand a lot of things yet. I am working on a project and I believe it will require the absense of delays since I want it to perform multiple tasks at the same time. However, I do not fully understand how the BlinkWithoutDelay example code works. Would someone please be willing to go through the program and explain in detail EXACTLY what each step is doing? Also what would happen if one of those steps was left out and how that would effect the program? I don't understand completely what long and unsigned long means, and what the difference between a const int and an int is, among other things in the program. I hope this isn't too much to ask for.

Thanks!

"unsigned long" vs. "long": An "unsigned long" cannot represent negative values. A "long" can.

"const int" vs. "int": A "variable" with the "const" qualifier cannot have its value changed at runtime.

If you miss out any of the steps it won't work as it is supposed to.

The blink without delay sketch has three key lines:

unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;

Think about how you could do a similar task given a clock, an egg-timer, a sheet of paper and a pencil/eraser.

"delay" method: Turn on the light. Turn over the egg-timer. Wait until all the sand has run through. Flick the switch. Repeat.

"Blink without delay method": Turn on the light. Write down the time now. label: Look at the clock. Is the time now minus the time you wrote down greater than the blink time? YES: Flick the switch, and note the new time. Go off, put the kettle on, read a page of a book, get the teabags out, whatever. Repeat from "label".

"delay" is an example of a "busy wait"; the processor is continually looking at the clock to see if the required wait time has expired, and can't do other things.

"blink without delay" allows you do do other things in between looking at the clock, but make sure that the other things don't take too long to execute.

Check out a new and improved blink-without-delay example program that shows setting up two independent timers. See if that doesn't help you kick the delay() habit. ;)

Reply #34 shows the current work in progress:

http://arduino.cc/forum/index.php/topic,74861.msg574440.html#msg574440

Lefty

Would someone please be willing to go through the program and explain in detail EXACTLY what each step is doing?

I suggest Googling for something like "C programming tutorial" and reading up on the first few chapters. That will explain data types, const, int etc.

Also what would happen if one of those steps was left out and how that would effect the program?

Try omitting a step and see what happens. Nothing will break. Maybe it'll stop blinking. Maybe the light will stay on. Maybe you'll get an error message. Have fun experimenting!

If you are familiar with the for( ; ; ) loop, you can do the same thing with it.

Lets say there is a counter that goes from 0 to 100. (or anything you like)

You can say when (counter%7==0) do something, which means when counter can be divided to 7 without any remainder. So that part will work when the counter is 0, 7, 14, 21 and so on.

If you need any other rate just write (counter%rateYouWant==0). Put your code in for( ; ; ) so it will work nonstop. Don't forget to reset the counter though.

It is relatively easy compared to timers. But it will have timing issues, as you may foresee, execution time for counter=1 and counter=2 may not be the same.

Hope this helps :)