Arduino:How to program an arduino using loop statements

Programming an Arduino(Level 2) using loop statements

 







1.Conditional statements(if/else)

These statements are executed when the condition which is specified in it becomes true
And these statement's are used when when we need to perform a specific task after the condition becomes true ,we can write this statement if the void loop field like this

if(condition)
{

(Task which need to be performed goes here)

}

else
{

(If the condition becomes false then this block of code will be executed)

}

Example of code


if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120){ digitalWrite(LEDpin, HIGH); } if (x > 120){ digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); }

2.While loop(while)

while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.

Syntax

while(condition){ // statement(s) }

Example of code

var = 0; while(var < 200){ // do something repetitive 200 times var++; }


3.Do while(do while)

The do…​while loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.

Syntax

do { // statement block } while (condition)

example of code:-


do { delay(50); // wait for sensors to stabilize x = readSensors(); // check the sensors } while (x < 100);

4.Switch case statements(switch)

Like if statements, switch case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.
The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions ("falling-through") until a break, or the end of the switch statement is reached.

Syntax

switch (var) { case label1: // statements break; case label2: // statements break; default: // statements }

Example code:-


switch (var) { case 1: //do something when var equals 1 break; case 2: //do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional break; }

5.Break(break)

break is used to exit from a forwhile or do…​while loop, bypassing the normal loop condition. It is also used to exit from a switch case statement.

Example of code:-


We have used for loop here,


for (x = 0; x < 255; x ++) { analogWrite(PWMpin, x); sens = analogRead(sensorPin); if (sens > threshold){ // bail out on sensor detect x = 0; break; } delay(50); }

6.For loop(for)

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.

Syntax

for (initialization; condition; increment) { //statement(s); }


Example of code:-


// Dim an LED using a PWM pin int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10 void setup() { // no setup needed } void loop() { for (int i=0; i <= 255; i++){ analogWrite(PWMpin, i); delay(10); } }


7. Continue

The continue statement skips the rest of the current iteration of a loop (forwhile, or do…​while). It continues by checking the conditional expression of the loop, and proceeding with any subsequent iterations.

Example of code:-


for (x = 0; x <= 255; x ++) { if (x > 40 && x < 120){ // create jump in values continue; } analogWrite(PWMpin, x); delay(50); }

8.Goto

It Basically Transfers program flow to a labeled point in the program
Syntaxgoto label; // sends program flow to the label

Example of code:-



for(byte r = 0; r < 255; r++)
{ for(byte g = 255; g > 0; g--)

{ for(byte b = 0; b < 255; b++)
{ if (analogRead(0) > 250){ goto bailout;}
// more statements ... } }}bailout:

8.Return

It Terminates a function and return a value from a function to the calling function, if desired.

Syntax

return value; // both forms are valid

Example of code:-


int checkSensor(){ if (analogRead(0) > 400) { return 1; } else{ return 0; } }

OR


void loop(){

// brilliant code idea to test here

return;

// the rest of a dysfunctional sketch here
// this code will never be executed
}

So That's all, hope you enjoyed and
learned to write code,
For any other info or query please write
in the comment box.

Comments

Post a Comment

Popular posts from this blog

Automatic Door Locking with Arduino

Arduino:How to make a RC car with arduino