Friday, 10 March 2023

TTB06: Using 'IF' statement (NO potentiometer)

from machine import Pin import time led=Pin(15,Pin.OUT) while True: CMD=input('What is your Command? (ON/OFF/TOGGLE) ') if CMD=='ON': led.value(1) if CMD=='OFF': led.value(0) if CMD=='TOGGLE' : led.toggle()

TTB06a: Using 'IF' statements with a Potentiometer in RPi Pico

from machine import Pin from time import sleep potPin = 28 myPot = machine.ADC(potPin) ledR = Pin(2,Pin.OUT) ledY = Pin(4,Pin.OUT) ledG = Pin(15,Pin.OUT) try: while True: potVal = myPot.read_u16() percentage = (-100/65262)*potVal-(-100/65262*272)+100 if percentage<80: ledG.value(1) ledY.value(0) ledR.value(0) if percentage>=80 and percentage <95: ledG.value(0) ledY.value(1) ledR.value(0) if percentage>=95: ledG.value(0) ledY.value(0) ledR.value(1) print(int(percentage)) sleep(0.5) except KeyboardInterrupt: ledR.value(0) ledY.value(0) ledG.value(0) print('LEDs are off, Goodbye')