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– Thijs2016年09月25日 11:23:58 +00:00Commented Sep 25, 2016 at 11:23
-
Delay in an ISR?Mikael Patel– Mikael Patel2016年09月25日 12:00:21 +00:00Commented Sep 25, 2016 at 12:00
1 Answer 1
In short:
- Arduino
delay()
is relying onmillis()
. - The
millis()
function is updated by timerISR
. ISR
handler blocks all other ISRs by disabling global interrupts.- No update for
millis
results into deadlock indelay
if you use it in anotherISR
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