注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ここ]をクリックして今すぐ参加しましょう!
4.1.13 GAME - 10秒間¶
はじめに¶
次に、集中力を試すゲームデバイスを作ってみましょう。 傾きスイッチを棒に結び付けて、魔法の杖を作ります。杖を振ると、 4桁のセグメント表示がカウントを開始し、もう一度振るとカウントが停止します。表示されたカウントを 10.00 で停止させることができれば、あなたの勝利です。友達とこのゲームをして、 誰が時間の魔法使いかを競い合うことができます。
必要な部品¶
このプロジェクトには、次のコンポーネントが必要です。
一式を購入するのが便利です、こちらがリンクです:
名前 |
このキットのアイテム |
リンク |
---|---|---|
Raphael Kit |
337 |
以下のリンクから別々に購入することもできます。
コンポーネントの紹介 |
購入リンク |
---|---|
- |
|
- |
回路図¶
T-Board Name |
physical |
wiringPi |
BCM |
GPIO17 |
Pin 11 |
0 |
17 |
GPIO27 |
Pin 13 |
2 |
27 |
GPIO22 |
Pin 15 |
3 |
22 |
SPIMOSI |
Pin 19 |
12 |
10 |
GPIO18 |
Pin 12 |
1 |
18 |
GPIO23 |
Pin 16 |
4 |
23 |
GPIO24 |
Pin 18 |
5 |
24 |
GPIO26 |
Pin 37 |
25 |
26 |
実験手順¶
ステップ1: 回路を組み立てる。
ステップ2: コードのフォルダに移動する。
cd ~/raphael-kit/python-pi5
ステップ3: 実行可能ファイルを実行する。
sudo python3 3.1.13_GAME_10Second_zero.py
杖を振ると、4桁のセグメント表示がカウントを開始し、もう一度振るとカウントが停止します。表示されたカウントを 10.00 で停止させることができれば、あなたの勝利です。もう一度振ると、ゲームの次のラウンドが始まります。
コード
注釈
下記のコードは 変更/リセット/コピー/実行/停止 が可能です。しかし、それを行う前に、 raphael-kit/python-pi5
のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を確認することができます。
#!/usr/bin/env python3
from gpiozero import OutputDevice, Button
import time
import threading
# Initialize the button connected to GPIO 26
sensorPin = Button(26)
# Define GPIO pins connected to the 74HC595 shift register
SDI = OutputDevice(24) # Serial Data Input
RCLK = OutputDevice(23) # Register Clock
SRCLK = OutputDevice(18) # Shift Register Clock
# Define GPIO pins for digit selection on the 7-segment display
placePin = [OutputDevice(pin) for pin in (10, 22, 27, 17)]
# Define segment codes for numbers 0 to 9 on the 7-segment display
number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
# Counter and timer variables
counter = 0
timer1 = None
gameState = 0
def clearDisplay():
""" Clear all segments on the 7-segment display. """
for _ in range(8):
SDI.on()
SRCLK.on()
SRCLK.off()
RCLK.on()
RCLK.off()
def hc595_shift(data):
""" Shift data to the 74HC595 shift register to display a digit. """
for i in range(8):
SDI.value = 0x80 & (data << i)
SRCLK.on()
SRCLK.off()
RCLK.on()
RCLK.off()
def pickDigit(digit):
""" Select which digit to display on the 7-segment display. """
for pin in placePin:
pin.off()
placePin[digit].on()
def display():
""" Display the current counter value on the 7-segment display. """
global counter
clearDisplay()
pickDigit(0)
hc595_shift(number[counter % 10])
clearDisplay()
pickDigit(1)
hc595_shift(number[counter % 100 // 10])
clearDisplay()
pickDigit(2)
hc595_shift(number[counter % 1000 // 100] - 0x80)
clearDisplay()
pickDigit(3)
hc595_shift(number[counter % 10000 // 1000])
def stateChange():
""" Handle state changes for the counter based on button presses. """
global gameState, counter, timer1
if gameState == 0:
counter = 0
time.sleep(1)
timer()
elif gameState == 1 and timer1 is not None:
timer1.cancel()
time.sleep(1)
gameState = (gameState + 1) % 2
def loop():
""" Main loop to check for button presses and update the display. """
global counter
currentState = 0
lastState = 0
while True:
display()
currentState = sensorPin.value
if (currentState == 0) and (lastState == 1):
stateChange()
lastState = currentState
def timer():
""" Timer function that increments the counter every 0.01 second. """
global counter, timer1
timer1 = threading.Timer(0.01, timer)
timer1.start()
counter += 1
try:
loop()
except KeyboardInterrupt:
if timer1:
timer1.cancel()
コードの説明
必要なモジュールをインポートしてスクリプトを開始します。
gpiozero
ライブラリはボタンなどのGPIOデバイスとのインターフェースに使用され、time
およびthreading
モジュールはタイミング関連のタスクや並行操作を扱うために使用されるかもしれません。#!/usr/bin/env python3 from gpiozero import OutputDevice, Button import time import threading
GPIOピン26に接続された
Button
オブジェクトを初期化します。このセットアップにより、ボタン押下を検出することができます。# Initialize the button connected to GPIO 26 sensorPin = Button(26)
シフトレジスタのシリアルデータ入力(SDI)、レジスタクロック入力(RCLK)、シフトレジスタクロック入力(SRCLK)に接続されたGPIOピンを初期化します。
# Define GPIO pins connected to the 74HC595 shift register SDI = OutputDevice(24) # Serial Data Input RCLK = OutputDevice(23) # Register Clock SRCLK = OutputDevice(18) # Shift Register Clock
7セグメント表示の各桁の選択用のピンを初期化し、0〜9の数字を表示するためのバイナリコードを定義します。
# Define GPIO pins for digit selection on the 7-segment display placePin = [OutputDevice(pin) for pin in (10, 22, 27, 17)] # Define segment codes for numbers 0 to 9 on the 7-segment display number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
7セグメント表示を制御するための関数です。
clearDisplay
はすべてのセグメントをオフにし、hc595_shift
はシフトレジスタにデータをシフトし、pickDigit
は表示上の特定の桁をアクティブにします。def clearDisplay(): """ Clear all segments on the 7-segment display. """ for _ in range(8): SDI.on() SRCLK.on() SRCLK.off() RCLK.on() RCLK.off() def hc595_shift(data): """ Shift data to the 74HC595 shift register to display a digit. """ for i in range(8): SDI.value = 0x80 & (data << i) SRCLK.on() SRCLK.off() RCLK.on() RCLK.off() def pickDigit(digit): """ Select which digit to display on the 7-segment display. """ for pin in placePin: pin.off() placePin[digit].on()
現在のカウンター値を7セグメント表示に表示する関数です。
def display(): """ Display the current counter value on the 7-segment display. """ global counter clearDisplay() pickDigit(0) hc595_shift(number[counter % 10]) clearDisplay() pickDigit(1) hc595_shift(number[counter % 100 // 10]) clearDisplay() pickDigit(2) hc595_shift(number[counter % 1000 // 100] - 0x80) clearDisplay() pickDigit(3) hc595_shift(number[counter % 10000 // 1000])
ボタン押下に基づいてカウンターの状態を変更する関数です。
def stateChange(): """ Handle state changes for the counter based on button presses. """ global gameState, counter, timer1 if gameState == 0: counter = 0 time.sleep(1) timer() elif gameState == 1 and timer1 is not None: timer1.cancel() time.sleep(1) gameState = (gameState + 1) % 2
ボタンの状態を継続的にチェックし、表示を更新するメインループです。ボタンの状態が変わると、
stateChange
を呼び出します。def loop(): """ Main loop to check for button presses and update the display. """ global counter currentState = 0 lastState = 0 while True: display() currentState = sensorPin.value if (currentState == 0) and (lastState == 1): stateChange() lastState = currentState
0.01秒ごとにカウンターを増加させるタイマー関数です。
def timer(): """ Timer function that increments the counter every 0.01 second. """ global counter, timer1 timer1 = threading.Timer(0.01, timer) timer1.start() counter += 1
メインループを実行し、キーボード割り込み(Ctrl+C)を使用してプログラムからクリーンに終了することができます。
try: loop() except KeyboardInterrupt: if timer1: timer1.cancel()