SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
How it works?
In the previous lessons, we have successfully made the LED blink. In other words, it is very simple for us to write
a code that makes the traffic light cycle color. What we need to do is to add a judgment on the state of the button.
But if we directly write the statement that reads the button value into the main program, we will find that it doesn’t fit
anywhere. Even if it is written in, it is difficult for us to read this value. This is because the program is stuck when
executing
utime.sleep()
, and the statement to read the button value is not executed at this time.
Of course, we can read the button value through the IRQ in the previous lesson. But this time we take another
approach-multithreading.
Multi-threading can be simply understood as dividing a thing into multiple parts, which are executed by different
people (or processors). Just like when the chef is frying the steak, the assistant chef makes the sauce so that the newly
prepared sauce can be poured on the properly prepared steak to make the best cooking.
Look at these lines:
import
machine
import
utime
import
_thread
led_red
=
machine
.
Pin(
15
, machine
.
Pin
.
OUT)
led_yellow
=
machine
.
Pin(
14
, machine
.
Pin
.
OUT)
led_green
=
machine
.
Pin(
13
, machine
.
Pin
.
OUT)
button
=
machine
.
Pin(
16
, machine
.
Pin
.
IN)
global
button_status
button_status
=
0
def
button_thread
():
global
button_status
while
True
:
if
button
.
value()
==
1
:
button_status
=
1
_thread
.
start_new_thread(button_thread, ())
while
True
:
if
button_status
==
1
:
led_red
.
value(
1
)
utime
.
sleep(
10
)
global
button_status
button_status
=
0
led_red
.
value(
1
)
utime
.
sleep(
5
)
led_red
.
value(
0
)
led_yellow
.
value(
1
)
utime
.
sleep(
2
)
led_yellow
.
value(
0
)
led_green
.
value(
1
)
utime
.
sleep(
5
)
led_green
.
value(
0
)
led_yellow
.
value(
1
)
utime
.
sleep(
2
)
led_yellow
.
value(
0
)
66
Chapter 3. For MicroPython User
Содержание Thales Kit
Страница 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Страница 2: ......
Страница 4: ...ii ...
Страница 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Страница 10: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 6 Chapter 1 Introduction to Raspberry Pi Pico ...
Страница 12: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 8 Chapter 2 What is Included in This Kit ...
Страница 13: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 1 Components List 2 1 Components List 9 ...
Страница 42: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 38 Chapter 2 What is Included in This Kit ...
Страница 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Страница 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...