TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: AHEM A RIVET`S SHOT
from: ALISTER
date: 2018-02-20 19:57:00
subject: Re: How to use while loop

On Tue, 20 Feb 2018 18:22:49 +0000, Ahem A Rivet's Shot wrote:

> On Tue, 20 Feb 2018 00:22:32 -0500 Dennis Lee Bieber
>  wrote:
>
>> On Mon, 19 Feb 2018 19:08:31 -0800 (PST), ewholz@gmail.com declaimed
>> the following:
>>
>>
>> >I leave the program running, and detect when door opens, and when door
>> >closes - but desire just one line of "OPEN" or "CLOSE"
>> >
>> >
>>  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...
>
>  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

I think you may have reversed newState & lastState




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

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.