Brian
Brian

Reputation: 17

Raspberry Pi Pico (Micropython)

I am trying to write a loop to read pin 14 on a pi pico. Normally the pin will be 0 (Low) but when it goes 1 (High) I would like to exit the while loop and continue with the rest of the script. I have it running and it is printing "HELLO" but when I put a high on pin 14 it doesn't "break". What I have just for this while portion is as follows:

import machine
from machine import Pin
import utime
from machine import Timer

#Create input for Pin 4
p14 = Pin(14, Pin.IN)

#Set Pin 14 Value Low Initially
#p14.value(1)

#(Section is for reading Input High on Pin 4)
check = (p14.value())
print(p14.value())
while check != 1:
  check2 = (p14.value())
  print("HELLO")
  if check2 == 1:
      print("Pin 14 went HIGH")
      break

I've tried numerous iterations of while but still cannot get this to work.

Upvotes: 0

Views: 568

Answers (1)

Gusisin
Gusisin

Reputation: 49

You could try and set up a call back so that when the pin changes state, an action occurs:

from machine import Pin

interrupt_flag=0
pin = Pin(5,Pin.IN,Pin.PULL_UP)
def callback(pin):
    global interrupt_flag
    interrupt_flag=1

pin.irq(trigger=Pin.IRQ_FALLING, handler=callback)
while True:
    if interrupt_flag is 1:
        print("Interrupt has occured")
        interrupt_flag=0

Upvotes: 0

Related Questions