.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _2.1.4_py_pi5: 2.1.4 スライドスイッチ ================================== はじめに ------------ このプロジェクトでは、スライドスイッチの使用方法を学びます。通常、スライドスイッチはPCB上に電源スイッチとしてはんだ付けされますが、ここではブレッドボードに挿入するため、しっかり固定されない場合があります。ブレッドボード上でのその機能を示すために使用します。 必要な部品 ------------------------------ このプロジェクトには、次のコンポーネントが必要です。 .. image:: ../python_pi5/img/2.1.4_slide_switch_list.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_slide_switch` - |link_slide_switch_buy| * - :ref:`cpn_capacitor` - |link_capacitor_buy| 回路図 ----------------- スライドスイッチの中央のピンをGPIO17に接続し、 二つのLEDをそれぞれGPIO22とGPIO27のピンに接続します。 スライドを引くと、二つのLEDが交互に点灯するのが見られます。 .. image:: ../python_pi5/img/2.1.4_slide_switch_schematic_1.png .. image:: ../python_pi5/img/2.1.4_slide_switch_schematic_2.png 実験手順 ----------------------- **ステップ 1:** 回路を組み立てる。 .. image:: ../python_pi5/img/2.1.4_slide_switch_circuit.png **ステップ 2**: コードのフォルダに移動する。 .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **ステップ 3**: 実行する。 .. raw:: html .. code-block:: sudo python3 2.1.4_Slider_zero.py コードが実行されている間、スイッチを左につなぐと、黄色いLEDが点灯します。右につなぐと、赤いLEDが点灯します。 .. warning:: エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。 **コード** .. note:: 下記のコードを **変更/リセット/コピー/実行/停止** することができます。しかし、その前に ``raphael-kit/python-pi5`` のようなソースコードのパスに移動する必要があります。コードを変更した後、直接実行して効果を見ることができます。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button # Import LED and Button classes for GPIO control from time import sleep # Import sleep function for adding delays # Initialize the micro switch on GPIO pin 17 with the pull-up resistor disabled micro_switch = Button(17, pull_up=False) # Initialize LED1 on GPIO pin 22 led1 = LED(22) # Initialize LED2 on GPIO pin 27 led2 = LED(27) try: # Main loop to control LED states based on the micro switch's state while True: if micro_switch.is_pressed: # Check if the micro switch is pressed print(' LED1 ON ') # Print status message led1.on() # Turn on LED1 led2.off() # Turn off LED2 else: # If the micro switch is not pressed print(' LED2 ON ') # Print status message led1.off() # Turn off LED1 led2.on() # Turn on LED2 sleep(0.5) # Wait for 0.5 seconds before rechecking the switch state except KeyboardInterrupt: # Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop pass **コード説明** #. この行はスクリプトをPython 3で実行するように設定します。GPIOデバイスを制御するために ``gpiozero`` から ``LED`` と ``Button`` をインポートし、遅延を追加するために ``time`` から ``sleep`` をインポートします。 .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, Button # Import LED and Button classes for GPIO control from time import sleep # Import sleep function for adding delays #. プルアップ抵抗を無効にしてGPIOピン17に接続されたマイクロスイッチ、およびGPIOピン22と27に接続された二つのLEDを初期化します。 .. code-block:: python # Initialize the micro switch on GPIO pin 17 with the pull-up resistor disabled micro_switch = Button(17, pull_up=False) # Initialize LED1 on GPIO pin 22 led1 = LED(22) # Initialize LED2 on GPIO pin 27 led2 = LED(27) #. メインループでは、マイクロスイッチの状態をチェックします。押された場合、LED1が点灯しLED2が消灯します;押されていない場合、LED1が消灯しLED2が点灯します。このループは0.5秒ごとに繰り返されます。Ctrl+C(KeyboardInterrupt)を捕捉して、スクリプトを優雅に終了させることができます。 .. code-block:: python try: # Main loop to control LED states based on the micro switch's state while True: if micro_switch.is_pressed: # Check if the micro switch is pressed print(' LED1 ON ') # Print status message led1.on() # Turn on LED1 led2.off() # Turn off LED2 else: # If the micro switch is not pressed print(' LED2 ON ') # Print status message led1.off() # Turn off LED1 led2.on() # Turn on LED2 sleep(0.5) # Wait for 0.5 seconds before rechecking the switch state except KeyboardInterrupt: # Handle a keyboard interrupt (Ctrl+C) for a clean exit from the loop pass