// Delay-Free Timing 02 // // In this case, we will set up a bunch of independent timers // Set variables // Set up three timers, each with a different offset to check against. long timer1 = 0; // "long" variables are a special kind of number, which // takes up 4 bytes of space to store (where "int" only takes // up 2 bytes). Range of numbers are from // -2,147,483,648 to 2,147,483,647. If we left it just an // "int" we would likely run out of numbers after only a // couple of minutes. "long" can last for days of continuous // running. int timer1offset = 1000; long timer2 = 0; int timer2offset = 900; long timer3 = 0; int timer3offset = 800; long timer4 = 0; int timer4offset = 700; long timer5 = 0; int timer5offset = 600; long timer6 = 0; int timer6offset = 500; // Setup pins int ledPin1 = 11; int ledPin2 = 10; int ledPin3 = 9; int ledPin4 = 8; int ledPin5 = 7; int ledPin6 = 6; void setup() { // The standard stuff... Serial.begin(9600); // Setup the serial communications so we can debug // Save some vertical space by combining lines pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT); pinMode(ledPin6, OUTPUT); timer1 = millis(); timer2 = millis(); timer3 = millis(); timer4 = millis(); timer5 = millis(); timer6 = millis(); // Start with all pins on digitalWrite(ledPin1,HIGH); digitalWrite(ledPin2,HIGH); digitalWrite(ledPin3,HIGH); digitalWrite(ledPin4,HIGH); digitalWrite(ledPin5,HIGH); digitalWrite(ledPin6,HIGH); } // Das Loop... void loop() { if((timer1 + timer1offset) < millis() ) { //Timer 1 doLED1(); timer1 = millis(); } if((timer2 + timer2offset) < millis() ) { //Timer 2 doLED2(); timer2 = millis(); } if((timer3 + timer3offset) < millis() ) { //Timer 3 doLED3(); timer3 = millis(); } if((timer4 + timer4offset) < millis() ) { //Timer 4 doLED4(); timer4 = millis(); } if((timer5 + timer5offset) < millis() ) { //Timer 5 doLED5(); timer5 = millis(); } if((timer6 + timer6offset) < millis() ) { //Timer 6 doLED6(); timer6 = millis(); } } // The doLED functions void doLED1() { Serial.print("LED 1: "); Serial.println(!digitalRead(ledPin1)); digitalWrite(ledPin1,!digitalRead(ledPin1)); } void doLED2() { Serial.print("LED 2: "); Serial.println(!digitalRead(ledPin2)); digitalWrite(ledPin2,!digitalRead(ledPin2)); } void doLED3() { Serial.print("LED 3: "); Serial.println(!digitalRead(ledPin3)); digitalWrite(ledPin3,!digitalRead(ledPin3)); } void doLED4() { Serial.print("LED 4: "); Serial.println(!digitalRead(ledPin4)); digitalWrite(ledPin4,!digitalRead(ledPin4)); } void doLED5() { Serial.print("LED 5: "); Serial.println(!digitalRead(ledPin5)); digitalWrite(ledPin5,!digitalRead(ledPin5)); } void doLED6() { Serial.print("LED 6: "); Serial.println(!digitalRead(ledPin6)); digitalWrite(ledPin6,!digitalRead(ledPin6)); }