0

After getting the first input, is not following the normal logic to continue waiting for the input and directly return an error.

1.Checking if there is any data on the stdin with readline() function and strip any spaces with strip() 2.If there is "1" received, will go to the logic of first menu item when is waiting for the input

issue is that after receiving the input "1", it continues to show the prompt and automatically show "False" for the input and the error, without any actual input. Similar for other menu item.

my code is the following, where i am trying to set the values for the red, green and blue components and drive the strip.

from machine import Pin, PWM
import sys, time, neopixel, select

led_control=neopixel.NeoPixel(Pin(5),1)
led_control[0]=(0,0,255)
led_control.write()
time.sleep(1)
led_control[0]=(0,0,0)
led_control.write()

strip_r=Pin(8,Pin.OUT)
strip_g=Pin(7,Pin.OUT)
strip_b=Pin(6,Pin.OUT)

pwm_r=PWM(strip_r)
pwm_r.freq(2000)
pwm_g=PWM(strip_g)
pwm_g.freq(2000)
pwm_b=PWM(strip_b)
pwm_b.freq(2000)

pwm_r.duty_u16(0)
pwm_g.duty_u16(0)
pwm_b.duty_u16(0)

def citire_serial():
    if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
        return sys.stdin.readline().strip()
    return None

def rgb_strip(rgb,bright):
    r,g,b=rgb
    duty_r=int((r*256)*int(bright)/100)
    duty_g=int((g*256)*int(bright)/100)
    duty_b=int((b*256)*int(bright)/100)
    pwm_r.duty_u16(duty_r)
    pwm_g.duty_u16(duty_g)
    pwm_b.duty_u16(duty_b)
    
def setari():
    print("Meniu setari RGB")
    print("----------------")
    print("[1] Setare nivel R")
    print("[2] Setare nivel G")
    print("[3] Setare nivel B")
    print("[4] Setare intensitate")
    
    while True:
        meniu = citire_serial()
        if meniu is not None:
            if meniu == "1":
                led_control[0]=(255,0,0)
                led_control.write()
                print("Introduce nivel Rosu (0 la 255):")
                time.sleep(0.5)
                nivel_rosu = citire_serial()
                print(nivel_rosu.isdigit())
                if nivel_rosu.isdigit():
                    rosu = int(nivel_rosu)
                    rgb=rosu,0,0
                    rgb_strip(rgb,100)
                else:
                    print("Eroare: Introduceti o valoare numerica!")
            elif meniu == "2":
                led_control[0]=(0,255,0)
                led_control.write()
                print("Introduce nivel Verde (0 la 255):")
                nivel_verde = citire_serial()
                print(nivel_verde)
                if nivel_verde.isdigit():
                    verde = int(nivel_verde)
                    rgb=0,verde,0
                    rgb_strip(rgb,100)
                else:
                    print("Eroare: Introduceti o valoare numerica!")

while True:
    check=citire_serial()
    if check == "setari":
        setari()
1
  • 1
    That's a lot of code and probably 85% of it isn't relevant to the problem. Can you reduce it down to a minimal reproducible example? It might also be good to show the exact output it gave you instead of just describing it. Commented May 14 at 21:22

0

Browse other questions tagged or ask your own question.