From d0ba3975ebc4fb1ad7f415a6a499788ea27fd131 Mon Sep 17 00:00:00 2001 From: julien Lengrand-Lambert Date: Sun, 27 Sep 2015 16:10:04 +0200 Subject: [PATCH] Creates day and night cycles. Currently is 14 hours of daylight, followed by 10 hours of night. --- .../ledCycleInterrupt/ledCycleInterrupt.ino | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/arduino/ledCycleInterrupt/ledCycleInterrupt.ino b/arduino/ledCycleInterrupt/ledCycleInterrupt.ino index 05475dd..bccd7a7 100644 --- a/arduino/ledCycleInterrupt/ledCycleInterrupt.ino +++ b/arduino/ledCycleInterrupt/ledCycleInterrupt.ino @@ -1,11 +1,17 @@ -// Arduino timer CTC interrupt example -// // avr-libc library includes #include #include #define LEDPIN 8 -volatile byte seconds; +int daycycle = 14; // number of hours the day lasts (LEDs ON) +int nightcycle = 10; // number of hours the day lasts (LEDs OFF) + +long hourseconds = 3600; // number of seconds in an hour +unsigned long daycycleseconds = hourseconds * daycycle; +unsigned long nightcycleseconds = hourseconds * nightcycle; + +volatile boolean isday = true; +volatile unsigned long current = 0; void setup() { @@ -30,19 +36,38 @@ void setup() // enable global interrupts: sei(); + + // switches LEDs ON + digitalWrite(LEDPIN, HIGH); } void loop() { -// main program +// nothing for now } ISR(TIMER1_COMPA_vect) { - seconds++; - if(seconds == 10) - { - seconds = 0; - digitalWrite(LEDPIN, !digitalRead(LEDPIN)); + current++; + if(isday){ + if(current == daycycleseconds){ + apply(); + } + } + else{ + if(current == nightcycleseconds){ + apply(); + } } } + +void apply(){ + current = 0; + isday = !isday; // switching day/night + switchLeds(); +} + +void switchLeds(){ + digitalWrite(LEDPIN, !digitalRead(LEDPIN)); +} +