![]() As you can see, its counting to 10 same as before with the for loops. Open the serial monitor to see the result. ![]() You will be prompted to save the file, name it basicWhileLoops. we add a delay of 1 second using delay(1000).then we print the value of x using Serial.print(x).next we open the while loop and create the condition while(xthen inside the void setup() we start the Serial monitor using Serial.begin(9600).Open a fresh sketch and copy the code below. The void loop() will continue looping the code until you stop it. It reaches 10 and goes back to the start. Now let’s cut the for loop from the void setup() and paste it into the void loop() The Arduino is counting 1 to 10 in 1 second intervals and then stops counting. Open the serial monitor and see the result. You will be prompted to save the file, name it basicForLoops. The for loop continues looping until x=10, then stops.we create a delay of 1 second using delay(1000).we print the value of x with Serial.print(x).then we add +1 everytime through the loop with x++.we create the condition if xwe start the serial monitor with Serial.begin(9600).Starting with a simple for loop copy the code below to your Arduino IDE. We won’t be covering the do-while loop in this guide. The do-while loop works in the same way as the while loop except the do while loop executes the code before the loop begins this way the code is always executed at least once. The while loop, loops an unknown number of times until a condition is met. The for loop, loops a specified known number of times. In coding languages, we have the for loop, while loop, and do-while loop. It saves time writing the same code repeatedly. We use loops to execute instructions multiple times until a condition is met or they can wait to execute the instruction until a condition is met.
0 Comments
Leave a Reply. |