2

I wrote this code and uploaded it to attiny85:

 int hours = 2;
 int minutes = 4;
 static int MINUTES_COUNT_BIT = 5;
 static int HOURS_COUNT_BIT = 3;
 #define button 2
void setup() {
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(button,INPUT);
 attachInterrupt(0,pin_ISR,CHANGE);
}
void loop() {
 // put your main code here, to run repeatedly:
}
void pin_ISR(){
 if(digitalRead(button)==LOW){
 for(int i = HOURS_COUNT_BIT;i>=0;i--){
 byte act_bit = bitRead(hours,i);
 switch(act_bit){
 case 0:
 digitalWrite(3,HIGH);
 delay(1000);
 digitalWrite(3,LOW);
 delay(1000);
 break;
 case 1:
 digitalWrite(4,HIGH);
 delay(1000);
 digitalWrite(4,LOW);
 delay(1000);
 break;
 }
 }
 }
 }

When I click button it lights up LEDs but it is so much faster than 500 milliseconds it looks like that: https://drive.google.com/open?id=0ByeWvrEgy2gRNUp3MDNxQTAxd2c Please help

asked Sep 25, 2016 at 10:18
2
  • Maybe you did not Burn Bootloader with the same MHz setting as you uploaded the Sketch. Commented Sep 25, 2016 at 11:23
  • Delay in an ISR? Commented Sep 25, 2016 at 12:00

1 Answer 1

2

In short:

  • Arduino delay() is relying on millis().
  • The millis() function is updated by timer ISR.
  • ISR handler blocks all other ISRs by disabling global interrupts.
  • No update for millis results into deadlock in delay if you use it in another ISR

And recommendation for ISRs? Do it as short as possible! Set some volatile flag and do the action later outside of ISR.

answered Sep 25, 2016 at 12:07

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.