At this point in time we did not have a lot of materials yet (button, motor, ect.) as they were still being shipped, so we focused on completing the Arduino code that would allow the button, ultrasonic sensor, and motor to communicate in the way we wanted them to (to flush the toilet).
The actual coding process was done step by step to correctly incorporate each of these three devices, but ultimately we wanted a code that would keep the motor at a stationary position until the button was pressed. Then, once the button was pressed, the ultrasonic sensor would start reading values. If the sensor reads a value that indicates a child is present, the toilet should do nothing for a certain amount of time, allowing the child to leave the stall, and then flush. Alternatively, if the sensor doesn't sense a child, it will flush immediately. This whole process would be independent of the toilets handle's normal function, so the toilet can still be flushed manually if need be.
The first version of our code had statements for all three devices, and four main functions it performed. The first function was a "while" statement that set up the default setting of the motor as "off." The next three functions fell under the condition that the button was pressed. Once the button is pressed the code breaks out of the "while" statement and does one of three things: (1) if the time is less than two minutes and there is a child in the stall, the motor does nothing, (2) if it has been less than two minutes and there is no child in the stall, the motor will flush the toilet, and/or (3) if the time is greater than two minutes the toilet will flush, regardless of whether or not there is a child in the stall.
At the point in time when we made this code, we did not have a motor to test on, but we could tell this code would not work as the ultrasonic sensor would only read values when the button was being held down, instead continuously after it was pressed. We tried putting "serial print" function that communicated sensor reading values in different sections of the code, but, depending on where we put it, the sensor would read one value when the button was pressed and wait until the code had run through its full loop to read a second value.
Not too long after creating our first code, our motor device came in, so we opted for removing the sensor portions of our code until we made sure that the button/motor were communicating properly.
The above code as able to correctly move the motor the way we wanted to. This allowed us to determine that the previous issue regarding our sensor readings was probably the result of the Millis functions we used. We ended up removing them from our code, and using delay functions in their place. When editing our code, we also realized that we technically only needed two statements for when the button is pressed rather than three (one for if the child is still in the stall, and another for when no child is in the stall).
The resulting code worked roughly in the way we wanted it to. All that was left was to fine tune the sensor values and change the delay time of the flush to 10 seconds. These values were based on our previous visit/meeting to the preschool, which is detailed in the previous post. Our final code is as follows:
Tune in next week to see the physical setup of our device!
No comments:
Post a Comment