0

i am using Arduino Mega.. ANd this is the code:

void bump(void);
void setup() 
{
 pinMode(19,INPUT);
 attachInterrupt(4,bump,HIGH);
 pinMode(13,OUTPUT);
}
void loop() 
{
}
void bump()
{
 for(float i = 0; i < 90000; i++){
 digitalWrite(13,HIGH);
 }
 for(float i = 0; i < 10000; i++){
 digitalWrite(13,LOW);
 }
} 

Although i give +5 V to it once, the LED will blink twice...! What is the solution?

asked Apr 24, 2015 at 7:02
3
  • Why have you chosen float as the data type of your counter, 'i'? At least, is 'float' used elsewhere in your program? If not, this use drags in a lot of code, just for counting. And why are you re-writing your port i/o on each count? Wouldn't: digitalWrite(13,HIGH); delay(ONTIME); digitalWrite(13,LOW); delay(OFF_TIME); do what you intend? Commented Apr 24, 2015 at 20:03
  • @JRobert: I wanted a huge value for i so i chose float.. Mayb long would have been better.. And we can't use delay inside ISR.. Commented Apr 25, 2015 at 6:02
  • ISRs should never do more then needed. You can block other functions of the Arduino. Very seldom do I do anything more then set or clear a flag. In some variations of the Arduino family long times will cause it to crash. Commented Sep 24, 2020 at 21:15

1 Answer 1

1

After wondering over it for quite a long time, I finally came to the conclusion, that the interrupt should be made to trigger at RISING edge, if I want it to go into ISR the moment it gets HIGH.

Hence the setup line was change to

attachInterrupt(4,bump,RISING);

And the cod e works well.
I suppose that is because, when it receives a HIGH signal, than the period for which it receives HIGH is a bit long, during which it adds another interrupt into the queue, and so the ISR is executed once again.

answered Apr 24, 2015 at 7:02
1
  • Remember to mark your answer as solution in three days ;) Commented Apr 24, 2015 at 7:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.