Remember last week ! yeaaah.. now it's time to add some details to it.
It came up to my mind while thinking of the assignment to make a piggy bank or coin box that opens when I get near it.
I searched for similar projects like: coin box
But I decided to make it using IR sensor instead of the method shown in the link.
software
I used Arduino to burn the code to arduino uno to start the trial physically
I used tinker cad to test and simulate the circuit before prototyping it physically to protect the electronics from burning or any problem
tools and materials
From left to right =D:
1- Compass
2- Hot glue gun
3- Cutter
4- Pen
5- Scissors
6- Cardboard
7- Ruler
electronics
arduino uno + usb cable
IR sensor
servo motor
breadboard
I was using IR sensor in week 6 to detect if I'm near the box as an input, so it makes the servo motor moves to open the coin box as an output then closes it after 5 secs.
This week I upgraded it to make the IR sensor just opens the coin box and when I'm done I push the button as the other input to close the box again.
Now, for the other output I added White LED turns on as an indicator when I'm near the box and the box is open, and when I push the button it turns off.
At first, I opened tinker cad and started to put the electronics or tools I need then I started to wire it:
1- PIR sensor(INPUT): VCC ' red wire ' to +
GND ' black wire ' to -
out ' orange wire ' to pin 4
2- servo motor(OUTPUT): Power ' red wire ' to +
ground ' brown wire ' to -
signal ' green wire ' to pin 3
3- push button(INPUT): terminal 1 to GND
terminal 2 to pin 5
4- LED(OUTPUT): long leg + to pin 6
short leg - to resistor
then resistor 330 ohm to - to GND
I wired my components following the wiring diagram in Tinker CAD
I started to put my electronics on the cardboard and mark with my pen to set the dimensions I need then I mount them, I started with arduino Uno and small breadboard at the bottom , then the IR sensor mounted to the top
I continued to mount the components so I marked where I should open for my: pushbutton, LED , IR sensor and USB port, in the front of the box
Last but not least I mounted the servo and shortened its wires and set them in the back.
I used divide and conquer technique to build my code:
I already had the code of week 6 with servo and IR sensor
I edited it to make the IR only opens the box using:
if (digitalRead(4) == LOW) {
servo.write(60);
}
I uploaded it and checked it's ok, then moved to the next step by adding push button as an INPUT_PULLUP on pin 5 and made it close the box again:
if (digitalRead(5) == LOW) {
servo.write(0);
}
Second upload and check... All good !
Great, now adding LED as an OUTPUT on pin 6 and make it turn on when IR senses obstacle and off when I press the push button:
if (digitalRead(4) == LOW) {
servo.write(60);
digitalWrite(6,HIGH);
}
if (digitalRead(5) == LOW) {
servo.write(0);
digitalWrite(6,LOW);
}
Testing physically
I had a problem after finishing integration and connected my arduino that the servo was set to wrong angles and was open all the time.
I had 2 options to solve this: 1- is to remount the cover correctly on the servo horn
2- is to fix my code
and I choose to fix the code
the wrong and first code was:
if (digitalRead(4) == LOW) {
servo.write(45);
digitalWrite(6,HIGH);
}
if (digitalRead(5) == LOW) {
servo.write(90);
digitalWrite(6,LOW);
}
and the right and last code is:
if (digitalRead(4) == LOW) {
servo.write(60);
digitalWrite(6,HIGH);
}
if (digitalRead(5) == LOW) {
servo.write(0);
digitalWrite(6,LOW);
}
Second problem was that I first coded if (digitalRead(5) == HIGH)
So, when the IR detects obstacle moves the servo to 60 ... but the push button at the same time moves the servo to 0 .. so the servo was shaking between the two states
I realised that it could be a problem in this line and the push button sends HIGH when it's not pressed .. and if I push it, it sends LOW. So I changed this line to: if (digitalRead(5) == LOW) and reuploaded the code. PROBLEM SOLVED.
It helped me more with mounting my components in the enclosure and I used my push button I will be using in my final project ^_^