Flashing an LED whilst in an interrupt function

Hi - I have searched for hours trying to come up with a way to do this. Basically I have a hardware interrupt that I wantbto trigger an ISR to flash an LED. I am making a brake light flasher for my motorcycle and I have mocked up the circuit. I have an LED that is dimmed using PWM that will act as a tail light, when the brake lever is depressed I want to trigger an interrupt to flash the rear light 3 times then remain on steady but very bright. When the lever is released it should drop out of the ISR and go back to the dimly lit LED.

I can get an ISR to work but obviously delay() doesn't work there in order to be able to flash the LED, is there at other way to achieve this ?

Cheers.

I don't know why you need an interrupt - a simple state machine would do the job .

How about using a "for" loop with no statement inside just to use up time?

you could set a global variable in the interrupt and check it's state in the main loop. That way you can do the flashing in the main loop, not the interrupt.

AWOL:
I don't know why you need an interrupt - a simple state machine would do the job .

I wanted to learn about interrupts, that's all. What is a 'simple state machine' ?

JimboZA:
How about using a "for" loop with no statement inside just to use up time?

I did think about this but it sounded a bit amateur !

iggykoopa:
you could set a global variable in the interrupt and check it's state in the main loop. That way you can do the flashing in the main loop, not the interrupt.

I might give this a go. Thanks

ilium007:

JimboZA:
How about using a "for" loop with no statement inside just to use up time?

I did think about this but it sounded a bit amateur !

If you mean making the ISR wait while you go through your flashing sequence, that's a very bad idea. The ISR should do as little as possible. If it needs to trigger some action that takes elapsed time to complete, just set a global variable to notify the main loop that the action needs to be taken.

In this case since all you really want is to trigger some code when an input changes state and the input only changes at a very slow frequency, there's no reason at all to use the interrupt. Since the use of the ISR makes switch debouncing trickier, you really would be better of just polling that input in the main loop.

Cheers - as I said earlier, the idea was to learn more about interrupts which I am doing. I am going to handle the debounce issue with a simple RC circuit involving a resistor, capacitor and inverting Schmidt trigger to get a nice square wave switch input.

ilium007:
Hi - I have searched for hours trying to come up with a way to do this. Basically I have a hardware interrupt that I wantbto trigger an ISR to flash an LED. I am making a brake light flasher for my motorcycle and I have mocked up the circuit. I have an LED that is dimmed using PWM that will act as a tail light, when the brake lever is depressed I want to trigger an interrupt to flash the rear light 3 times then remain on steady but very bright. When the lever is released it should drop out of the ISR and go back to the dimly lit LED.

I can get an ISR to work but obviously delay() doesn't work there in order to be able to flash the LED, is there at other way to achieve this ?

If you want to experiment with interrupt-driven programming, you could look at the AVR lib fade example.