S
S
Shapck2015-05-14 15:20:32
Arduino
Shapck, 2015-05-14 15:20:32

After the reset, the first LED does not light up, why?

Hello! 8 LEDs are connected, each lights up after 1 second. When you press the button, they are extinguished and light up again, all except the first one, why?
the code:

int led[] = {3,4,5,6,7,8,9,10};
int c=0;

void on(int pin){
digitalWrite(pin, HIGH);
}
void off(int pin){
digitalWrite(pin, LOW);
}
void setup(){
pinMode(2, INPUT);
attachInterrupt(0, button, RISING);
for(c=0; c<sizeof(led); c++){
pinMode(led[c], OUTPUT);
}
  
}
void loop(){
for(c=0; c<sizeof(led); c++){
on(led[c]);
delay(1000);
}
  
}
void button(){
for(c=0; c<sizeof(led); c++){
off(led[c]);
}
c=0; 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DuMOHsmol, 2015-05-14
@Shapck

void loop(){
for(c=0; c<sizeof(led); c++){
on(led[c]);
delay(1000);
}

It's in this piece of code. When an interrupt is called, the main loop stops and then continues from where it left off. With a very high probability, the cycle will be interrupted at the moment delay(1000), respectively, the variable "c" will become zero in this particular section. And then, after the delay is completed, the increase by one will immediately work and it turns out that the first LED will not be lit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question