I can use an interrupt in arduino mega 2560 r3 as attachInterrupt, but after interrupt began i couldn't use any kind of delay function in sub-program. Actually i have find a link in forum about this problem but i couldn't get it all. Link is given below.
It's not a problem and there is no way to delay inside an interrupt. You're better off explaining why you think you need a delay in an interrupt including providing any code that you have currently.
You should describe what you want to achieve - trying to delay in an interrupt handler is inadvisable, and can't be
done with delay(). For short delays (up to a few 100us only) you can call delayMicroseconds().
Generally though you should try and do the minimum work in the interrupt handler and poll for the
heavy-lifting in the loop() routine.
The delay() function uses the milliseconds interrupt. Inside an interrupt handler, all other interrupts are suspended so the millis() counter doesn't count and delay() never ends.
delayMicroseconds() will work in the interrupt routine because it just sits in a busy loop.
If you spend more than about two milliseconds total in your interrupt routine (including calls to delayMicroseconds) your millisecond timer (millis()) will start losing time.
If you think you need to do a delay() in an interrupt routine you have designed your code incorrectly. Two alternatives:
Note the time (interruptTime = millis() and have loop() do the rest after the appropriate delay (if (millis()-interrutpTime > delayNeeded))
Set up a timer interrupt to signal a different interrupt when the timer expires.
Arrch:
You're better off explaining why you think you need a delay in an interrupt including providing any code that you have currently.
I'm trying to do a Active Traffic Lights Control. There is a crossroad. There are 43(red+yellow+green)=12 lights for cars and 42(red+green)=8 for pedestrian so toltally 20 output. There are 4 buttons for pedestrian, so 4 inputs at all.
When the "cargreen_1" is HIGH for a moment after that changes to LOW, cargreen_2 will be HIGH, but if a a person comes and wants to accross to road so he will push to "button". I want my program that. When interrupt by buttons accur run a new program in interrupt sub-program just like below:
->sub-program: (as if button_1 is pushed when main program is runing).
delay for 5sec for cargreen_1(@HIGH)
cargrren_1 LOW
pedred_1(LOW)
pedgreen_1(HIGH)
delay for 10sec for pedred_1
pedred_1(LOW)
->sub-program finished. This codes are not true-working-codes for Arduino, i just try to tell my problem.
Use the interrupt from the push button to set a flag variable, recording that the button has been pushed. Then in loop(), have the program examine the flag at appropriate points. If it finds that it is set, it can clear the flag and call the subprogram.
The root of your problem is that you appear to be dependent on delay() in many forms. Real traffic lights do not use delay(). You shouldn't either. Instead, you need to read, understand, and embrace the blink without delay example, and read up on state machines.
A traffic light is a state machine. There are states, like "east green". There are transitions, like "east green" to "east yellow". There are events, like "north wanted" or "north pedestrian wanted", caused when a vehicle or pedestrian nears/arrives at the intersection.
You need to manage the transitions from one state to another, based on events and time passage. Do NOT use delay() for that time passage. You will miss events.
You are by no means the first person to try to emulate a traffic control device; research how others have done it, and try to improve on that, not re-invent, in a misunderstood way.
Your approach will work, with the understanding that it has its own set of problems.
There are many ways to delay inside of an isr. The oldest of which would be to waste time in a loop: you can adjust the length of the loop or to layer them to control the amount of time wasted / delayed.
Considering the speed of a well-written loop() and the relatively glacial time requirements of an active traffic light package, why use interrupts at all??? The debounce of the pedestrian buttons alone more than dwarfs everything else by 100's of times at least.
Without interrupts you could watch 40 buttons and switch those lights so fast they'd all look like are on at the same time, just not as bright.
Compare that to how long it takes to press and release a button then look up to start to see when the light changes. Even longer is the delay before you change the light just to let drivers in traffic have a fair reaction time.
Try adding the times each action and response should take to your plans, you don't need interrupts.
Just like what Paul posted above, get into Blink Without Delay and learn about state machines.
dhenry:
Your approach will work, with the understanding that it has its own set of problems.
There are many ways to delay inside of an isr. The oldest of which would be to waste time in a loop: you can adjust the length of the loop or to layer them to control the amount of time wasted / delayed.
Suggesting that someone can delay for 5 seconds inside an ISR and that "will work" is just nonsense. Sure, you can delay for 5 seconds inside an ISR. You can delay for 3 months. But what if another interrupt comes along, what then, eh?