- Wearable:Tech Projects with the Raspberry Pi Zero
- Jon Witts
- 255字
- 2021-07-02 19:46:27
Writing the Python program
We will make use of the Nano text editor to write our Python files. First, we will create a directory to save all of our programs into. Type the following two commands:
mkdir ~/WearableTech
cd ~/WearableTech
We should now be inside our new WearableTech directory. Type the following command to open Nano, and create a new file called ledflash.py:
nano ledflash.py
We are now in the Nano text editor. Type in the following code to create a simple program, which will make the LEDs flash on and off when you press the button, and then stop the LEDs flashing when you press the button again:
#!/usr/bin/python3
from gpiozero import LED, Button
btn = Button(25)
green = LED(23)
red = LED(14)
while True:
btn.wait_for_press()
green.blink(0.5,0.5)
red.blink(0.25,0.25)
btn.wait_for_press()
green.off()
red.off()
Save the file by pressing Ctrl and O together, followed by the Enter key, and then exit Nano by pressing Ctrl and X together. We are now going to make the file executable by typing the following command:
sudo chmod +x ./ledflash.py
We can now run the file by typing this:
./ledflash.py
All being well, you should now be able to press the button wired up to your Pi and LEDs should start blinking at different rates. Press the button again (you may need to hold it for half a second) and your LEDs should switch off! If it doesn't work, check your program and double check your wiring.
If you want to stop your program from running, press Ctrl and C together.
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程(第2版)
- Docker and Kubernetes for Java Developers
- PHP 從入門到項(xiàng)目實(shí)踐(超值版)
- 程序員面試筆試寶典
- FFmpeg入門詳解:音視頻原理及應(yīng)用
- Mastering Android Development with Kotlin
- C程序設(shè)計(jì)實(shí)踐教程
- The Professional ScrumMaster’s Handbook
- Unity 3D/2D移動(dòng)開發(fā)實(shí)戰(zhàn)教程
- Java程序員面試筆試寶典(第2版)
- TMS320LF240x芯片原理、設(shè)計(jì)及應(yīng)用
- Regression Analysis with Python
- Unity 2018 Augmented Reality Projects
- 網(wǎng)絡(luò)數(shù)據(jù)采集技術(shù):Java網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- App Inventor少兒趣味編程動(dòng)手做