How to use while loop to detect one of two events, event a, or event b

Hello Group,

I found a nice program for detecting if a door is open or closed. The example I am using works, but it continually loops if the door is open, writing "Door Open" to my log file. It writes just one event of "Door Closed" (which is what I want).

I would like the program to be running all the time, and if the door is opened write "Door Opened at [time, etc]" one time.

And when the door is closed - write "Door Closed at [time, etc]"

Here is the code I am currently using: while True:

## if the switch is open if io.input(door_pin): io.output(pin,1)## Switch on pin 25 foo.door_event(door_pin) ## foo writes to a file ## logger.log("Door","Open") # stream a message saying "Open" ## logger.flush() # send the message immediately door=0 # set door to its initial value of zero time.sleep(1) # wait 1 second before the next action ## if the switch is closed and door does not equal 1 if (io.input(door_pin)==False and door!=1): io.output(pin,0) ## Switch off pin 25 time.sleep(speed / 2) ## Wait foo.door_event(0) ## logger.log("Door","Close") # stream a message saying "Close" ## logger.flush() # send the message immediately door = 1 # set door so that this loop won't act again until the switch # has been opened

I leave the program running, and detect when door opens, and when door closes - but desire just one line of "OPEN" or "CLOSE"

any tips will help,

thanks

ewholz

Reply to
ewholz
Loading thread data ...

Here is how to do it using Tcl instead of Python (based on what I did you should be able to translate it to Python("

package require piio

## ## The program is run as: tclsh door.tcl ## where is replaced by the real PIIO pin number ## set inputPin [lindex $argv 0]

piio function $inputPin input piio pull down $inputPin

piio event $inputPin rising [list puts stdout Open] piio event $inputPin falling [list puts stdout Close]

vwait forever

--
+----------------------------------------------------------------------+ 
| Gerald W. Lester, President, KNG Consulting LLC                      | 
| Email: Gerald.Lester@kng-consulting.net                              | 
+----------------------------------------------------------------------+
Reply to
Gerald Lester

On Mon, 19 Feb 2018 19:08:31 -0800 (PST), snipped-for-privacy@gmail.com declaimed the following:

Without providing any actual code...

You need to keep a "history" state... AND you need to use an IF/ELSE-IF structure.

newState = readPin() if lastState == closed and newState == opened: print opened elif lastState == opened and newState == closed: print closed lastState = newState

Using two IF blocks, without an ELSE block means the second IF gets tested even after you execute the contents of the first IF -- and the first IF (in your code) has set the history such that the other IF triggers and outputs...

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

Actually the way you have it written the state is not set until after both ifs so you could use an if instead of an elif (but the elif is more efficient FWIW) - an alternative construction that makes it really obvious that the trigger for printing is a state change goes like this:

newState = readPin() if lastState != newState: if newState == opened: print "Opened" else: print "Closed" newState = lastState

--
Steve O'Hara-Smith                          |   Directable Mirror Arrays 
C:\>WIN                                     | A better way to focus the sun 
The computer obeys and wins.                |    licences available see 
You lose and Bill collects.                 |    http://www.sohara.org/
Reply to
Ahem A Rivet's Shot

I think you may have reversed newState & lastState

--
Keep emotionally active.  Cater to your favorite neurosis.
Reply to
alister

________^^^^^^^^^^^^^^^^^^^

definitive.

The problem with computers is, that they always do what we tell them and not that, what we want (origin unknown).

Regards, Julius

--
I think, an old Windows has old bugs, 
a new Windows has new bugs and 
other operating systems have other bugs.
Reply to
Julius Kavay

print(last==readpin()?((last=readpin()==OPENED)?"Opened":"Closed"):"");

I knew one line was possible!

--
There?s a mighty big difference between good, sound reasons and reasons  
that sound good. 

Burton Hillis (William Vaughn, American columnist)
Reply to
The Natural Philosopher

Apart from the case when the pin changes state between your two reads ..

Reply to
Andy Burns

Damn. :-)

Been there, done that ...."Your code has a bug - every twenty minutes or so it simply locks up"...

Timer interrupt between two statements in the entire OS would do just that..

Thank god for ICE's

Friend worked on the original ARM OS. Itr would lock up ebvery ciuple of hours. They fund a race coindition., A wait state made it lockup up every couple of weeks. So they added two wait states, and that made it only once every ten years 'At which point they will just put it down to cosmic rays and reboot it anyway' he said 'if anyone was using the computer switched on that long'

Doesn't the pi have GPIO interrupts?

--
All political activity makes complete sense once the proposition that  
all government is basically a self-legalising protection racket, is  
fully understood.
Reply to
The Natural Philosopher

I presume it does, but let the O/P walk before they run ...

Reply to
Andy Burns

On Wed, 21 Feb 2018 07:10:30 +0000, The Natural Philosopher declaimed the following:

That's not Python. Assignment is not an expression in Python but a statement.

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

Besides, the while loop somehow vanished into thin air.

--
Martin    | martin at 
Gregorie  | gregorie dot org
Reply to
Kiwi User

Oh sorry. I thought we were talking 'C'

--
"The great thing about Glasgow is that if there's a nuclear attack it'll  
look exactly the same afterwards." 

Billy Connolly
Reply to
The Natural Philosopher

well that can all be on a single line, in C..

In fact a whole program can be a single line, as the compiler skips white space: All that matters is the semicolon...

So while (1) printf(last==readpin()?((last=readpin()==OPENED)?"Opened":"Closed"):"");

is a single line while loop

--
"The great thing about Glasgow is that if there's a nuclear attack it'll  
look exactly the same afterwards." 

Billy Connolly
Reply to
The Natural Philosopher

You could have crafted it in such perl that nobody could tell you there were bugs. :-)

Reply to
Rob Morley

On Wed, 21 Feb 2018 17:44:04 +0000, The Natural Philosopher declaimed the following:

The OP code -- while rather dense looking (tab those comments over) -- was obviously Python...

--
	Wulfraed                 Dennis Lee Bieber         AF6VN 
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/
Reply to
Dennis Lee Bieber

Not in python though which all the earlier examples are.

-- Steve O'Hara-Smith | Directable Mirror Arrays C:\>WIN | A better way to focus the sun The computer obeys and wins. | licences available see You lose and Bill collects. |

formatting link

Reply to
Ahem A Rivet's Shot

Doh! It's an example of the importance of code review .

-- Steve O'Hara-Smith | Directable Mirror Arrays C:\>WIN | A better way to focus the sun The computer obeys and wins. | licences available see You lose and Bill collects. |

formatting link

Reply to
Ahem A Rivet's Shot

I thought it was a deliberate error to weed out those who simply want an answer from those who want true understanding ;-) (that would have been my excuse anyway)

--
His ideas of first-aid stopped short of squirting soda water. 
		-- P.G. Wodehouse
Reply to
alister

Reply to
ewholz

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.