Hardware Reference
In-Depth Information
Listing 17-1 continued
#used to know if a new event has occurred
innerBeamStateWasBroken = False
outerBeamStateWasBroken = False
#function to return the current time, formatted as
# e.g. 13 Jun 2013 16:07:30 572
def getFormattedTime():
now = datetime.datetime.now()
return now.strftime(“%d %b %Y %H:%M:%S. “) + ;
str(int(round(now.microsecond/1000.0)))
#generate and record an event to file
def logEvent(sensor, state):
logFile.write(str(sensor) + “,” + str(state) + ;
“,” + getFormattedTime() + “\n”)
#turn on IR LED emitters
p.digital_write(INNER_BEAM_EMITTER,1)
p.digital_write(OUTER_BEAM_EMITTER,1)
#open a file for appending, do not buffer
logFile = open('birdlog.txt', 'a', 0)
#indicate the point the program started in the log
logFile.write(“###starting up at:” + getFormattedTime() + “\n”)
#main loop of the code
while (True):
#read the current state of the beam
innerBeamIsBroken = (p.digital_read(INNER_BEAM) == 0)
outerBeamIsBroken = (p.digital_read(OUTER_BEAM) == 0)
##handle Inner Beam
# if the beam has become broken, that is if the beam
# was not broken before but is now,
# then record that the beam was broken and log to file
if (not innerBeamStateWasBroken and innerBeamIsBroken):
innerBeamStateWasBroken = True
logEvent(0,1)
#print “inner beam has been broken”
Search WWH ::




Custom Search