How to Stop Motor DC by Value of Ultrasonic sensor

Hello

i want to make automatization door who can close and open according to the height read by the sensor HC-SR04. the door move by motor DC. take a look my schematic instrument in attachment.

this is my code

const int TrigPin = 2;
const int EchoPin = 3;
const int numReadings = 10;
const int Relay1 = 11;
const int Relay2 = 12;
const int Relay3 = 13;
const int EN1 = 9;
const int DC1 = 8;
const int DC2 = 7;
const int EN2 = 6;
const int DC3 = 5;
const int DC4 = 4;

long Duration = 0;

int readings[numReadings];
int index = 0;
int total = 0;
long average = 0.0;

void setup() {
  Serial.begin (9600);
  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);
  pinMode(Relay1,OUTPUT);
  pinMode(Relay2,OUTPUT);
  pinMode(Relay3,OUTPUT);
  pinMode(EN1,OUTPUT);
  pinMode(DC1,OUTPUT);
  pinMode(DC2,OUTPUT);
  pinMode(EN2,OUTPUT);
  pinMode(DC3,OUTPUT);
  pinMode(DC4,OUTPUT);

  for (int thisReading = 0; thisReading < numReadings; thisReading++)   //28
    readings[thisReading] = 0;   
    
}

void loop() {
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);

  Duration = pulseIn(EchoPin, HIGH);
  long Distance = (Duration*0.034)/2;

  total = total - readings[index];
  readings[index] = Distance;
  total = total + readings[index];
  index = index +1;

  if (index >= numReadings)
  index = 0;

  long average = (total / numReadings);
  int tinggiair = 17 - average;
  Serial.println(tinggiair);
  delay(1000);

  if(tinggiair<7)
  {
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,LOW);
    digitalWrite(Relay3,HIGH);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,HIGH);
    digitalWrite(DC2,LOW);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,HIGH);
    delay(3000);
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,LOW);
    digitalWrite(Relay3,HIGH);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,HIGH);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,HIGH);
  }
  else if (tinggiair>7)
  {
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,HIGH);
    digitalWrite(Relay3,LOW);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,LOW);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,LOW);
    delay(3000);
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,HIGH);
    digitalWrite(Relay3,LOW);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,HIGH);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,HIGH);
  }
  else if (tinggiair=7)
  {
    digitalWrite(Relay1,LOW);
    digitalWrite(Relay2,HIGH);
    digitalWrite(Relay3,HIGH);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,LOW);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,LOW);
    digitalWrite(DC4,HIGH);
    delay(3000);
    digitalWrite(Relay1,LOW);
    digitalWrite(Relay2,HIGH);
    digitalWrite(Relay3,HIGH);
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,HIGH);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,HIGH);
  }
  else
  {
    analogWrite(EN1,255);
    analogWrite(EN2,255);
    digitalWrite(DC1,HIGH);
    digitalWrite(DC2,HIGH);
    digitalWrite(DC3,HIGH);
    digitalWrite(DC4,HIGH);
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,HIGH);
    digitalWrite(Relay3,HIGH);
  }
}

the problem is, when sensor detected the height is below 7cm, motor DC active for 2 second to open the door. but after 2 second motor DC stop just about split of second, after that motor DC move again for 2 second, and it will repeat until sensor detected the height is above 7cm.

program that i need is, when sensor detected the height is below 7cm, motor DC will active for 2 second and after that it will stop until sensor detected the height is above 7cm.

can anybody help me to fix my probelm?

Arsyaas:
can anybody help me to fix my probelm?

your problem is learning programming basics.

Arsyaas:

program that i need is, when sensor detected the height is below 7cm, motor DC will active for 2 second and after that it will stop until sensor detected the height is above 7cm.

  • initialize variables
  • start program
  • if(sensor.height < 7cm) then (run motor for 2 seconds)
  • if(sensor.height > 7cm) then (stop motor)

do you understand the code;

  1. just for the motor ?
  2. just for the ultrasonic sensor ?

BabyGeezer:
your problem is learning programming basics.

  • initialize variables
  • start program
  • if(sensor.height < 7cm) then (run motor for 2 seconds)
  • if(sensor.height > 7cm) then (stop motor)

do you understand the code;

  1. just for the motor ?
  2. just for the ultrasonic sensor ?

i’m a newbie for programming, i just learn from web

the code is for both of them, sensor for measurement and motor for actuator

Arsyaas: the code is for both of them, sensor for measurement and motor for actuator

yes, but do you know which code is for the sensor, and which for the motor ?

Arsyaas: i'm a newbie for programming, i just learn from web

then you should start a bit slower.

learn to use JUST the ultrasonic sensor.

and learn to use JUST the motor.

Why are you using relays to switch LEDs?

DKWatson: Why are you using relays to switch LEDs?

because my lecture recommended me to use relay, so i can change LED with Lamp if it necessarily

BabyGeezer: yes, but do you know which code is for the sensor, and which for the motor ?

then you should start a bit slower.

learn to use JUST the ultrasonic sensor.

and learn to use JUST the motor.

yeah i learn it one by one, and i test it one by one, but when i mix its not become what i imagine

Arsyaas: yeah i learn it one by one, and i test it one by one, but when i mix its not become what i imagine

you should start by separating the code into functions.

give it meaningful names like read_sensor() or init_motor() etc

then you can see what part of the code is doing what.

if (tinggiair=7)Oops