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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
seaside_motors
Posts: 17
Joined: Thu Mar 25, 2021 4:19 am

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

Post by seaside_motors » Fri Apr 23, 2021 4:56 am

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

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

seaside_motors
Posts: 17
Joined: Thu Mar 25, 2021 4:19 am

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

Post by seaside_motors » Fri Apr 23, 2021 5:20 am

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.

My brain is getting kinda fuzzy so going to give it a rest for the night, hopefully some magic pythonista's will chime in with helpful hints ;)

Thanks,

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » Fri Apr 23, 2021 6:25 am

Suggested topic, Finite State Machines.

Below is what I do when I want to keep track of a switch state through-out both of its states.

Code: Select all

previous_PIN3_state = 0
new_PIN3_state = 0
   
# check PIN3 to see there has been a change
if((PIN3.value) != previous_PIN3_state)
    new_PIN3_state = PIN3.value()

 # switch debouncing, both ON and OFF transitions
    utime.sleep(.1)

# if PIN3 state has not changed in 100ms,
# we have a valid PIN3 state change
    if((PIN3.value() == new_PIN3_state)
        previous_PIN_state = new_PIN3_state #  update PIN3 state 
        
        if (new_PIN3_state == 1)
            do_something_useful()
        else if (new_PIN3_state == 0)
            do_something_else()
I translated this from a C program so there might be some minor errors.

The idea is you keep track of any changes. Hope it helps

Post Reply