Wednesday, 19 April 2023

TTB07: Pico W LESSON 7: Controlling 3 LED with a Potentiometer in Micropython

from machine import Pin, ADC from time import sleep potPin = 28 greenLED = 3 yellowLED = 9 redLED = 15 myPot = ADC(potPin) myGreen = Pin(greenLED,Pin.OUT) myYellow = Pin(yellowLED,Pin.OUT) myRed = Pin(redLED,Pin.OUT) while True: potVal = myPot.read_u16() myVal=(100/65103)*potVal-(100*432/65103) print(myVal) sleep(.1) if myVal<80: myGreen.value(1) myYellow.value(0) myRed.value(0) if myVal>=80: myGreen.value(0) myYellow.value(1) myRed.value(0) if myVal>=95: myGreen.value(0) myYellow.value(0) myRed.value(1) Modification using compound conditional statement: if myVal>=80 and myVal<95: myGreen.value(0) myYellow.value(1) myRed.value(0)