In this project, I want to build a system that can provide a safe environment for kids to play video games at night. This system works in two states, in the normal state, it will detect the position of the parents and adjust the brightness of the light, in the emergency state, it will move the kid to their bed or sound the alarm to attract their parents’ attention.
One day, I saw an interesting gif showing a dog reminding its little owner to stop watching TV and pretend to do homework when the father is opening the door. I realized that all these amazing works done by that smart dog can also be finished by a simple embedded system. Thus, this project is kind of to realize one of my childhood dreams. Generally, this project needs to solve two problems. First, try not to attract the parents’ attention so that they won’t think about entering the room to check whether the kid is sleeping. Second, if the parent is entering the room, take appropriate action to prevent the kid from being caught by their parents.
Finish the overall design for the whole model, including the dimensions, the position of each component, how to realize all functions I want with the components I have, the circuit. Be familiar with all the components I need, like how to use the sound sensor to efficiently get the amplitude of the sound, how to control the servo as fast as possible. Finish the code for each individual module, like get the distance with the ultrasonic sensor, use ADC to read the sound sensor, use PWM to control the servo.
Put everything together, use cardboard and the components I have to build the real model. Put the code together, arrange them properly so that each task can be finished in time.
As shown in the Components figure(the second image), there are 12 modules in this system, details like resistors, LEDs(for sound sensor) and transistor(for buzzer) are not included.
Ultrasonic sensor is used to detect the position of the parents. Directly control the pin to send a trigger signal and receive the echo with pin change interrupt. The trigger signal is only 10uS, which is very short, thus I use a while loop to control its duration. The duration of the echo signal is proportional to the distance, thus I set an extra variable to help distinguish whether the pin change is falling edge or rising edge.
Two servos are controlled by PWM signals generated by timer1. The moving servo is used to move the kid between the game position and bed position. The control servo is used to trigger the control switch or just make some noise.
Buzzer is also controlled by a PWM signal generated by timer2. The buzzer works as the alarm, so it only needs to sound for a short period of time.
The room light LED is controlled by a PWM signal generated by timer1, because I need to adjust its brightness according to the distance. The screen LED is directly controlled by a pin output, as I only need it on or off. The two sound sensors are powered by two pin output, their outputs are directly connected to warning LEDs. As the two sensors draw little current, here I choose to control their power directly with the pin output rather than with MOSFET or transistor. The output of these sensors is only high or low depending whether the sound amplitude is over the threshold, thus I use the output to control the warning LED directly without further processing. There are three switches in this system. The control switch and door switch are read by pin change interrupt, because I need to know exactly when they are triggered. The bed switch is read by the pin state, because I only need to know its state at certain moments, when it is pressed or released is not important. The potentiometer is read by ADC, I use it to control the control servo. By setting OCR0A and letting timer0 work under fast PWM mode, timer0 will overflow every 1ms, and I have a software timer that is accurate to ms with timer0 overflow interrupt.
As shown in the Flow chart figure(the third image), there are two states, normal state and emergency state for this system. The little control part is separate from the two states, thus the system will always read ADC and control the control servo so that we can trigger the control switch or make some noise at any time. In the normal state, the kid is playing the game in their room. The brightness of the room light will be adjusted according to the distance between the parent and the door. The sound sensors are on so that when the kid makes too much noise, the warning LEDs will light up. However, in reality, it is kind of tricky to adjust the threshold appropriately so the warning LEDs will keep blinking at slightly different frequencies according to the noise. Only if the door switch is triggered, the system will enter the emergency state, the room light, sound sensors, ultrasonic sensor, and screen will be closed, the time when the switch is triggered will also be recorded so that the system knows how long have passed since the door is opened. Within 3 seconds, no action will be taken so that the kid still has some time to decide whether they need to save the game or not. In this period, if the control switch is triggered, the alarm will sound and the triggered time will be updated so that the kid has extra 3 seconds to do something else. The control switch can be triggered several times so that the kid has more time, but the alarm will only sound once. Then the moving servo will move the kid to their bed. If the kid is still not in their bed after 2 seconds( or 5 seconds since the triggered time), the alarm will sound(if it hasn’t sounded before). Once the kid is in their bed, the system will wait for 15 seconds since the triggered time and then go back to normal state. If the control switch is triggered in this period, the system will go back to normal state immediately.
As all the functions I want are realized in this project, I think I successfully completed this project. The accomplishment that I’m most proud of is that I successfully made a software timer with timer overflow interrupt, this software timer plays a very important role in this project, many functions will fail without it. One thing I learned a lot from this project is how to make full use of a MCU. Before this project, I did know that Atmega328P has three timers and there are several interrupts related to these timers. But I didn’t realize that many of these functions can be applied at the same time, like the overflow interrupt can still work when we use that timer to output a PWM signal. Reading the datasheet over and over again to figure out which functions can work together and which can not is really a great lesson for me. I would also say that this project is still not entirely completed because I used lots of jump wire to avoid the tricky soldering work. Building a circuit that can work on a breadboard is much simpler, but such a circuit is really fragile. One thing I want to improve is finding a way to extend the time between the parent opening the door and checking the room. In reality, once the parent opens the door, they could see everything in the room and there is almost no time for the kid to take any action, thus the current system is not practical at all. One solution I could come up with at present is to make the door much harder to open, if the parent needs several seconds to open the door, then there is enough time for this system to work.
Arduino UNO Pinout figure https://content.arduino.cc/assets/Pinout-UNOrev3_latest.pdf
Ultrasonic module datasheet https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
Servo SG90 datasheet http://www.ee.ic.ac.uk/pcheung/teaching/DE1_EE/stores/sg90_datasheet.pdf