简体   繁体   English

使用 MCP3002 模数转换器控制 LED 亮度

[英]Control LED brightness by using MCP3002 Analog to Digital Converter

I'm trying to control a list of PWMLED brightness by using MCP3002 ADC and Potentiometer.我正在尝试通过使用MCP3002 ADC和电位器来控制PWMLED亮度列表。

My issue is when I run my script everything is functioning as expected, except the potentiometer that is connected to MCP3002 ADC, it won't adjust the brightness of my PWMLED while I adjust the knob.我的问题是当我运行我的脚本时,一切都按预期运行,除了连接到 MCP3002 ADC 的电位器,当我调整旋钮时它不会调整我的PWMLED的亮度。

Here's my code:这是我的代码:

#!/usr/bin/python3

from gpiozero import PWMLED, Button, MCP3002
from threading import Thread
from signal import pause
from time import sleep
LEDs = [PWMLED(5), PWMLED(17), PWMLED(22), PWMLED(19), PWMLED(26)]
potentiometer = MCP3002(0)
pushButton = Button(21)
SPEED = 0.03


def analogRead():              # Now this function is working
    while True:                # the potentiometer value gets
    global LEDs                # displayed on the screen and it changes as
    print("analogRead called") # I adjust the knob but the ledSequnce() lights keep
    for PWMLED in LEDs:        # flickering, and the sequence gets interrupted
        if potentiometer.value < 0.02:
            PWMLED.value = 0
        else:
            PWMLED.value = potentiometer.value
        print(potentiometer.value)
        sleep(0.1)


def speedCounter():
    global SPEED
    if SPEED < 0.4:
        SPEED += 0.1
    else:
        SPEED = 0.03


def ledSequence():
    while True:
        for PWMLED in LEDs:
            PWMLED.on()
            sleep(SPEED)
            PWMLED.off()
        for PWMLED in reversed(LEDs):
            PWMLED.on()
            sleep(SPEED)
            PWMLED.off()


try:
    pushButton.when_pressed = speedCounter
    ledFlash = Thread(target=ledSequence, daemon=True)
    ledFlash.start()
    pot = Thread(target=analogRead, daemon=True)
    pot.start()
    pause()

except KeyboardInterrupt:
    exit(1)

But when I try this script it works just fine:但是当我尝试这个脚本时它工作得很好:

#!/usr/bin/python3

from gpiozero import PWMLED, MCP3002
from time import sleep

pot = MCP3002(0)
led = [PWMLED(5), PWMLED(17), PWMLED(22), PWMLED(19), PWMLED(26)]
while True:
    for PWMLED in led:
        if pot.value < 0.02:
            PWMLED.value = 0
        else:
            PWMLED.value = pot.value

        print(pot.value)
        sleep(0.1)

Your help would be really appreciated a lot!非常感谢您的帮助!

I found the solution to my problem.我找到了解决我的问题的方法。 So, I thought I should post it here!所以,我想我应该把它贴在这里!

#!/usr/bin/python3

from gpiozero import PWMLED, Button, MCP3002
from threading import Thread
from signal import pause
from time import sleep
LEDs = [PWMLED(5), PWMLED(17), PWMLED(22), PWMLED(19), PWMLED(26)]
potentiometer = MCP3002(0)
pushButton = Button(21)
SPEED = 0.03


def speedCounter():
    global SPEED
    if SPEED < 0.4:
        SPEED += 0.1
    else:
        SPEED = 0.03


def ledSequence():
    while True:
        for PWMLED in LEDs:
            PWMLED.on()
            PWMLED.value = potentiometer.value
            sleep(SPEED)
            PWMLED.off()
        for PWMLED in reversed(LEDs):
            PWMLED.on()
            PWMLED.value = potentiometer.value
            sleep(SPEED)
            PWMLED.off()


try:
    pushButton.when_pressed = speedCounter
    ledFlash = Thread(target=ledSequence, daemon=True)
    ledFlash.start()
    pause()

except KeyboardInterrupt:
    exit(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM