Hardware Reference
In-Depth Information
# this detects when the beam has become un-broken again.
# That is when the beam was broken and it is not broken
# any longer. When this occurs, record the new state and
# log it to file
if (innerBeamStateWasBroken and not innerBeamIsBroken):
innerBeamStateWasBroken = False
logEvent(0,0)
#print “inner beam has been un-broken”
##handle Outer Beam, with same structure as inner
if (not outerBeamStateWasBroken and outerBeamIsBroken):
outerBeamStateWasBroken = True
logEvent(1,1)
#print “outer beam has been broken”
if (outerBeamStateWasBroken and not outerBeamIsBroken):
outerBeamStateWasBroken = False
logEvent(1,0)
#print “outer beam has been un-broken”
he structure of Python programs should be familiar to you by now, but it is worth consider-
ing new concepts introduced and reinforcing others.
Constants
Some languages (such as C) have constants to hold values that do not change over the lifetime
of a program. hey allow programmers to deine the value of a constant in one place and then
refer to it throughout the program. If the programmer updates the program, the value needs to
be changed in only one place. his is easier than searching through the code looking for all
instances of the value to change. In Listing 17-1 the pin number of the PiFace interface that is
connected to the beams is deined in INNER_BEAM and OUTER_BEAM . As such, if the circuit is
changed, the value needs to be updated in only one place in the program. his is part of pro-
gram design for the future. It takes into consideration that the beams may be connected to
other pins and so makes it easy for them to be changed in one place at the top of the program.
Unlike other programming languages, Python does not really have constants. However, the
convention is to use variables with all uppercase names instead.
Detecting Changes of State
he variables innerBeamStateWasBroken and outerBeamStateWasBroken are ini-
tialised to False . hese variables are used to detect if the state of the input pin has changed,
so the event of a beam changing can be recorded.
 
Search WWH ::




Custom Search