Controlled garage door
For this week assignment I had several ideas, I wanted to do something regarding home automation and I was going to make a smart home, and I also thought about making an RC car, but I combined the two ideas and decided to build a controlled garage door and a small car to enter the garage.
Circuito.io
Arduino IDE
Bread Board
Jumpers
Adaptor 9V or 5V
DC Motor *2
Servo motor
Arduino UNO
Crocodile wires
LCD screen 16*2
ON/OFF switch
L298 Dual H-Bridre
The circuit
I then connected my components to the arduino and breadboard and started to write down the code logic and the code.
The logic for the code
Code Sequence and logic:
So what I Originally wanted to do is open the garage door using the arduino and drive the car to enter the garage and then print the status on the screen.
First I included the libraries I'm going to use (servo and LCD),
defined my servo and named it to use it easily,
defined the data I'm going to recieve from the mobile using the blutooth module, and then the pins of the motor driver connected to the motor.
Then in the setup section I started to set up the pins as output and the pin for the servo and initial position and initialized the LCD, and started the serial communication for when I use the blutooth.
In the Loop section I started to write my code sequence:
When the serial communication is done and some data iss recieved by the blutooth module it should store the data in the "incomingData" variable so that I can take action upon the recieved data.
If the data is equal to '1' the car (aka the two motors connected to the motor driver) should drive forward.
If the data is equal to '2' the car should drive backward.
If the data is equal to '3' the car should stop.
And to control the garage door we have '4' to open the garage and '5' to close it.
The data is recieved from the mobile using the application for the communication to the blutooth.
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo garageDoor;
char incomingData = '0';
int motor1pin1 = 2;
int motor1pin2 = 4;
int motor2pin1 = 3;
int motor2pin2 = 5;
void setup(){
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.noBacklight();
garageDoor.attach(6);
garageDoor.write(0);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor1pin1, OUTPUT);
}
void loop(){
while(Serial.available()==0);
incomingData = Serial.read();
Serial.println(incomingData);
if (incomingData == '1'){
//move car forward
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("car is forward"); // print on screen
}
else if (incomingData == '2'){
//move car backward
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("car is backward");
}
else if (incomingData == '3'){
//stop car
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("car stopped");
}
else if (incomingData == '4'){
//open garage door
garageDoor.write(150);
Serial.println("open");
lcd.clear();
lcd.backlight();
lcd.setCursor(0,1);
lcd.print("Garage Door open");
}
else if (incomingData == '5'){
//open garage door
garageDoor.write(0);
Serial.println("closed");
lcd.clear();
lcd.backlight();
lcd.setCursor(0,1);
lcd.print("Garage Door closed");
}
}
garage (enclosure needs finishing)
Small car (2 modes: forward & backward)
In the end of week session in my team i took the coding part and it wasn't working because of a simple mistake that we didn't see at first, after debugging we figured it out, it was that we put numbers instead of character in the recieved data.
The challenges I faced were related to the small RC car, first l only had 1 small dc motor in the kit so l had a small toy that l broke before and took the dc motor from it.
I also didn't have wheels so i made them out of bottle caps.
Then l had a problem with the adapter that l later found out it was from the power outlet, the 5 volts adaptor supplied the voltage to the motors but didn't always start imidiatly, when l tried the 9V adapter the car starts immidiately and so fast that l sometimes lose control of the car, but then l changed the power outlet and it worked fine.
Title of Media