0

I’m trying to write a program that will loop through 3 GPIO pins connected to an external charging pad and see if a device has been placed on the pad (high voltage). If high signal is detected the program will stop at that pin. Is there a way to wait indefinitely at a pin using the time library? How would I go about continuously checking the 3 pins? I have written some starter python code to try to figure this out which I will provide.

class switch(object):
    def __init__(self, value):
        self.value = value
        self.fall = False

    def __iter__(self):
        """Return the match method once, then stop"""
        yield self.match
        raise StopIteration

    def match(self, *args):
        """Indicate whether or not to enter a case suite"""
        if self.fall or not args:
            return True
        elif self.value in args: # changed for v1.5, see below
            self.fall = True
            return True
        else:
            return False

PAD1 = 17
PAD2 = 27
PAD3 = 22

for case in switch(PAD1)
    if case(GPIO.output(17, true))
        wait
        break

for case in switch(PAD2)
    if case(GPIO.output(26, true)) 
        wait
        break

for case in switch(PAD3) 
    if case(GPIO.output(22, true))
        wait
        break 

In using the switch class I am trying to get the program to switch from pin to pin. Wondering if this is sound logic to follow to achieve this.

  • 1
    what does program will stop at that pin mean? .... you have not explained what the program is supposed to do – jsotola Mar 22 at 1:15
  • Program execution will halt when high voltage is detected at the specific pin (maybe like sleep method in the time library). What I cannot get my head around is how to make the program stop indefinitely at the pin. Sleep only allows the program to stop for x amount of seconds. – Kelvin A. Vasquez Mar 22 at 1:17
  • 2
    that is a puzzling thing ..... what is the point of a program that waits for an event and then stops and does nothing when the event occurs ..... anyway, for your question, just enter a do nothing loop – jsotola Mar 22 at 1:25
  • This (incomplete) code is very confused (not helped by the use of case & switch - which are reserved words in most languages). Rather than polling use a wait for interrupt function. See raspi.tv/rpi-gpio – Milliways Mar 22 at 1:31
  • 3 separate loops/statements for each respective pin? – Kelvin A. Vasquez Mar 22 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.