Ok so just getting started here and trying to figure some basic code. I have a set of LED's that I have setup to flash when you press a button. This works as coded.
I am trying to feedback some basic info back to the terminal window. So in my loop when button state is (1) #'on' it flashes the LED's and prints a message 'You pressed the button'. Now this part works just fine, other then it prints every time the LED's flash, so If I hold the button down the leds will continue to flash and it will continue to print the statement. I tried moving it under the 'utime' statement and that did nothing, I also tried moving it out of the loop but then it did not print at all.
Next up when button state is (0), this kills the leds and this works. I wanted to print a statement 'You killed the lights' but when I put it in the loop it prints it continuously when your not pressing the button. I tried taking it out of the loop, I also tried both a if and elif style loop and neither will work properly in regards to printing the statement, If I take it out of the loop then it simply does not print.
What I would ultimately like to do is have it print once per occurrence for both statements. So when button state ()=1 it prints, 'You pressed the button' one time only even if I hold it down. Same is true if I release the button, button state ()=0, it should print one time only 'You killed the lights')
In addition if you see any way of optimizing the code I am all ears. I tried to put all the external_led=machine.PIN gpios all in one statement but they would not work correctly until I put them all in their individual statements.
Example:
led_external=machine.Pin(11,12,13,14,15, machine.Pin.OUT)
Thanks!
Code: Select all
import machine
import utime
# Define external components
button=machine.Pin(10, machine.Pin.IN, machine.Pin.PULL_DOWN)
led_external0=machine.Pin(11, machine.Pin.OUT)
led_external1=machine.Pin(12, machine.Pin.OUT)
led_external2=machine.Pin(13, machine.Pin.OUT)
led_external3=machine.Pin(14, machine.Pin.OUT)
led_external4=machine.Pin(15, machine.Pin.OUT)
while True:
if button.value()==1: #Set button ON action
led_external0.toggle()
led_external1.toggle()
led_external2.toggle()
led_external3.toggle()
led_external4.toggle()
utime.sleep(.25)
print('You pressed the button')
if button.value()==0: #Set button OFF action
led_external0.value(0)
led_external1.value(0)
led_external2.value(0)
led_external3.value(0)
led_external4.value(0)
print('Lights Out!')