I have a project that requires me to actuate a motor once a specific reading is detected from an ultrasonic sensor.
Basically, a drone comes into range of an ultrasonic sensor and actuates a door via a DC motor. I want the sensor to stop taking readings until the DetectionofObject() method is done. This method will open the door and close the door.
The problem I am running into is that the ultrasonic sensor is constantly reading the sensor value, which causes the motor to open and close the door continually.
Below is my code. I am limited due to my programming knowledge. I can logically write this down on paper, but I am having trouble translating this into code.
// C++ code
#include <Wire.h>
// Motion Sensor Pins & Conversions
const int trigPin = 6;
const int echoPin = 5;
int distanceCm, distanceInch;
long duration;
int distance;
// Linear Actuator Pins
int enA = 9;
int in1 = 8;
int in2 = 7;
// Push Button Pins
const int OpenDoor_But = 2;
const int CloseDoor_But = 3;
// Lockout State
boolean lockout = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Motor Pin modes
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// Push Button Pin modes
pinMode(OpenDoor_But, INPUT_PULLUP);
pinMode(CloseDoor_But, INPUT_PULLUP);
// Setting Initial Position of Motor
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Setting Speed Via PWM 200 out of of possible 0~255
analogWrite(enA, 200);
delay(2000); // Delaying next for 2 seconds
}
/* // Creating Motor Functions
void InitialPosition(){
// Reversing Direction of Motor to close bay door
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Setting Speed Via PWM 200 out of of possible 0~255
analogWrite(enA, 200);
delay(2000); // Delaying next for 2 seconds
}
*/
void DetectionOfObject(){
if(lockout == false && distanceCm <= 30){
digitalWrite(in1,LOW); // Turn on the actuator
digitalWrite(in2,HIGH);
analogWrite(enA, 200); // Setting speed of motor
delay(2000);
// Closing of Bay Door
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
analogWrite(enA, 200); // Setting speed of motor
delay(2000);
lockout == true;
}
}
void PushButtonActuation(){
while(digitalRead(OpenDoor_But) == LOW){ // Open Door
digitalWrite(in1,LOW); // Turn on the actuator
digitalWrite(in2,HIGH);
analogWrite(enA, 255);
Serial.println(digitalRead(OpenDoor_But));
}
if(digitalRead(OpenDoor_But) == HIGH){ // Once open door is unpressed the motor will stop
analogWrite(enA, 0);
}
while(digitalRead(CloseDoor_But) == LOW){ // Close Door
digitalWrite(in1,HIGH); // Turn on the actuator
digitalWrite(in2,LOW);
analogWrite(enA, 200);
}
if(digitalRead(CloseDoor_But) == HIGH){ // Once close door is unpressed the motor will stop
analogWrite(enA, 0);
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
distanceCm = duration * 0.034 / 2; // Conversion to cm
// distanceInch = duration * 0.0133 / 2; // Conversion to in
// If motion sensor detects the drone at 40cms or less open door
// InitialPosition();
DetectionOfObject();
PushButtonActuation();
}
My code is also not formatted in the best manner. Any help is appreciated.