#include <TimerOne.h> const byte LED = 13; void handleOtherStuff() { delay(100); //Serial.println(Timer1.read()); } unsigned int led = LOW; void timerISR() { digitalWrite(LED, led); led ^= (HIGH^LOW); } void setup() { //Start up the serial port Serial.begin(9600); while (!Serial); // for Leonardo Serial.println(F("Initialized example7")); // configure the pins pinMode(LED, OUTPUT); Timer1.initialize(); // breaks analogWrite() for digital pins 9 and 10 Timer1.attachInterrupt(timerISR, 500000); // attaches timerISR() as a timer overflow interrupt -- blinks at 1 Hz //Timer1.attachInterrupt(timerISR, 13); // attaches timerISR() as a timer overflow interrupt -- blinks at 38 kHz } void loop() { handleOtherStuff(); }
This example uses a timer overflow interrupt. You will have do download the TimerOne library and install it in your Arduino directory. See this page for instructions.