Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

MicroPython assist needed, basic led program
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MicroPython assist needed, basic led program

by seasidemotors on Fri Apr 23, 2021 1:28 pm

How to print a single statement when button state =(0)?

Howdy all,

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')

Could I define a button function statement?

Example:
def button_on()
print('You pressed the button!')
def button_off()
print('You killed the lights!')

Then I could place a call to each function with their respective if statements?

But would they not simply print on and on, or would they simply print the one time?

I think I need a boolean truth/false statement such as "if button value =1" then call function "button_on()" and likewise if button value =0 then call function button_off().?
If any print statements are included in the if loop then they are going to simply print over and over because loop.

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!

PS:Running on RPi Pico

Code: Select all | TOGGLE FULL SIZE
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!')

seasidemotors
 
Posts: 9
Joined: Wed Mar 24, 2021 9:55 pm

Re: MicroPython assist needed, basic led program

by seasidemotors on Fri Apr 23, 2021 1:58 pm

Ok so I just tried using the definition statements and they error out the program :(

I tried commenting out the print statements and also adding a colon behind the def statements, all of which still resulted in errors.

Code: Select all | TOGGLE FULL SIZE
import machine
import utime

# Define button states, print confirmation statement
def buttonstate_on()
    print('You pressed the button')
def buttonstate_off()
    print('You killed the lights')

# Define external components

seasidemotors
 
Posts: 9
Joined: Wed Mar 24, 2021 9:55 pm

Re: MicroPython assist needed, basic led program

by MarksBench on Fri Apr 23, 2021 10:19 pm

Hi,

I am not a programming expert by any means so others will probably have better ways to do this, but if you're looking to just have the message show up once when you hold the button down or the button isn't pressed, you could use another variable to hold the "last" state of what the button was doing and then if something has changed, print the message then.

Something like:
Code: Select all | TOGGLE FULL SIZE
#Setting up button state variable to a default (0 in this case)
buttonState = 0

while True:
    if button.value()==1:
        if buttonState == 0:
            print("You pressed the button")
            buttonState = 1

    if button.value()==0:
        if buttonState == 1:
            print("You released the button")
            buttonState = 0


So each time it goes through the loop it looks for two things: whether the button is pressed or not pressed, and whether the button was pressed or not pressed the previous run through. If it sees that the button was in a different state the previous run through, show the message and then set the buttonState to what the button is currently set at. If it goes through and it's in the same state it was before then it won't print anything.

As for the functions, I'm pretty sure you need a colon after a function definition, so instead of

Code: Select all | TOGGLE FULL SIZE
def buttonstate_on()

it should be
Code: Select all | TOGGLE FULL SIZE
def buttonstate_on():

I hope this helps - good luck with your project!

MarksBench
 
Posts: 9
Joined: Tue Apr 20, 2021 5:40 pm

Re: MicroPython assist needed, basic led program

by seasidemotors on Sat Apr 24, 2021 1:34 pm

Unfortunately I was unable to make any of the print statements work correctly.

Trying to define button states to be able to print a single line to the screen was a failure :(

Perhaps some point in the future I can come back to my blinkypee.py program and give it another go.

Thanks for trying to help.

For those interested I have 5 different colored LED's in a single stack with a single button on a bread board. When the button is pressed all 5 leds blink at the same time at the same defined rate (.25 second), they will continue to blink as long as the button is held down, as soon as you release, they turn off.

Here is my final code before I walk away and move on to other experiments.

Code: Select all | TOGGLE FULL SIZE
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)

    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)


Cheers!

seasidemotors
 
Posts: 9
Joined: Wed Mar 24, 2021 9:55 pm

Re: MicroPython assist needed, basic led program

by MarksBench on Sat Apr 24, 2021 7:51 pm

Sorry it's being difficult but I'm sure we can get this figured out.

Let's keep it simple and not break anything else out into its own function right yet. What happens when you use this code?

Code: Select all | TOGGLE FULL SIZE
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)

# Define button state variable, initialize it with zero
previousButtonState = 0

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)
        if previousButtonState == 0:
            print('You pressed the button. Setting previousButtonState to 1.')
            previousButtonState = 1
    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)
        if previousButtonState == 1:
            print('Lights Out! Setting previousButtonState to 0.')
            previousButtonState = 0


I just ran this on a Pi Pico using MicroPython 1.15 (2021-04-18) and I _think_ it's doing what you're looking for:

animation3.gif
Pi Pico responding to button press and release
animation3.gif (905.98 KiB) Viewed 63 times

MarksBench
 
Posts: 9
Joined: Tue Apr 20, 2021 5:40 pm

Re: MicroPython assist needed, basic led program

by seasidemotors on Sun Apr 25, 2021 12:59 am

That indeed did the trick! :)

Thanks for taking the time to get involved in my little piece of code and helping out.
Uploading that little gif was a nice touch too ;)

That is really appreciated as I am new to programming and micropython.
Not making it easy on myself either as I am writing my code in VIM editor and uploading code via rshell.

Thanks Again,
Robert

seasidemotors
 
Posts: 9
Joined: Wed Mar 24, 2021 9:55 pm

Re: MicroPython assist needed, basic led program

by MarksBench on Sun Apr 25, 2021 2:52 am

Hey, that's great! Good to hear it worked with your setup!

Writing it in vim and uploading it remotely... you're braver than I am :-)

Good luck with your project!

MarksBench
 
Posts: 9
Joined: Tue Apr 20, 2021 5:40 pm

Please be positive and constructive with your questions and comments.