#include void setup() { // segments = anodes = +ve terminals -> 0v => reverse biased pinMode( 0, OUTPUT); digitalWrite( 0, LOW); // A pinMode( 1, OUTPUT); digitalWrite( 1, LOW); // B pinMode( 2, OUTPUT); digitalWrite( 2, LOW); // C pinMode( 3, OUTPUT); digitalWrite( 3, LOW); // D pinMode( 4, OUTPUT); digitalWrite( 4, LOW); // E pinMode( 5, OUTPUT); digitalWrite( 5, LOW); // F pinMode( 6, OUTPUT); digitalWrite( 6, LOW); // G pinMode( 7, OUTPUT); digitalWrite( 7, LOW); // H pinMode( 8, OUTPUT); digitalWrite( 8, LOW); // I pinMode( 9, OUTPUT); digitalWrite( 9, LOW); // J // colours = cathodes = -ve terminal -> 2v => reverse biased pinMode(10, OUTPUT); digitalWrite(10, HIGH); // red LEDs pinMode(11, OUTPUT); digitalWrite(11, HIGH); // blue LEDs pinMode(12, OUTPUT); digitalWrite(12, HIGH); // green LEDs // timer Timer1.initialize(1000); // run interrupt handler every 1 millisecond Timer1.attachInterrupt(displayValue); // name of timer interrupt handler } void setLED(int segment, int bgr) { if (0 == bgr) digitalWrite(segment, LOW); // segment off else digitalWrite(segment, HIGH); // segment on if (0b001 & bgr) digitalWrite(10, LOW); else digitalWrite(10, HIGH); // red if (0b010 & bgr) digitalWrite(12, LOW); else digitalWrite(12, HIGH); // blue if (0b100 & bgr) digitalWrite(11, LOW); else digitalWrite(11, HIGH); // green } volatile int value = 0; // value to be displayed ('volatile' because it is used in both loop() and in the interrupt handler) int segment = 0; // segment currently illuminated by displayValue int ledColours[10] = { 2, 2, 2, 2, 2, 2, 3, 3, 3, 1 }; // as the name says ;-) void displayValue(void) { setLED(segment, 0b000); // turn off the active segment segment = segment + 1; // move to the next segment if (segment == 10) segment = 0; // 'wrap' around from 9 --> 0 at the end of the LED array if (value > segment) // this segment should be turned ON setLED(segment, ledColours[segment]); // turn on the active segment } void loop() { for (int i = 0; i <= 10; i = i + 1) { value = i; // update the value to display on the LEDs delay(200); } }