---
# System prepended metadata

title: 60位 WS2812B燈環與編碼器應用
tags: [Micropython]

---

---
disqus: ahb0222
GA : G-VF9ZT413CG
---
# 60位 WS2812B燈環與編碼器應用
> [color=#40f1ef][name=LHB阿好伯, 2021/07/16][:earth_africa:](https://www.facebook.com/LHB0222/)
###### tags: `Micropython`

[TOC]

# 選轉編碼器(rotary encoder)
https://www.youtube.com/watch?v=v4BbSzJ-hz4
{%youtube v4BbSzJ-hz4 %}
![](https://hackmd.io/_uploads/BJDSCTPic.png)

![](https://hackmd.io/_uploads/H1n80pvjc.png)


順時針
![](https://hackmd.io/_uploads/BkSapTDsq.png)

逆時針
![](https://hackmd.io/_uploads/HJex0pvsq.png)


## 選轉編碼器套件
https://github.com/infinite-tree/micropython-encoder-knob

## 選轉選單
```python=
import time
from encoder import EncoderKnob
from machine import Pin
from neopixel import NeoPixel
pin = Pin(32, Pin.OUT)   # 將GPIO4設置為輸出以驅動Neopixels
ws2812 = NeoPixel(pin, 60)
button_pin = Pin(23, Pin.IN, Pin.PULL_UP)
def rotated(amount):
    print("")

def pressed():
    print("")
    


def test():
    n1 =  0
    n2 = 0
    enc = EncoderKnob(21, 22, btn_pin=23, rotary_callback=rotated, btn_callback=pressed)
    while True:
        if button_pin.value() == 0:
            enc = EncoderKnob(21, 22, btn_pin=23, rotary_callback=rotated, btn_callback=pressed)
            n1 = 0
        n1 = abs(enc.value())%60
        time.sleep(0.01)
        if n1 != n2:
            ws2812[n2] = (0, 0, 0)
            ws2812.write()
        ws2812[n1] = (1, 1, 1)
        ws2812.write()
        n2 = n1
            
        
print("Turn the knob and press the button...")
test()

```

## 彩虹燈環
```python=
from machine import Pin
from neopixel import NeoPixel
import time

pin = Pin(32, Pin.OUT)   # 將GPIO4設置為輸出以驅動Neopixels
ws2812 = NeoPixel(pin, 60)   # 在GPIO0上以8像素創建NeoPixel驅動程序
#  彩虹燈條
def wheel(pos):
    #Input a value 0 to 255 to get a color value.
    #The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)



def rainbow_cycle(n, wait):
    for j in range(255):
        for i in range(n):
            rc_index = (i * 256 // n) + j
            ws2812[i] = wheel(rc_index & 255)
            ws2812.write()
            time.sleep_ms(wait)

rainbow_cycle(60,50)   #rainbow_cycle(n, wait) n 燈泡數   wait 間距毫秒數

```

## 彩虹選轉選單
```python=
from machine import Pin
from neopixel import NeoPixel
import time

pin = Pin(32, Pin.OUT)   # 將GPIO4設置為輸出以驅動Neopixels
ws2812 = NeoPixel(pin, 60)   # 在GPIO0上以8像素創建NeoPixel驅動程序
#  彩虹燈條
def wheel(pos):
    #Input a value 0 to 255 to get a color value.
    #The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)



def rainbow_cycle(n, wait):
    for j in range(255):
        for i in range(n):
            rc_index = (i * 256 // n) + j
            ws2812[i] = wheel(rc_index & 255)
            ws2812.write()
            time.sleep_ms(wait)

rainbow_cycle(60,50)   #rainbow_cycle(n, wait) n 燈泡數   wait 間距毫秒數

```

🌟全文可以至下方連結觀看或是補充


全文分享至

https://www.facebook.com/LHB0222/

https://www.instagram.com/ahb0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

# [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings)



![](https://i.imgur.com/nHEcVmm.jpg)


