You might also be interested in Arduino Playground - HomePage
attachInterrupt(function, period)
Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.
Completely removes the need for anything in the loop.
Give it a go:
#include <TimerOne.h>
void setup() {
DDRB |= _BV (5); //pinMode(13, OUTPUT);
Timer1.initialize();
Timer1.attachInterrupt(blink, 500000);
}
void loop() {
}
void blink() {
PINB |= _BV (5); // digitalWrite(13, !digitalRead(13));
}
Also has the advantage of operating completely independently of "the loop", leaving it free for other things.
Not saying it's a better way, but another different way that might serve a different purpose.
Incidentally, I used this opportunity to "big up" that nice Mr Nick Gammon's post listing the direct port numbers. I have it pinned on my wall for permanent reference!
Simply replacing:
pinMode(13, OUTPUT);
with
DDRB |= _BV (5);
and
digitalWrite(13, !digitalRead(13));
with
PINB |= _BV (5);
takes it from 1,478 to 904 bytes saving 574 bytes. Reasonably significant. (Your example is 980 bytes, so actually smaller code without "fiddling"!).