Wednesday, May 6, 2015

Final Project Week 2

During our second week working, we focused on getting our code right, which took several tries. Below, I'll detail the steps we took to get to our final code that will be used to make the toilet flush.

Goal: Just to be clear, I'd like to briefly outline what it is we were trying to get our code to do so that the photos and descriptions below might be easier to understand. Initially, we want our code to do nothing. However after a child presses a button, we would like the ultrasonic sensor to begin 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. Additionally, if the sensor does not sense a child, it will flush immediately. Note that the original flush handle attached to the toilet will still work on its own if need be.

That being said, our first iteration of the code was broken down into four parts. First, there is a while statement addressing the situation in which the button is unpressed. The motor stays off during this. Then, once the button is pressed the code breaks out of the while statement and follows a series of if statements. The first if statement adresses the situation when the time is less than two minutes and there is a child in the stall, the toilet should do nothing. Second, if it has been less than two minutes and there is no child in the stall, the motor will flush the toilet. Lastly, if the time is greater than two minutes the toilet will flush, regardless of whether or not there is a child in the stall.


However, this code failed to work when we tested it. The  ultrasonic sensor would only read values when the button was being held down, instead of just after it was pressed. Or, alternately, depending on where we put certain parts of the code, 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. 

At this point we were concerned with whether or not our button was working correctly on its own. So we made a new code to simply test our button by making the motor function solely as a function of the state of the button. 

As it turned out, our button worked correctly. This told us that our problem was with the Millis function. So, we decided to remove that piece of the code and simply use a delay instead. In addition, we also simplified our code from being in four parts to three. Instead of having three separate if statements according to the time, we now only have two that are associated with only the value of the sensor reading. Our code now states that if there is no child in the stall, flush, and if there is a child in the stall, wait a certain amount of time and then flush. This simplification took us a great deal further on the road to our final iteration. 


Our final code, below, looks similar to the one above but with some adjusted values. After talking to a director of the preschool, we determined the child only needs ten seconds to exit the stall, so that was adjusted in our code. Here is our final version:


For my next post I will discuss the physical setup of our prototype and the different aspects of making it come together.

1 comment:

  1. Not sure if I posted before with the same starting comment, but I really like the format of your side bar! It is easy to pick up the post that I want to see. And looking at your code, it's cool that your team used millis() to determine the seconds of delay before the child leaves the restroom. If we had more time to do our project, I think we would have liked to use the millis() function too, and could have gotten tips from your post!

    ReplyDelete