Hi folks, I've a basic question.
before writing this post I browse the forum but I didn't find something similar to my question.
I hope not to bore you with my basic problem.
I'm using the Arduino IDE for the first time but I can't understand clearly the behavior of the micro-controller.
I wrote the classical code for blinking a led :
//THIS CODE WORKS !!
void setup() {
pinMode(5,OUTPUT); }
void loop() {
digitalWrite(5,HIGH);
delay(1000);
digitalWrite(5,LOW);
delay(1000);
}
Then I decided to make the led flashing just five times - in the previous example the led flash forever.
Here how I modified the code:
//THIS CODE DOES NOT WORK !!
int counter = 0;
void setup() {
pinMode(5,OUTPUT); }
void loop() {
if(counter < 5)
{
digitalWrite(5,HIGH);
delay(1000);
digitalWrite(5,LOW);
delay(1000);
}
counter = counter +1;
}
PROBLEM : The led flash forever instead of flashing just five times, WHY ???
I made some experiments, I found a workaround but I do not understand the behavior of the Arduino environment. I print out on the serial port the value of the var counter, and actually i saw counter running but the led didn't stop after 5 counts.
here the workaround i tried:
//THIS CODE, with the workaround, WORKS !! - but.... WHY ???
int counter = 0;
void setup() {
pinMode(5,OUTPUT); }
void loop() {
if(counter < 5)
{
digitalWrite(5,HIGH);
delay(1000);
digitalWrite(5,LOW);
delay(1000);
}
counter = counter +1;
delay(15);
}
WHY adding delay(15) the code works ??
Thanks a lot for some explanation about the functioning of the Arduino code.