简体   繁体   中英

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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