SAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD

Arduino For Loops Explained

Well, this the best ever explanation about "For Loop" I have ever found. In this post I will see 3 kind of sketch and how they work. So let's start with the first one.
Ad

Sketch 1
int led = 4;

void setup() {
pinMode (led,OUTPUT);
}

void loop() {

digitalWrite (led, HIGH);
delay(500);

digitalWrite (led, LOW);
delay(500);

digitalWrite (led, HIGH);
delay(500);

digitalWrite (led, LOW);
delay(500);

digitalWrite (led, HIGH);
delay(500);

digitalWrite (led, LOW);
delay(500);

digitalWrite (led, HIGH);
delay(500);

digitalWrite (led, LOW);
delay(500);

digitalWrite (led, HIGH);
delay(500);

digitalWrite (led, LOW);
delay(500);

delay(3000);
}

The above sketch seems too long and you might think it probably does a very complicated task. Well, it although it seems like that but it only turns a led on and off 5 times then wait for 3 seconds and it starts the task all over again. You can do the same task with less code by using a "for" statement.

Sketch 2
int led = 4;
int i;

void setup() {
pinMode (led,OUTPUT);
}

void loop() {

for (i=0; i<5; i++){
  digitalWrite (led, HIGH);
delay(500);
  digitalWrite (led, LOW);
delay(500);
}
delay(3000);
}

Now you can see that sketch 2 looks very nice and tidy and does exact same task as sketch 1, this is the miracle of the for statement. Let me explain about it a bit.

for (i=0; i<5; i++){
digitalWrite (led, HIGH);
delay(500);
digitalWrite (led, LOW);
delay(500);
} 

Three thing are going on in the brackets after the for ().

i=0; "i" is a reference point of something that you want it to start from. For example: If you write i=3 that means you want the code between the two yellow curly brackets to run from number three. So if your code is (i=3; i<10; i++) so it will run 7 times only and not 10 times.
i<5; It means that if "i" (which is equal to 0 in the sketch) is less than 5 then run the code (what ever code you put in between the yellow brackets) until it reaches equal to 5.
i++  Well, this bit of code adds value of 1 each time the code between yellow brackets has been run. For example: if the code has been run once it will add one and if it has run for 4 times it will add 4+1=5 so after adding the last 1 it will stop because the "i" is equal to 5 now and 5 is not less than 5 so the "For Statement" will stop there and ignore what ever code is between the yellow brackets.
Ad

Note:
Remember after reaching the maximum number it will only ignore that particular for statement and it will continue to run other codes.


One last sketch I want to finish this post with.
Sketch 3
int led = 4;
int i;

void setup() {

pinMode (led,OUTPUT);

for (i=0; i<10; i++){
  digitalWrite (led, HIGH);
delay(500);
  digitalWrite (led, LOW);
delay(500);
 }
}

void loop() {
 
}

The sketch 3 is very helpful when you trying to run a code for certain amount of times then turn it off until you click the reset button and turn it on again.

That's all for this post I hope you've fund it helpful. :)

Ad


No comments:

Post a Comment



Newly posted:
Reason why rich getting richer and poor getting poorer