3

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!

MrYosuko
  • 47
  • 7
  • 2
    It appears the your PWMLEDs is a list, and as far as I know a list does not have a .value attribute which can be assigned. Perhaps you mean to write: ‘PWMLED.value’ (with no ‘s’), which is each PWM in your list? – S3DEV Apr 08 '20 at 17:40
  • @S3DEV yes that's what I want. So, I just would have to remove 's' from ```PWMLEDs.value``` ? – MrYosuko Apr 08 '20 at 18:16
  • @S3DEV I removed the 's', and it didn't have any effect on my ```PWMLED``` – MrYosuko Apr 08 '20 at 18:28
  • Yes, @S3DEV in ```if: for``` loop and ```else: for``` loop. I have updated my code – MrYosuko Apr 08 '20 at 18:40
  • 1
    Interesting. That was definitely an issue, but uncertain why you’re having a problem. Made a couple edits your code above. Please accept the edits and make them in your source code and try again, please. – S3DEV Apr 08 '20 at 18:45
  • @S3DEV I applied the edits on my code, and still did not affect the brightness of PWM LED – MrYosuko Apr 08 '20 at 18:47
  • It looks like there is not a feedback loop in place for your pot. I.e: When you turn the knob, I don’t see anything to pick up the change in position and trigger that bit of code. It’s like analogRead() is only called on startup, and not while the controller is running. Please have a look further into this. Your button has a when_pressed listener, but I don’t see one on the pot. I suggest putting some print() statements thoughout to get verification what parts of code are running and what are not. – S3DEV Apr 08 '20 at 18:51
  • @S3DEV I added a ```while True:``` loop to the function it still isn't working even though I made a separate python script for the same ```PWMLED```, and it worked but for the original script isn't working I don't understand why even though code for the function from the original python code and the second one I just made for ```PWMLED```are identical! check it out, it's at the bottom. P.S. I added a print statement to the function, and it prints the same number no matter how much I adjust the pot knob – MrYosuko Apr 08 '20 at 19:17
  • 1
    I’m 90% certain it has to do with the analogRead() function not getting called. (Don’t think you need a while True loop in there). Under global LEDS put: print(‘analogRead called’). Then run. This will help us debug if the function is getting called. – S3DEV Apr 08 '20 at 19:36
  • @S3DEV I added the print statement and got printed on the screen. Pot still has zero effect on the red sequence – MrYosuko Apr 08 '20 at 19:48
  • Sorry mate; I’m out of ideas at the moment, but will have a think ... – S3DEV Apr 08 '20 at 19:54
  • 1
    Ok @S3DEV , I honestly appreciate all your help man... – MrYosuko Apr 08 '20 at 20:00

1 Answers1

0

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)
MrYosuko
  • 47
  • 7