Page 1 of 1

Print message when same button is pressed

Posted: Sat Sep 25, 2021 1:10 am
by soggycashew
Hello, What I'm trying to do is I have two buttons and a active buzzer. If I press B1 I need it to Print("Goto button 2") and I would proceed to B2 and it would print Print("Goto button 1") and all is good BUT here is my issue.

If I press B1 and then I press B1 again I need to Print("Error you double pressed button 1") and
sound the active buzzer for 3 seconds and print Print("Goto button 2").

If I press B2 and then I press B2 again I need to Print("Error you double pressed button 2") and
sound the active buzzer for 3 seconds and print Print("Goto button 1").

How can I do this? The code below I put together from the web and all it does is print when I press either button and I can hold down the buttons and it will only print 1 time with each press.

Code: Select all

import machine
import utime
 
B1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
B2 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(12, machine.Pin.OUT) 

B1_last_state = False
B1_current_state = False
B2_last_state = False
B2_current_state = False
        
while True:
    B1_current_state = B1.value()
    if B1_last_state == 0 and B1_current_state == 1:
        print("You pressed button 1)
        utime.sleep(0.25)
    B1_last_state = B1_current_state
 
 
    B2_current_state = B2.value()
    if B2_last_state == 0 and B2_current_state == 1:
        print("You pressed button 2")
        utime.sleep(0.25)
    B2_last_state = B2_current_state

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 1:33 am
by davef
I'd try using a counter on the button states.

And some more if ... else statements.

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 7:23 am
by fdufnews
The program needs to remember wich button was previously pressed.
And you then compare the currently one and the previous and if it matches you print an error.

So, when a button is pressed you put the button number in a variable for later use.

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 2:40 pm
by soggycashew
Im a beginner can you give an example to start from?

Thanks,

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 3:49 pm
by hippy
Replace

Code: Select all

print("You pressed button 1")
With something like -

Code: Select all

if last_pushed == 1:
  print("You pressed button 1 again")
elif last_pushed == 2:
  print("You pressed button 1 after pressing button 2")
else:
  print("You pressed button 1")
last_pushed = 1
Add similar for the other button, initialise last_pushed to zero.

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 9:00 pm
by cnmcdee
soggycashew wrote:
Sat Sep 25, 2021 1:10 am
Hello, What I'm trying to do is I have two buttons and a active buzzer. If I press B1 I need it to Print("Goto button 2") and I would proceed to B2 and it would print Print("Goto button 1") and all is good BUT here is my issue.

If I press B1 and then I press B1 again I need to Print("Error you double pressed button 1") and
sound the active buzzer for 3 seconds and print Print("Goto button 2").

If I press B2 and then I press B2 again I need to Print("Error you double pressed button 2") and
sound the active buzzer for 3 seconds and print Print("Goto button 1").

How can I do this? The code below I put together from the web and all it does is print when I press either button and I can hold down the buttons and it will only print 1 time with each press.

Sounds like a fun problem, there are LOTS of ways to code this however you want to groom your logic so it behaves.
Here is a simple toggle example:

Code: Select all


import machine
import utime
 
B1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
B2 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(12, machine.Pin.OUT) 

def nasty_buzzer():
	print("nasty buzzer")
	# I think the standard tutorial on a buzzer is milliseconds on / milliseconds off..
	# I'll leave it for someone if they want to look it up.	

button1_counter = 0
button2_counter = 0

if B1.value(): # was the B1 button pushed?
	button1_counter += 1
	button2_counter = 0   # Force the toggle (reset the other side)
	if button1_counter % 2 == 1:  # Message head to button 2 on odd pushes
		print("Goto Button 2")
	if button1_counter %2 == 0:  #Pushed it twice kick to buzzer 
		print("You double pressed Button 1!")
		nasty_buzzer()
		print("Goto Button 2!")
	
if B2.value():  # Was the B2 button pushed?
	button2_counter += 1
	button1_counter = 0 # Force the toggle (reset the other side)
	if button2_counter %2 == 1:  # Message head to button 0 on odd pushes
		print("Goto Button 1")
	if button1_counter %2 == 0: #Pushed it twice kick to buzzer
		print("You double pressed Button 2!")
		nasty_buzzer()
		print("Goto Button 1!")
	
This logic block will toggle between "Goto button X" and Buzzer. If you want it to latch to buzzer indefinitely after the second push it would be:

Code: Select all

	if button2_counter == 1: # Message head to button 0 on the first push
		print("Goto Button 1")
	if button2_counter > 1:  # Buzzer here on in.
		nasty_buzzer()


Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 9:15 pm
by soggycashew
Thank you so much guys/gals I cant wait to get home ant try this! If i were to have another button B3 that could turn off the buzzer when it goes off is that doable?

On a side note thank you for the #comments so i know how its working, I been searching the web for a few weeks and cant find anything pertaining to my scenario. Is there a book of examples i can get pertaining to the pico and micropython? I do have the book getting started with micropython on the Pico but it's kind of vague.

Thanks!

Re: Print message when same button is pressed

Posted: Sat Sep 25, 2021 9:28 pm
by cnmcdee

Code: Select all


buzzer_enabled = True

def nasty_buzzer():
	if buzzer_enabled:
		print("Nasty buzzer")
		
if button3.value():  # Silence your buzzer.
	buzzer_enabled = False


Re: Print message when same button is pressed

Posted: Sun Sep 26, 2021 12:27 am
by soggycashew
UPDATE: I added a while True and a pause then tabbed everything over once and ran and it ran! I'm so excited, I finally have a starting point! I keep looking at the code and basically from what I'm getting is your passing a 1 or 0 around and if its a 1 its pressed and 0 is not pressed.

Is there a good tutorial I can read? Or a book I should get? I would love to learn a little bit more with buttons etc. I would also like to know what the % represents?
% 2 == 1

Here is what I added is it correct? Thanks

Code: Select all

import machine
import utime
 
B1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
B2 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(12, machine.Pin.OUT) 

def nasty_buzzer():
    print("nasty buzzer")
    # I think the standard tutorial on a buzzer is milliseconds on / milliseconds off..
    # I'll leave it for someone if they want to look it up.	

button1_counter = 0
button2_counter = 0

while True:
    if B1.value(): # was the B1 button pushed?
        button1_counter += 1
        button2_counter = 0   # Force the toggle (reset the other side)
        if button1_counter % 2 == 1:  # Message head to button 2 on odd pushes
            print("Goto Button 2")
            utime.sleep(0.25)
        if button1_counter %2 == 0:  #Pushed it twice kick to buzzer 
            print("You double pressed Button 1!")
            nasty_buzzer()
            print("Goto Button 2!")
            utime.sleep(0.25)

    if B2.value(): # was the B2 button pushed?
        button2_counter += 1
        button1_counter = 0   # Force the toggle (reset the other side)
        if button2_counter % 2 == 1:  # Message head to button 1 on odd pushes
            print("Goto Button 1")
            utime.sleep(0.25)
        if button2_counter %2 == 0:  #Pushed it twice kick to buzzer 
            print("You double pressed Button 2!")
            nasty_buzzer()
            print("Goto Button 1!")
            utime.sleep(0.25)