Step 10: Software
Main Loopthe main program runs through this code, checking for a reason to play a sample;
seatstatus := seatposition
lightstatus := checklight
IF (lightstatus == 1 AND seatstatus == 1)
IF (lightstatus == 0 AND seatstatus == 1)
IF (lightstatus == 3 AND seatstatus == 0)
If lightstatus == 1 and seatstatus == 1, the seat is up and the light is on. Toilet buddy goes ahead and plays cover.wav. If the light is off and the seat is up, seat.wav is played.
If lightstatus == 3, that means the light timeout has been reached. Then it plays the light.wav file.
checklightThis method checks if the bathroom light is on. It uses the rctime object (part of the Propeller Tool download) to accomplish that. It charges up the capacitor connected to the photoresistor and counts how long it takes to discharge with a single line;
The variable RCValue will store the discharge time. If it's greater than the limit we set, that means not very much light is hitting the photoresistor, and we return 0. If more light is hitting it, we increment the light timer and return 1, until we reach the light timer limit.
seatpositionWe check the status of the seat with this method. We use ir object to accomplish this, which is also part of the Propeller Tool download. First, we have to tell the ir method what pins the IR LED and receiver are connected to - we do this at the beginning of the program with;
ir.init(IRAnode, IRCathode, IRSensor)
Then, when we call ir.distance, it will return the measured distance of an object in front of the sensor.
playaudioThis method actually takes care of the audio playback. It's a simplified version of rayman's wav player. First, it opens up the file we requested. Then it reads the header to see if the wav file is 44khz or 48khz. Then it starts up the wav player in a second core. Our first core then fills the audio buffer for the second core.
The important thing to keep in mind is this player will only playback 16-bit stereo wavs. Also, the headers on the wav file need to be empty so the player can find the sample rate.