Stopping Ultrasonic Sensor from Reading

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.

The lockout variable was an attempt to solve my problem as I referenced another post from a similar problem but was not able to get it to work.

What code in your program is causing the sensor to be triggered and read? When you can find this, you can make it function ONLY when you want it to.

1 Like

Hi, Paul. The above code is where the distance is read from the sensor and the motor then rotates in one direction for 2 seconds and then 2 seconds in the other way. The problem I am having is when the drone is constantly hovering at the set distance the door keeps opening and closing since the reading is less than or equal to 30cm while the drone is hovering. The drone would then land while the door is open (separate control loop) and then close.

What do you mean by making it a function only when I want it to? How do you suggest I do this?

I wrote you about MAKING IT function. But might be easier to make it a function and write code the call the function or not call the function.

I understand what you are saying, although, I want the sensor to be reading constantly except for the time that it detects the object at 30cm or less.

After the reading is true I want the sensor to stop reading for a certain amount of time.

I don’t understand how making it a function would solve my problem. If I make it a function I would need another input ex. A button press to call the function, but I do not want to add another input into the system.

Please let me know if my understanding is lacking.

Do you know how to determine the amount of time?
Make all your reading happen only if a boolean is false.
As soon as you determine the object is 30cm or less, set your boolean to true so the reading will be skipped.
Then as long as the boolean is true, count the time. When the time is up, set the boolean back to false, so the reading can start again.

That's not what you want.

You want to be reading the sensor continuously. The first time the reading drops to 30 or below, do what ever action is required. After that, take no action regardless of the sensor readings until they are again is above 30.

Actually, what you REALLY want is some Hysteresis around that 30cm value. Say 2cm on either side. That way your door won't jitter around due to noise in the readings or small movements in the object being detected.

There's your problem. you meant to write:
lockout = true;

After that line the distance sensor will not open and close the door again. Ever. (Unless you put in some code to set 'lockout' to false again, perhaps when the drone is not in range.)

If you would really deactivate the sensor from reading

what kind of change of whatever would activate the readings again?

de-activating the reading would require some additional hardware.

The final purpose of your wish to de-actiavte the readings is:
stopping the door from open/close/open/close/open/close

**not-**executing door-opening/door-closing is a much better approach

you can modifiy code into any direction and you can make code-execution conditional to almost any kind of conditions. Even hundreds of very different conditions if you like.

Yes this is exactly what you should do!

As soon as you write down in normal words a detailed description of what should happen suggestions how to realise this as code can be made.

best regards Stefan

Yes you are right that is what I would like it to do, but I am struggling with finding the path from paper to implementation of code.

I see I made that mistake, must've been when I copy and pasted some other segment from my code. I will look into this, thank you John.

Valvo, thank you for taking the time to respond.

You are right in terms of what I want it to do. But I am having trouble implementing this solution into code. The point regarding hysteresis is actually a very good one.

omehow to me personally thi answer sounds like hiding away to say "can you write the code for me?"
maybe this is a misinterpretation. Anyway make an

own attempt

how the code could be modified to achieve conditional execution of opening/closing the door.

You should show some own effort with a first attempt how the code-modification might look like. If you have modified the code you will have a specific question. Specific questions are very very welcome and will be answered quickly.
despite un-specific questions are very like to get only late the same way unspecific answers (that don't help much)

best regards Stefan

I did not mean to insinuate that I am asking for a code solution; that is not what I am after.

I appreciate the tough love. I have been working on a solution that has helped me out. After all of the comments from others and yourself I have developed a very, in my opinion, elementary solution to my problem.

void DetectionOfObject(){  
  if(lockout == false && distanceCm <= 30){
  digitalWrite(in1,LOW); // Turn on the actuator
  digitalWrite(in2,HIGH);
  analogWrite(enA, 255); // Setting speed of motor
  delay(8000);
  // Closing of Bay Door
  digitalWrite(in1,HIGH); 
  digitalWrite(in2,LOW);
  analogWrite(enA, 200); // Setting speed of motor
  delay(5000); 
  lockout = true; 
  } 
  if(lockout == true)
    delay(1000);
    lockout = false;
   }

I have been working on this to provide me with a temporary solution. I want to make it more elegant, but as of right now, it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.