6.2 流れる光

あなたの居間に楽しさとインタラクティブな要素を加えたいと思ったことはありませんか? このプロジェクトでは、WS2812 LEDストリップと障害物回避モジュールを使用して走行する光を作ります。 障害物が検出されると光の方向が変わり、家やオフィスの装飾にエキサイティングな追加をすることができます。

必要な部品

このプロジェクトには、以下のコンポーネントが必要です。

キット全体を購入すると確かに便利です。こちらがリンクです:

名前

このキットのアイテム

リンク

ESP32 Starter Kit

320+

ESP32 Starter Kit

以下のリンクから個別に購入することもできます。

コンポーネントの紹介

購入リンク

ESP32 WROOM 32E

BUY

ESP32カメラ拡張ボード

-

ジャンパーワイヤ

BUY

障害物回避モジュール

BUY

WS2812 RGB 8 LED ストリップ

BUY

回路図

../../_images/circuit_6.2_flowing_led.png

WS2812 LEDストリップは、異なる色やパターンを表示できる一連の個別LEDで構成されています。 このプロジェクトでは、ストリップは特定の方向に動く走行光を表示するように設定されており、 障害物回避モジュールによって障害物が検出されると方向が変わります。

配線図

../../_images/6.2_flowing_light_bb.png

コード

注釈

  • esp32-starter-kit-main\micropython\codes パスにある 6.2_flowing_led.py ファイルを開くか、コードをThonnyにコピー&ペーストします。次に、「Run Current Script」をクリックするかF5キーを押して実行します。

  • 右下隅にある「MicroPython (ESP32).COMxx」インタプリタを選択してください。

from machine import Pin
import neopixel
import time
import random

# Set the number of pixels for the running light
num_pixels = 8

# Set the data pin for the RGB LED strip
data_pin = Pin(14, Pin.OUT)

# Initialize the RGB LED strip object
pixels = neopixel.NeoPixel(data_pin, num_pixels)

# Initialize the avoid sensor
avoid = Pin(25, Pin.IN)

# Initialize the direction variable
direction_forward = True

# Initialize the reverse direction flag
reverse_direction = False

# Continuously loop the running light
while True:

# Read the input from the infrared sensor
avoid_value = avoid.value()

# Generate a random color for the current pixel
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

# If no obstacle is detected
if avoid_value:
      for i in range(num_pixels):

            # Turn on the current pixel with the random color
            pixels[i] = color

            # Update the RGB LED strip display
            pixels.write()

            # Turn off the current pixel
            pixels[i] = (0, 0, 0)
            time.sleep_ms(100)

# If detects an obstacle, change the direction of the LED strip
else:
      for i in range(num_pixels-1, -1, -1):

            pixels[i] = color
            pixels.write()
            pixels[i] = (0, 0, 0)
            time.sleep_ms(100)

スクリプトが実行されると、RGB ストリップの LED が 1 つずつ点灯します。 障害物回避モジュールの前に物体が置かれるとすぐに、RGB ストリップ上の LED が反対方向に 1 つずつ点灯します。