Two buttons on the Pico W

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Two buttons on the Pico W

Post by scruss » Fri Oct 28, 2022 5:54 pm

It's great when things get worked out.

Here's your code for posterity, in code tags and reformatted using black:

Code: Select all

# Program for working with two buttons.
# Date: october 2022
# Creat : Wim van der Leek with help from a menber of  raspberrystore.nl

import machine
from machine import Pin
import time

# On which pin are the led

led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(4, machine.Pin.OUT)

# On which pin are the buttons

btn1 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
btn2 = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)

btn1_prev = btn1.value()
btn2_prev = btn2.value()

while True:

    if (btn1.value() == 1) and (btn1.value() != btn1_prev):
        btn1_prev = (
            btn1.value()
        )  # Reminber the last state, for if someone who pressed the butoon continius.

        # Calling Twilio for sending a SMS, which is not URGENT

        led1.value(1)
        exec(open("MessageNOR.py").read())
        time.sleep(1)
        led1.value(0)

    if (btn2.value() == 1) and (btn2.value() != btn2_prev):
        btn2_prev = btn2.value()

        # Calling Twilio for sending a SMS, It is very urgent.

        led2.value(1)
        exec(open("MessageUR.py").read())
        time.sleep(1)
        led2.value(0)

    btn1_prev = btn1.value()
    btn2_prev = btn2.value()

    time.sleep(0.1)

Post Reply