.. note:: こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。 **参加する理由は?** - **エキスパートサポート**:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。 - **学び&共有**:ヒントやチュートリアルを交換してスキルを向上させましょう。 - **独占的なプレビュー**:新製品の発表や先行プレビューに早期アクセスしましょう。 - **特別割引**:最新製品の独占割引をお楽しみください。 - **祭りのプロモーションとギフト**:ギフトや祝日のプロモーションに参加しましょう。 👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう! .. _1.1.3_py_pi5: 1.1.3 LED バーグラフ ====================== 概要 ------------- このプロジェクトでは、LEDバーグラフ上のライトを順番に点灯させます。 必要な部品 ------------------------------ このプロジェクトには、次のコンポーネントが必要です。 .. image:: ../python_pi5/img/1.1.3_led_bar_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_bar_graph` - \- 回路図 ------------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO18 Pin 12 1 18 GPIO23 Pin 16 4 23 GPIO24 Pin 18 5 24 GPIO25 Pin 22 6 25 SPICE0 Pin 24 10 8 SPICE1 Pin 26 11 7 GPIO12 Pin 32 26 12 GPIO16 Pin 36 27 16 GPIO20 Pin 38 28 22 GPIO21 Pin 40 29 21 ============ ======== ======== === .. image:: ../python_pi5/img/1.1.3_LedBarGraph_schematic.png 実験手順 ------------------------------ **ステップ1**: 回路を組み立てます。 .. note:: 接続する際に方向に注意してください。逆に接続すると点灯しません。 .. image:: ../python_pi5/img/1.1.3_LedBarGraph_circuit.png **ステップ2:** コードのフォルダに移動します。 .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **ステップ3**: 実行可能ファイルを実行します。 .. raw:: html .. code-block:: sudo python3 1.1.3_LedBarGraph_zero.py コードを実行した後、LEDバーグラフ上のLEDが定期的に点灯および消灯するのを確認できます。 .. warning:: エラー メッセージ ``RuntimeError: Cannot determine SOC peripheral base address`` が表示された場合は、 :ref:`faq_soc` を参照してください。 **コード** .. note:: 以下のコードは **変更/リセット/コピー/実行/停止** することができます。ただし、それを行う前に「raphael-kit/python_5」というソースコードのパスに移動する必要があります。コードを変更した後、そのまま実行して効果を確認できます。 .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED from time import sleep # Define GPIO pins where LEDs are connected led_pins = [18, 23, 24, 25, 8, 7, 12, 16, 20, 21] # Create LED objects for each pin leds = [LED(pin) for pin in led_pins] def odd_led_bar_graph(): # Sequentially light up odd-numbered LEDs (index 0, 2, 4, etc.) for i in range(5): j = i * 2 # Calculate odd index leds[j].on() # Turn on odd-numbered LED sleep(0.3) # Delay for visual effect leds[j].off() # Turn off LED def even_led_bar_graph(): # Sequentially light up even-numbered LEDs (index 1, 3, 5, etc.) for i in range(5): j = i * 2 + 1 # Calculate even index leds[j].on() # Turn on even-numbered LED sleep(0.3) # Delay for visual effect leds[j].off() # Turn off LED def all_led_bar_graph(): # Sequentially light up all LEDs one by one for led in leds: led.on() # Turn on LED sleep(0.3) # Delay for visual effect led.off() # Turn off LED def turn_off_all_leds(): # Turn off all LEDs at once for led in leds: led.off() try: # Main loop to cycle through LED patterns while True: odd_led_bar_graph() # Activate odd-numbered LEDs sleep(0.3) # Pause between patterns even_led_bar_graph() # Activate even-numbered LEDs sleep(0.3) # Pause between patterns all_led_bar_graph() # Activate all LEDs sleep(0.3) # Pause before restarting except KeyboardInterrupt: # Handle interruption (Ctrl+C) gracefully turn_off_all_leds() # Ensure all LEDs are turned off on exit pass **コードの説明** #. これらの行は必要なクラスと関数をインポートします。LED制御のための ``gpiozero`` の ``LED`` および遅延のための ``time`` の ``sleep`` です。 .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED from time import sleep #. ``led_pins`` リストにはGPIOピン番号が含まれており、 ``leds`` は ``led_pins`` の各ピンに対応する ``LED`` オブジェクトのリストです。 .. code-block:: python # Define GPIO pins where LEDs are connected led_pins = [18, 23, 24, 25, 8, 7, 12, 16, 20, 21] # Create LED objects for each pin leds = [LED(pin) for pin in led_pins] #. LEDバーグラフの奇数番号のLEDを順番に点灯させます。 .. code-block:: python def odd_led_bar_graph(): # Sequentially light up odd-numbered LEDs (index 0, 2, 4, etc.) for i in range(5): j = i * 2 # Calculate odd index leds[j].on() # Turn on odd-numbered LED sleep(0.3) # Delay for visual effect leds[j].off() # Turn off LED #. LEDバーグラフの偶数番号のLEDを順番に点灯させます。 .. code-block:: python def even_led_bar_graph(): # Sequentially light up even-numbered LEDs (index 1, 3, 5, etc.) for i in range(5): j = i * 2 + 1 # Calculate even index leds[j].on() # Turn on even-numbered LED sleep(0.3) # Delay for visual effect leds[j].off() # Turn off LED #. LEDバーグラフのLEDを1つずつ順番に点灯させます。 .. code-block:: python def all_led_bar_graph(): # Sequentially light up all LEDs one by one for led in leds: led.on() # Turn on LED sleep(0.3) # Delay for visual effect led.off() # Turn off LED #. ``while True`` ループはLEDパターンを連続してサイクルします。 ``except`` ブロックはKeyboardInterrupt(Ctrl+C)を処理し、終了時にすべてのLEDが消灯することを確認します。 .. code-block:: python try: # Main loop to cycle through LED patterns while True: odd_led_bar_graph() # Activate odd-numbered LEDs sleep(0.3) # Pause between patterns even_led_bar_graph() # Activate even-numbered LEDs sleep(0.3) # Pause between patterns all_led_bar_graph() # Activate all LEDs sleep(0.3) # Pause before restarting except KeyboardInterrupt: # Handle interruption (Ctrl+C) gracefully turn_off_all_leds() # Ensure all LEDs are turned off on exit pass