Here is my Sketch. I don't find any problem in it but it seems for loop is not working.
LED at pin 13 is continuously blinking. it is not stopping after 5 after it is program in for loop.
int ledPin=13;
int ontime=200;//2000ms on time for LED
int offtime=0500;//00ms off time for LED
int numBlink=5;//number of times LED to blink
void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);//set the pin mode to output type
}
void loop() {
// put your main code here, to run repeatedly:
int i;// count variable
for(i=0; i<numBlink; i++){//run the following for i=0 to i=4
digitalWrite(ledPin,HIGH);//set LED on
delay(ontime);//wait for 1 sec
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 1 sec
}
}
What is the case for for loop for not stopping at count.
It keeps on looping the part under loop (funny, right?)
They really should include a note like " // put your main code here, to run repeatedly:" to let people know.
If you ONLY want it to blink 5 times and then do nothing at all after, like you only want it "to run once:" hint hint:
int ledPin=13;
int ontime=200;//200ms on time for LED
int offtime=500;//500ms off time for LED
int numBlink=5;//number of times LED to blink
void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);//set the pin mode to output type
for(int i=0; i<numBlink; i++){//run the following for i=0 to i=4
digitalWrite(ledPin,HIGH);//set LED on
delay(ontime);//wait for 200ms
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 500ms
}
}
void loop() {
// put your main code here, to run repeatedly. Nothing is here because you don't want to keep doing anything.
}
int ledPin = 13;
int ontime = 200; //2000ms on time for LED
int offtime = 0500; //00ms off time for LED
int numBlink = 5; //number of times LED to blink
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); //set the pin mode to output type
}
void loop() {
// put your main code here, to run repeatedly:
int i;// count variable
for (i = 0; i < numBlink; i++) { //run the following for i=0 to i=4
digitalWrite(ledPin, HIGH); //set LED on
delay(ontime);//wait for 1 sec
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 1 sec
}
while(1){} // Go No further Stop here
}
if you only want this to run once you could also move it to the setup funciotn
int ledPin = 13;
int ontime = 200; //2000ms on time for LED
int offtime = 0500; //00ms off time for LED
int numBlink = 5; //number of times LED to blink
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); //set the pin mode to output type
int i;// count variable
for (i = 0; i < numBlink; i++) { //run the following for i=0 to i=4
digitalWrite(ledPin, HIGH); //set LED on
delay(ontime);//wait for 1 sec
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 1 sec
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Your problem is that you don't (seem to) understand that loop() is called repeatedly; no offense intended. So anything that you have placed in loop will be executed time after time.
You should, for what you want to achieve, basically forget that for- and while-loops exist and let loop() do the work.
void loop()
{
static byte flashcounter = 0;
// check if we have done 5 flashes
if(flashcounter < 5)
{
digitalWrite(ledPin, HIGH); //set LED on
delay(ontime);//wait for 1 sec
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 1 sec
// increment the counter
flashcounter++;
}
}
I have replaced your variable 'i' by one called 'flashcounter'; just find it a bit clearer as it exactly reflects what it is used for.
The static keyword causes the value in the variable to be remembered between calls to loop().
If it really needs to run only once, use INTP's approach. The above approach has the advantage that it can easily be adjusted to react on a button press and flash 5 times again.
e.g.
void loop()
{
static byte flashcounter = 0;
// check if we have done 5 flashes
if(flashcounter < 5)
{
digitalWrite(ledPin, HIGH); //set LED on
delay(ontime);//wait for 1 sec
digitalWrite(ledPin, LOW);//set LED off
delay(offtime);//wait for 1 sec
// increment the flash counter
flashcounter++;
}
// reset the flash counter on button press (button between pin and ground)
if(digitalRead(yourButton) == LOW)
{
flashcounter = 0;
}
}
You probably want to polish the last part a bit; e.g. only reset if flashcounter has reached 5.
For now, there is no need for debouncing as you're using two delays of one second. Once you change to the blink-without-delay approach, you will need to debounce the switch.