On Tuesday, February 20, 2018 at 10:30:05 AM UTC-8, 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
>
> --
> 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/
Another good suggestion - i did not expect so many replies! Thanks
--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)
|