I have an Arduino project to create two automatic doors. I use DC motors and limit switch sensors to control the doors' forward and reverse rotation (opening and closing). The open and close positions both have limit switches and initially the two doors are closed, with each one triggering its respective limit switch.
I want the effect to be that when someone approaches, the first door opens and waits for 10 seconds before closing. If someone approaches again during this time, the door will open again. Once the first door is closed, the second door will open. While the second door operates, any approach to the first door will have no effect. When the second door is fully open and reaches its limit switch, an ultrasonic sensor will measure the distance and store the value. Then the second door will close. After the second door is fully closed, an LED ring light will reflect the measured distance value in the number of lit LEDs. Then it will wait for someone to approach the first door again.
However, after writing the code and testing, some problems have arisen:
- The first door's limit switch A0 is in the triggered state, but when someone approaches, the door does not open. The door only opens if someone approaches and activates the limit switch again.
- When the door reaches limit switch A1, it waits 2 seconds before beginning to close. But as soon as it leaves limit switch A1, the door opens again. The door needs to maintain the triggered state of limit switch A1 to close. When the door is closing, it also does not react if someone approaches.
- When the first door returns to limit switch A0, the second door opens. But when it reaches the limit switch B1/C, the door does not stop. The door only stops if limit switch A0 is not triggered, and then limit switch B1/C can be triggered to stop the second door.
- When the second door returns to limit switch B0, the door does not stop.
The Arduino code is provided below, and I hope you guys can help me please.
#include <Adafruit_NeoPixel.h>
int switchAPin = 2; // door open
int switchBPin = 3; // door close
int motorPin1 = 4;
int motorPin2 = 5;
int trigPin = 7; // HC-SR04 trigPin
int echoPin = 8; // HC-SR04 echoPin
int miniFanPin = 9;
int motor12vPin1 = 11;
int motor12vPin2 = 12;
int GarbagetrigPin = A2; // The seconnd HC-SR04 trigPin
int GarbageechoPin = A3; // The seconnd HC-SR04 echoPin
int limitSwitchB0 = A4; // The second door open
int limitSwitchB1 = A5; // The second door open
int limitSwitchC = A5; // The second door open
int ledPIN = 10;
int ledCount = 16; // amount of LED
// HC-SR04 max/min distance
const int minDistance = 0;
const int maxDistance = 50; // in cm
Adafruit_NeoPixel ledRing = Adafruit_NeoPixel(ledCount, ledPIN, NEO_GRB + NEO_KHZ800);
int storedDistance = 0;
bool buttonPressed = false;
bool motorRunning = false;
bool motorDirection = true;
bool miniFanRunning = false;
bool motor12vRunning = false;
bool motor12vDirection = true;
void setup() {
pinMode(switchAPin, INPUT_PULLUP);
pinMode(switchBPin, INPUT_PULLUP);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(miniFanPin, OUTPUT);
pinMode(motor12vPin1, OUTPUT);
pinMode(motor12vPin2, OUTPUT);
pinMode(limitSwitchB0, INPUT);
pinMode(limitSwitchB1, INPUT_PULLUP);
pinMode(limitSwitchC, INPUT_PULLUP);
pinMode(limitSwitchA1, INPUT);
pinMode(GarbagetrigPin, OUTPUT);
pinMode(GarbageechoPin, INPUT);
ledRing.begin();
ledRing.show(); // set LED RING
ledRing.setBrightness(15); // set lumenn 50 (0-255)
}
void loop() {
int delayTime = 80; // each light delay time in ms
int tailLength = 8;
int maxBrightness = 255;
if (digitalRead(switchAPin) == LOW) {
if (motorRunning && !motorDirection && miniFanRunning) { // motor run opposite
// Stop the motor and the minifan
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
delay (2000);
// 12vmotor forward
digitalWrite(motor12vPin1, HIGH);
digitalWrite(motor12vPin2, LOW);
motor12vRunning = true;
motor12vDirection = true;
// the motor and the minifan still stopped when 12v is running
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
while (digitalRead(switchAPin) == LOW) {
delay(10);
}
} else if (!motorRunning){
// detected people around
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration / 29 / 2;
if (distance < 30) {
// start the motor and the minifan
digitalWrite(miniFanPin, HIGH);
miniFanRunning = true;
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
motorRunning = true;
motorDirection = true;
}
}
while (digitalRead(switchAPin) == LOW) {
delay(10);
}
} else if (digitalRead(switchBPin) == LOW) { // Stop the motor when arrive switch B
if (motorRunning) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
// wait 2 second
delay(2000);
// motor run opposite
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
motorRunning = true;
motorDirection = false;
while (digitalRead(switchBPin) == LOW) {
delay(10);
}
}
} else if (motorRunning && !motorDirection) { // motor run opposite
// detected people around
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration / 29 / 2;
if (distance < 30) {
// motor run forward
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
motorRunning = true;
}}
// Read limit switch state
int limitSwitchB0Value = analogRead(limitSwitchB0);
int limitSwitchB1Value = analogRead(limitSwitchB1);
int limitSwitchCValue = analogRead(limitSwitchC);
if (motor12vRunning && motor12vDirection) {
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
// check limit switch B1 or C
if (limitSwitchB1Value < 100 || limitSwitchCValue < 100) {
buttonPressed = true;
digitalWrite(motor12vPin1, LOW);
digitalWrite(motor12vPin2, LOW);
motor12vRunning = false;
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
// Measure distance
long duration, distance;
digitalWrite(GarbagetrigPin, LOW);
delayMicroseconds(2);
digitalWrite(GarbagetrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(GarbagetrigPin, LOW);
duration = pulseIn(GarbageechoPin, HIGH);
distance = (duration / 2) / 29.1; // in cm
if (buttonPressed) {
storedDistance = distance;
}
delay (2000);
digitalWrite(motor12vPin1, LOW);
digitalWrite(motor12vPin2, HIGH);
motor12vRunning = true;
motor12vDirection = false;
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
}
} // check limit switch B0
else if (limitSwitchB0Value < 100) {
if (motor12vRunning && !motor12vDirection) {
digitalWrite(miniFanPin, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
motorRunning = false;
miniFanRunning = false;
// Lights the LED RING according to the stored distance value
int lightedLeds = map(storedDistance, minDistance, maxDistance, 0, ledCount);
for (int i = 0; i < ledCount; i++) {
if (i < lightedLeds) {
ledRing.setPixelColor(i, ledRing.Color(0, 255, 0));
}
}
ledRing.show();
delay(100);
motor12vRunning = false;
}
}
}