Raspberry Realize Blink with While loop
In this project, we will use the while-loop to achieve the effect of a continuously flashing LED.
Write a Program
Using the while loop, we can easily make the program repeat indefinitely. We only need to change the “for i in range(10)" in the program to “while True":
1 while True
2 LED.value(1)
3 utime. sleep (1)
4 LED.value(0)
5 utime. sleep (1)
The “while True" is a use of while-loop statement. Unlike a general while-loop that only executes the loop when a certain condition is met, this “while True" is an indefinite loop statement, which means that the program will be executed repeatedly and continuously until it is artificially terminated.
The complete code is as follows:
1 import machine
2 import utime
3 LED = machine. Pin(16, machine. Pin.OUT)
4
5 while True:
6 LED.value(1)
7 utime. sleep (1)
8 LED.value(0)
9 utime. sleep (1)
Click the “run" button again, and the LED connected to the Shield starts flashing. Unless the process is artificially stopped, the LED will continue to flash.