
If ( (millis () - LED2_statechange_Timei) >= LED2_ON_interval) If ( (millis () - LED1_statechange_Timeii) >= LED1_OFF_interval) If the time since the last change in state from ON to OFF is equal or greater than the OFF interval If ( (millis () - LED1_statechange_Timei) >= LED1_ON_interval)
If the time since the last change in state from OFF to ON is equal or greater than the ON interval LED3_statechange_Timeii = millis () // Remember when LED3's state was changed from OFF to ON LED3 loop that turns LED OFF if it is ON LED3_statechange_Timei = millis () // Remember when LED3's state was changed from ON to OFF LED3 loop that turns LED ON if it is OFF LED2_statechange_Timeii = millis () // Remember when LED2's state was changed from OFF to ON LED2 loop that turns LED OFF if it is ON LED2_statechange_Timeii = millis () // Remember when LED2's state was changed from ON to OFF LED2 loop that turns LED ON if it is OFF LED1_statechange_Timei = millis () // Remember when LED1's state was changed from OFF to ON LED1 loop that turns LED OFF if it is ON LED1_statechange_Timei = millis () // Remember when LED1's state was changed from ON to OFF LED1 loop that turns LED ON if it is OFF Setting 3 digital pins as LED output pins and starting millisecond timer Declaring the variables holding the timer value, i.e. Assigning ON and OFF interval constants.Ĭonst unsigned long LED1_ON_interval = 3000 //Ĭonst unsigned long LED1_OFF_interval = 6000 Ĭonst unsigned long LED2_ON_interval = 500 //Ĭonst unsigned long LED2_OFF_interval = 1000 Ĭonst unsigned long LED3_ON_interval = 100 //Ĭonst unsigned long LED3_OFF_interval = 3000
ARDUINO AS TIMER FOR STROBE LIGHT CODE
I am very new to the Arduino and writing code of any sort, so that approach seems quite daunting to me. If not I might have to try a different approach like using the interupt function. I hope I'm slowly moving in the right direction. Unfortunately I've ended getting myself really confused and now my LEDs seem to sometimes flash the OFF interval, sometimes with the ON and sometimes with a combination of the both.
Adapted my loops to use both the OFF and ON intervals. Defined seperate ON and OFF intervals for each LED. I think I'm going in the right direction with the method I've chosen. I've been working on this tonight and made a few alterations to my code. with an interval of 333ms, so not everything will be divisble by 25. Unfortunately there will be times when I need to LED to flash 3 times a second i.e. Thank you very much for replying so quickly billabot. Then again, you could just call the iHandler directly without the external interrupt - but what fun would that be? To make the implementation easier(reliable) change (I am not sure if you want to count to 32 or 31.) Here is the guts of it: pin 9 is pulsed every 25 ms by your loop(), the iHandler() bumps a counter, the main loop() has a case statement that allows the required LED transistion actions to take place depending on the counter value only when old_counter is not equal to counter.Ġ <= counter <= 32 (because 800/25=32), reset counter to 0 after 32 interrupts have occurred. Observing that all your time factors are multiples of 25 ms, I would suggest that you use a digital pin on your UNO to initiate a self generated external interrupt.Īdding physical wire/resister+LED/cap. */Īny help would be greatly appreciated because I'm very new to this!ĪttachInterrupt ( 0, blink, CHANGE ) //0 is digtal pin 2 It will be called many thousand times per second because the above codeĭoes not wait for the LED blink interval to finish. * Other code that needs to execute goes here. If ( (millis () - LED3_timer) >= LED3_interval) If ( (millis () - LED2_timer) >= LED2_interval) If ( (millis () - LED1_timer) >= LED1_interval) LED1 loop that turns it ON if it is OFF and vice versa Setting 3 digital pins as output pins and resetting timer Declaring the variables holding the timer values for each LED. In summary: I need a new approach or an alteration to my code to be able to independently change the ON and OFF periods for each of my LEDs independently. So I need to somehow alter the OFF period independently. My problem is that at the moment my code causes LED1 to turn ON for 25 ms and off for 25 ms, LED2 turns ON for 50 ms and off for 50 ms etc. At the moment I'm using the millis() function. So far I have set up the hardware: 3 LEDs on digital pins 6, 7 and 8 using my Arduino UNO board and a breadboard.Ĭode-wise I understand that I can't use the "delay" function because it causes the whole system to delay i.e. I'm trying to write code to get 3 LEDs flashing independently, each with a different ON and OFF period.