Good Afternoon,
I want to make a project about automatic gate and garage door via Android app and connected by HC-05 bluetooth module.
Each of the gate and garage door have two limit switches(located at each end of travel) to trigger the DC motor to stop.
I have made some code and do researches, but the DC motor will only stop when I pressed it, if I release it again it will start automatically. I want to make it stop until the bluetooth module send the code again.
I have input the pull-down resistor into the limit switch.
Can anybody please help me?
Here's my current code, thank you
// Motor for Gate (MotorA)
int IN1 = 2;
int IN2 = 3;
int EnableMotorA = 9;
// Motor for Garage Door (MotorB)
int IN3 = 4;
int IN4 = 5;
int EnableMotorB = 10;
const int LimitSwitch1 = A1;
const int LimitSwitch2 = A2;
const int LimitSwitch3 = A3;
const int LimitSwitch4 = A4;
int state;
void setup(){
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(EnableMotorA,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(EnableMotorB,OUTPUT);
pinMode(LimitSwitch1, INPUT);
pinMode(LimitSwitch2, INPUT);
pinMode(LimitSwitch3, INPUT);
pinMode(LimitSwitch4, INPUT);
digitalWrite(LimitSwitch1, LOW);
digitalWrite(LimitSwitch2, LOW);
digitalWrite(LimitSwitch3, LOW);
digitalWrite(LimitSwitch4, LOW);
Serial.begin(9600);
}
void loop(){
if(Serial.available() > 0){
state = Serial.read();
}
if(state == 'A') {
OpenGate();
if(digitalRead(LimitSwitch2) == HIGH){
StopMotorA();
}
}
else if(state == 'B') {
CloseGate();
if(digitalRead(LimitSwitch1) == HIGH){
StopMotorA();
}
}
else if(state == 'C') {
OpenGarageDoor();
if(digitalRead(LimitSwitch4) == HIGH){
StopMotorB();
}
}
else if(state == 'D') {
CloseGarageDoor();
if(digitalRead(LimitSwitch3) == HIGH){
StopMotorB();
}
}
}
//****************** Motor A control *******************
void OpenGate(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EnableMotorA, 250);
}
void CloseGate(){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EnableMotorA, 250);
}
void StopMotorA(){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(EnableMotorA, 0);
}
//****************** Motor B control *******************
void OpenGarageDoor(){
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EnableMotorB, 250);
}
void CloseGarageDoor(){
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EnableMotorB, 250);
}
void StopMotorB(){
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(EnableMotorB, 0);
}
//**********************************************************