Looping with WHILE In Arduino

WHILE command is a command for looping based on a condition, so the amount of repetition can not be determined with certainty. In WHILE checking if there are conditions such as IF commands for looping. The general form of the command WHILE namely:

while (condition) {
  // Code execution

  }

If the conditions are appropriate, the order or the source code contained within the curly braces "{}" will be executed. To understand more about WHILE command, let us modify the previous article Sketch with the addition WHILE and several other changes.
    // Free Tutorial Arduino
    // www.ioisalman.com
    

    // 8 pin for LED
    const int pinLED = 8;

    void setup() {
        // Led pin as an output
       pinMode(pinLED, OUTPUT);
   }

   // Start the time delay 1000 | 1 Second
   int timeDelay = 1000;

   void loop() {

     // during Time Delay Value>0
     // Execution block this program
     while(timeDelay > 0){
       //Led Life and death with a duration of 500 milisecond
       digitalWrite(pinLED, HIGH);
       delay(500);
       digitalWrite(pinLED, LOW);
       delay(500);

       // Subtract Time Delay With 100 

     }


timeDelay = timeDelay - 100; 
  }
 
// After Delay time is reduced continuously
// It will eventually be worth minus or <0
// Then while the above will stop
// For value timeDelay <1000
// This program block execution

while(timeDelay < 1000){
// LED life and death with a duration of 100 milliseconds 
digitalWrite(pinLED, HIGH);
delay(100);
digitalWrite(pinLED, LOW);
delay(100);

// Add timeDelay with 100 
timeDelay = timeDelay + 100;
}
 

Program on the Sketch on top will blink an LED with different durations. Initially the LED will flash with a duration of ½ seconds 10 times, then the LED will flash more quickly with a duration of 1/10 seconds 10 times.

Subscribe to receive free email updates:

0 Response to "Looping with WHILE In Arduino"

Post a Comment