.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _4.1.18_py:
4.1.18 GAME - 10秒間
==============================
はじめに
-------------------
次に、あなたの集中力に挑戦するゲームデバイスを作成してみましょう。
チルトスイッチをスティックに結びつけて、魔法の杖を作ります。杖を振ると、
4桁のセグメントディスプレイがカウントを開始します。再度振るとカウントが停止します。表示されるカウントが **10.00** のままになったら、あなたの勝ちです。このゲームを友人とプレイして、誰がタイムウィザードであるかを確認してみてください。
必要な部品
------------------------------
このプロジェクトには、以下のコンポーネントが必要です。
.. image:: ../img/list_GAME_10_Second.png
:align: center
キット全体を購入することは間違いなく便利です。こちらがリンクです:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - 名前
- このキットのアイテム
- リンク
* - Raphael Kit
- 337
- |link_Raphael_kit|
以下のリンクからそれらを個別に購入することもできます。
.. list-table::
:widths: 30 20
:header-rows: 1
* - コンポーネントの紹介
- 購入リンク
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_4_digit`
- \-
* - :ref:`cpn_74hc595`
- |link_74hc595_buy|
* - :ref:`cpn_tilt_switch`
- \-
回路図
------------------------
============ ======== ======== ===
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
============ ======== ======== ===
.. image:: ../img/Schematic_three_one13.png
:align: center
実験手順
---------------------------------
**ステップ1**: 回路を組み立てます。
.. image:: ../img/image277.png
**ステップ2**: コードのフォルダに移動します。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python/
**ステップ3**: 実行可能ファイルを実行します。
.. raw:: html
.. code-block::
sudo python3 4.1.18_GAME_10Second.py
杖を振ると、4桁のセグメントディスプレイがカウントを開始します。再度振るとカウントが停止します。表示されるカウントが **10.00** になったら勝ちです。もう一度振るとゲームの次のラウンドが開始されます。
**コード**
.. note::
以下のコードを **修正/リセット/コピー/実行/停止** することができます。しかし、それをする前に、 ``raphael-kit/python`` のようなソースコードのパスに移動する必要があります。コードを修正した後、直接実行して効果を確認することができます。
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
import threading
sensorPin = 26
SDI = 24
RCLK = 23
SRCLK = 18
placePin = (10, 22, 27, 17)
number = (0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90)
counter = 0
timer =0
gameState =0
def clearDisplay():
for i in range(8):
GPIO.output(SDI, 1)
GPIO.output(SRCLK, GPIO.HIGH)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW)
def hc595_shift(data):
for i in range(8):
GPIO.output(SDI, 0x80 & (data << i))
GPIO.output(SRCLK, GPIO.HIGH)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
GPIO.output(RCLK, GPIO.LOW)
def pickDigit(digit):
for i in placePin:
GPIO.output(i,GPIO.LOW)
GPIO.output(placePin[digit], GPIO.HIGH)
def 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():
global gameState
global counter
global timer1
if gameState == 0:
counter = 0
time.sleep(1)
timer()
elif gameState ==1:
timer1.cancel()
time.sleep(1)
gameState = (gameState+1)%2
def loop():
global counter
currentState = 0
lastState = 0
while True:
display()
currentState=GPIO.input(sensorPin)
if (currentState == 0) and (lastState == 1):
stateChange()
lastState=currentState
def timer():
global counter
global timer1
timer1 = threading.Timer(0.01, timer)
timer1.start()
counter += 1
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(SDI, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
for i in placePin:
GPIO.setup(i, GPIO.OUT)
GPIO.setup(sensorPin, GPIO.IN)
def destroy(): # When "Ctrl+C" is pressed, the function is executed.
GPIO.cleanup()
global timer1
timer1.cancel()
if __name__ == '__main__': # Program starting from here
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
**コード説明**
.. code-block:: python
def stateChange():
global gameState
global counter
global timer1
if gameState == 0:
counter = 0
time.sleep(1)
timer()
elif gameState ==1:
timer1.cancel()
time.sleep(1)
gameState = (gameState+1)%2
このゲームは2つのモードに分かれています:
``gameState==0`` は「スタート」モードで、セグメントディスプレイに時間が表示され、傾斜スイッチを振ると「表示」モードに入ります。
``gameState==1`` は「表示」モードで、タイミングを停止し、セグメントディスプレイに時間を表示します。傾斜スイッチを再び振ると、タイマーがリセットされ、ゲームが再開されます。
.. code-block:: python
def loop():
global counter
currentState = 0
lastState = 0
while True:
display()
currentState=GPIO.input(sensorPin)
if (currentState == 0) and (lastState == 1):
stateChange()
lastState=currentState
``loop()`` はメインの関数です。まず、4ビットのセグメントディスプレイに時間が表示され、傾斜スイッチの値が読み取られます。傾斜スイッチの状態が変わった場合、 ``stateChange()`` が呼び出されます。
.. code-block:: python
def timer():
global counter
global timer1
timer1 = threading.Timer(0.01, timer)
timer1.start()
counter += 1
インターバルが0.01sに達すると、timer関数が呼び出されます。カウンターに1を加え、タイマーは0.01sごとに繰り返し自分自身を実行するために再び使用されます。
現象の画像
-----------------------
.. image:: ../img/image278.jpeg
:align: center