tom90
Posts: 2
Joined: Mon Apr 16, 2018 11:17 am

How to print once on switch activation

Mon Apr 16, 2018 11:26 am

Hello all,

Total newbie to Pi and coding, I have plans on wiring my new house with the Pi and am trying to learn the basics.

I am attempting to wire a Reed switch and when it is activated turn an LED on and print the time and date that it was activated.
I have successfully been able to do this but it will continuously print the statement out until the reed switch is re-opened.

How can I re-work my code so it will only print the statement out once and then re-print it with the updated time once each time the reed switch switch is closed.

my code is:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(38, GPIO.OUT)
GPIO.output(38, 0)

localtime = time.asctime( time.localtime(time.time()) )

try:
while True:
if GPIO.input(7):
GPIO.output(38, 1)
print(localtime)
time.sleep(.5)
else:
GPIO.output(38, 0)
time.sleep(.5)


except KeyboardInterrupt:
print("Program Stop")
GPIO.cleanup()


Thank you for your help!
Tom

pfletch101
Posts: 106
Joined: Sat Feb 24, 2018 4:09 am

Re: How to print once on switch activation

Mon Apr 16, 2018 1:42 pm

It is a little difficult to read Python code without the necessary indentation! This is one of the many reasons to put code in a code block.
You probably want something like:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(38, GPIO.OUT)
GPIO.output(38, 0)

localtime = time.asctime( time.localtime(time.time()) )
wasoff = True
try:
    while True:
        if GPIO.input(7):
            if wasoff:
                GPIO.output(38, 1)
                print(localtime)
                wasoff = False
            time.sleep(.5)
        else:
            wasoff = True
            GPIO.output(38, 0)
            time.sleep(.5)


except KeyboardInterrupt:
    print("Program Stop")
    GPIO.cleanup()
The extra variable wasoff stores the current state of the line, so that the conditional code you want to execute only gets activated when the state of the line you are watching changes to True.

Edited 14:00 CDT to prevent 'hard looping' when GPIO.input(7) is True.

tom90
Posts: 2
Joined: Mon Apr 16, 2018 11:17 am

Re: How to print once on switch activation

Tue Apr 17, 2018 3:00 am

Awesome, Thanks pfletch101 that worked exactly as I wanted it to!

pfletch101
Posts: 106
Joined: Sat Feb 24, 2018 4:09 am

Re: How to print once on switch activation

Tue Apr 17, 2018 1:49 pm

You're welcome!
It could be argued that the else: clause should also be modified similarly, i.e.:

Code: Select all

...
            if not wasoff:
                wasoff = True
                GPIO.output(38, 0)
            time.sleep(.5)
...
The revised code is marginally more efficient, and it is probably better practice only to 'touch' an output port if you need to.

Return to “Beginners”

Who is online

Users browsing this forum: gkreidl, PI_Ninja and 59 guests