.. note::
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
**参加する理由は?**
- **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。
- **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。
- **特別割引**:最新製品の独占割引をお楽しみください。
- **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
.. _2.1.5_py:
2.1.5 傾斜スイッチ
======================
はじめに
------------
このスイッチは、内部に金属のボールが入っている傾斜スイッチです。わずかな角度の傾斜を検出するために使用されます。
必要な部品
------------------------------
このプロジェクトでは、以下の部品が必要です。
.. image:: ../img/list_2.1.3_tilt_switch.png
一式をまとめて購入するのは非常に便利です。以下にリンクを掲載します。
.. 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_led`
- |link_led_buy|
* - :ref:`cpn_tilt_switch`
- \-
回路図
-----------------
.. image:: ../img/image307.png
.. image:: ../img/image308.png
実験手順
-----------------------
**ステップ1:** 回路を組み立てます。
.. image:: ../img/image169.png
**ステップ2:** ディレクトリを変更します。
.. raw:: html
.. code-block::
cd ~/raphael-kit/python/
**ステップ3:** 実行します。
.. raw:: html
.. code-block::
sudo python3 2.1.5_Tilt.py
傾斜スイッチを垂直に配置すると、緑色のLEDが点灯します。傾斜させると、「Tilt!」と画面に表示され、赤色のLEDが点灯します。再度垂直に配置すると、緑色のLEDが点灯します。
**コード**
.. note::
下記のコードは **修正/リセット/コピー/実行/停止** が可能です。ただし、それに先立ち、 ``raphael-kit/python`` のようなソースコードのパスに移動する必要があります。コードを修正した後、直接実行して効果を確認できます。
.. raw:: html
.. code-block:: python
import RPi.GPIO as GPIO
TiltPin = 17
Gpin = 27
Rpin = 22
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(Gpin, GPIO.OUT) # Set Green Led Pin mode to output
GPIO.setup(Rpin, GPIO.OUT) # Set Red Led Pin mode to output
GPIO.setup(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V)
GPIO.add_event_detect(TiltPin, GPIO.BOTH, callback=detect, bouncetime=200)
def Led(x):
if x == 0:
GPIO.output(Rpin, 1)
GPIO.output(Gpin, 0)
if x == 1:
GPIO.output(Rpin, 0)
GPIO.output(Gpin, 1)
def Print(x):
if x == 0:
print (' *************')
print (' * Tilt! *')
print (' *************')
def detect(chn):
Led(GPIO.input(TiltPin))
Print(GPIO.input(TiltPin))
def loop():
while True:
pass
def destroy():
GPIO.output(Gpin, GPIO.HIGH) # Green led off
GPIO.output(Rpin, GPIO.HIGH) # Red led off
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the program destroy() will be executed.
destroy()
**コード説明**
.. code-block:: python
GPIO.add_event_detect(TiltPin, GPIO.BOTH, callback=detect, bouncetime=200)
TiltPinに対して検出を設定し、コールバック関数にdetectを指定します。
.. code-block:: python
def Led(x):
if x == 0:
GPIO.output(Rpin, 1)
GPIO.output(Gpin, 0)
if x == 1:
GPIO.output(Rpin, 0)
GPIO.output(Gpin, 1)
Led()という関数を定義して、2つのLEDを制御します。x=0の場合、赤いLEDが点灯します。それ以外の場合は、緑のLEDが点灯します。
.. code-block:: python
def Print(x):
if x == 0:
print (' *************')
print (' * Tilt! *')
print (' *************')
Print()という関数を定義して、画面に上記の文字を表示します。
.. code-block:: python
def detect(chn):
Led(GPIO.input(TiltPin))
Print(GPIO.input(TiltPin))
傾斜スイッチの読み取り値に基づいて、2つのLEDの点灯または消灯を制御するコールバック関数を定義します。
現象の画像
------------------
.. image:: ../img/image170.jpeg