1

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

  • Maybe you did not Burn Bootloader with the same MHz setting as you uploaded the Sketch. – Thijs Sep 25 '16 at 11:23
  • Delay in an ISR? – Mikael Patel Sep 25 '16 at 12:00
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.