This week’s assignment was to implement a smart circuit like the previous week but this time the circuit communicates wirelessly via Bluetooth module to a PC/ phone. This week honestly took me a lot of time to figure out what to do , it’s quite difficult to come up with an idea that you think would challenge your capabilities but stills checks all the requirements , anyway this week I was inspired by our week7 exercise , it’s a security system that takes from the user both his id and password then it opens the door after that it checks if there are still people entering using ir sensor when it detects that there is no one the door closes and the user could send on/off to illuminate his apartment . the user has three trails only to enter his passcode .
I used both Tinkercad and Arduino this week . tinkercad was used to implement the circuit but obviously there are some difference between implementing on tinkercad and the real simulation , some are for example :
- Tinkercad doesn’t include some components or there maybe be alternatives for example H-bridge is included as an IC so you have to be careful how to connect its pins for example this ic needs to have both gnd and vcc connected also the enable should be connect in order for the motor to rotate
Also in Arduino I like to make a lot of functions because it gives me a sense of organization and it’s easier to track the code and debug it
To make a function you need to think if you want this function to return/ give you something also if this function needs something for you (variable )
If the answer is yes that the function return something you need to think of the type of data this function returns ( for example int / char / float / string..) and you write the type before the functions name ex: int password ( …) in the two brackets goes the variable that is received while calling the function you should have it declared the same type as the variable it would be receiving ex: int password ( int num) but in case in neither returns or receives then you just put void ex; void password (void)
To call the function if it returns data
You need to equalize it to a variable in the main code and if it sends variable put in between the brackets ex: pass = password(number) ;
In case it doesn’t send variable ex: pass = password () ;
Case it neither returns/sends ex: password();
Also a nice note is that void setup is like what you put in the main in c and the void loop is what you put in the while(1) loop because while(1) is always valid so it would keep repeating like void loop
First I included the library for the liquid crystal ic2 , then I defined the variable led as 10
I also declared variables motor_1 = 6 , motor_2 = 7 which are the two pins extended from the h-bridge to rotate the motor in both directions , the pir is declared as 13 . those variable I would be using during the whole code , it’s better to define variables as numbers instead of directly using the number for a couple of reasons
- If you changed the location of the pin on the Arduino , you have to track and change pin number for each line but simply change the variable declaration to the number you want , also some developers call the usage of numbers as magic numbers and it’s suppose to be unprofessional behaviour .
Wrong trails variable would indicate the number of failed entered id/pass from the user , previous_id is the stored id from the user , if the user want to have multiple id for each family member that could be easily adjusted in the code by making the previous_id and previous_pass two array in the type string that each element in one array corresponds to the pass of the element in the same order .
Incoming _id & incoming_pass are the id and the passcode entered by the user , incoming_state is the state the user wants his lights to be on after entering his home , he could either send on / off
------------------------------------------
In void step up , I initialized the lcd by calling the void functions lcd.init() & lcd.backlight() those functions could differentiate depending on the included library that why you should check the file.h
I called the function serial.begin(9600) the parameter passed in this function is the bound rate which mean bit/sec at which the data is passed in .
Because I would be setting one pin of the motor as high and the other as low to rotate it in one direction , the pinMode is set as OUTPUT , but as the ir sensor would be giving be either low if there is a person still infront of the door or HIGH if there is not ( IR IS ACTIVE LOW ) the pinMode is INPUT
Lcd.print function is used to print something on the lcd in my case I wanted it to print “ WELCOME HOME ” only once that’s why it’s in the voild setup
Then I called function pass_id() ; that would be explained later one
In void loop I called function take_pass()
Function pass_id
I first cleared the lcd using lcd.clear function
Then I set the cursor to row 0 column 0 by lcd.setCursor(0, 0); first parameter is the column and the second is the row
I printed id in the first row and pass in the second row .
----------------------------
Function take pass
First I made a loop which has a condition that while the Serial.aviable() == 0 which means that the serial is sending nothing , if this condition is true it would be stuck in the empty loop forever but when you start sending something the condition won’t be valid anymore and it would continue on with the code .
Incoming_id would be equal to the returned value from function Serial.readString() which reads a string insteads of a character like serial.read ()
The two new functions I used here were (name of string).remove and (name of string).length
In function .remove it take two parameters ( index , count) index means the character that I don’t want position but take into considerations that an index starts from 0 not 1 , count is how many characters I want removed from that index for example if index = 1 and count = 2 in string hello
It would remove the ‘ e’ & ‘l ’ so it would be hlo
I had to use those functions because for some reason the string would print an unknown last character and stores also this unknown character so even if the id/pass would be right it never enter the right pass loop due to this weird character
Then I had to use function length to know the length of the string entered so I could remove the last unknown character but please not that function length starts counting from 1 so the last character’s index would be (name).length – 1
I set the cursor to column 5 row 0 so that it would print the entered id besides the “id:”
Then I created another empty loop so that the code would be withheld unless the user enters something else which is in our case the passcode
same as before
Function check_pass is called
Function check_pass
First there is an condition if both the entered id is equal to the previously stored id and the entered pass is also equal to previously stored pass then
- It clears the lcd
- And prints door open
- Setting motor_1 as high and motor_2 as low indicates opening the door then there’s a 6 seconds delay so that the user entered his apartment but then there is an empty loop the code is stuck in while its satisfies condition that the ir sends low ( means there is still a person infront of the door) then when the ir sends high it gets out of the loop and prints door closes then the motor rotates in the opposite direction by setting motor_2 as high and motor_1 as low
- Call function switch lights
Case that the password/id is wrong
- It increments wrong_trails
- Clears the lcd
- And prints wrong pass/id
- And prints the number of this trail
- In case 1 & 2 it calls functions pass_id
- But in case 3 it prints there is an intruder
Function switching_lights
- It clears the lcd
- Print send on/off for lighting
- Empty loop if serial.available()== 0
- Incoming_state == serial.readSting ()
- And we remove the last character as previously explained
- If the incoming_state == on or ON the leds will be high( on)
- Else it would be low
Vcc & gnd from the Arduino to the breadboard
Lcd connection
- Vcc to the vcc on the bread board
- GND to the GND on the breadboard
- SDA to A4 in Arduino
- SCL to A5 in Arduino
Bluetooth module :
vcc to vcc on breadboard
GND to GND ON BREADBOARD
TXD to RX (pin 0 )
RXD WITH resistor to a point (1) from point (1) to txd (pin1) and from point(1) with a resistor 1k to GND
Ir senor
- Vcc to vcc on breadboard
- GND to GND on breadboard
- Out to pin 13
H-bridge and motor
- Connect the two wired to from motor to out 1&2
- Then connect the 9v power source GND to h-bridge gnd
- And 9v vcc to 12 vcc of the h-bridge
- The 5 v vcc of the h-bridge to the vcc of the breadboard
- Input 1 to pin 6 and input 2 to pin 7 on Arduino
Leds
are connected in parallel the negative legs aligned and the positive leg with a resistor on the other end , the negative leg to the breadboard gnd and the resistor end to the pin 10
This video display the case where the user enters the password wrong two times then enters it right but the sensor didn't detect any object so it would stay open for a while then close
This video display the case where the user enters the password Right from the first time and the sensor detects an object so the door would stay open until the person enters and it doesn't detect an object
I did ask for feedback specially during the hackathon because there where some technical problems while assembling the file
For example
- Only one of the three servo motors did work , initially we thought that the connection was wrong or maybe there wasn’t enough power supply or maybe the code was wrong but after we did check all of this , still nothing changed so we started testing the two servo motors and indeed they were damaged , so maybe next time I would doubt the components before myself 😊
- Another thing that I didn’t take into consideration is that you need to calibrate the servo motor before using it , some ways I found are
· Try to rotate the motor using its axis before usage to the point you want it to rotate 180 from
· You can adjust the min and max degree from function servo.attach(pin, min,max) min and max are in microseconds min usually 540 and max 2400
I did help in coding some parts in hackathon and I tried to explain why we put serial.aviable == 0 as condition to have the code stuck until the user enters a value
Also the functions serial.read() ;
And how to connect the Bluetooth module to the Arduino
This week’s assignment I got stuck a couple of times and honestly it took me hours fixing it
- One was that the string added a unknown character at the end of the entered string so the loop of the right password and id was never entered and I also didn’t want the user to enter it character by character so I fixed it by using function (name of the string).remove( index, count ) and I used function length so that it would return the string length and and I would use it in the index parameter
- Also the motor didn’t rotate for a long time and I didn’t know why , it turns out that I had to connect the 5v channel of the h-bridge to the vcc , I taught that because I am using an external power supply I didn’t need to but turns out i had to .
- Also I mistook the smoke sensor with the pir sensor which did also cosume a lot of time as I am trying to understand if it’s active high or low based on it’s reaction when I put my hand infront of it but it obviously didn’t work so I used an ir sensor ( which is active low)
This week was vey much needed because I was planning on adding a feature as nice to have which is wireless connection to the game so the user can move the board using a smart phone application instead of the joystick also now I can have the two players enter there name and store it in the eeprom so if the same player comes again he would know his previous highest score .
I was very excited while making the robot arm because I was trying to win the competition which unfortunately never happens :)) also the middle servo motor was kinda hectic it did vibrate in a weird way sometimes but overall I enjoyed the experience .