Interrupt on this blinky led sketch


hi!
i found sketch on web lately. driving 6 leds pwm pins. im using arduino pro mini quest, , @ start point of sketch working great. problem skech want add funcktion, pause sketch everytime button or switch pressed, main loop go "idle" 5 seconds , pull led outputs high. playing game, if game buttons not pressed while game go demo mode light show.

and if button have not been pressed in time, loop continue going. sketch cycle routine long , "pause" if commands sketch @ start. arduino detects button way late,and @ start of sketch cycle. have tried use interrupt on sketch no effect. there original code below. dont understand structure of code found, @ start of void loop()...

you have correctly identified problem being caused delay() function.  need using millis() timing in blinkwithoutdelay example.

save time event happens, each time through loop() check whether required period has elapsed since event.  if not go round loop() again reading inputs etc.  if period has elapsed take required action. 

your program has number of states during each of different leds lit program lends being written state machine.  when in particular state turn on leds state , start timing using millis().  when period has elapsed change next state, save start time , set led pattern.  states best handled using switch/case , state variable code current state executed each time through loop().  because loop() continuously executed can read inputs , use them change state well, stopping or changing led sequence @ time.


Arduino Forum > Using Arduino > Programming Questions > Interrupt on this blinky led sketch


arduino

Comments