1

I want to make a gate using 360 servo and ultrasonic sensor, here I use if else. when ultrasonic conditions >= 10 cm the servo should rotate one time to the right for 5 seconds, and vice versa. but when it reaches the condition >= 10 cm where the servo continues to rotate without stopping, how to make it stop in 1 rotation? and also i need distance data from ultrasonic sensor to display. I'm a beginner in this, I will be very grateful for the help.

this is my code :

#include <Wire.h>
#include <Servo.h>                      //Servo library
#define echoPin 3                       //triger pin, echo pin
#define triggerPin 4

int waktu;
int jarak;
int hasiljarak;
Servo servo;

void setup() 
{
  Serial.begin(9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
}

void loop() 
{
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  waktu = pulseIn(echoPin, HIGH);
  
  jarak = waktu * 0.034 / 2;

  Serial.print(jarak);
  Serial.print(" CM");
  delay(100);
  servo.attach(9);

  if(jarak<=10)
  {
    servo.write(2000);
    delay(5000);
  }
  else if(jarak>=30)
  {
    servo.write(1000);
    delay(5000);
  }
  else
  {
    servo.write(1500);
    delay(1000);
  }
  servo.detach();

}
dedek
  • 7,981
  • 3
  • 38
  • 68
  • `servo.write(angle)` makes the servo turn the specified angle in degrees. For just 1 turn you need to pass 360 as parameter. You are passing much larger values. Also you should not attach/detach the servo at each loop cycle but only once in the setup function –  Dec 21 '21 at 15:26
  • thanks for the advice, i'll try later and post the result immedietly. – Agan Azaro Putra Kiswanto Dec 22 '21 at 11:24

1 Answers1

0

i've finish the servo with this code

void loop()
{
  int ultra1 = tinggiUltra - sonar.ping_cm();
  int ultra2 = tinggiUltra - sonar2.ping_cm();
  
  delay(1000);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print(ultra1); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  Serial.print(ultra2);
  Serial.println("cm");

  if (ultra1 >= 11 && ultra1 <= 40 && ultra2 >=11)
  {
    servo.write(posClose);
    delay(2000);
  }
  else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
  {
    servo.write(posOpen);
    delay(2000);
  }
  else if (ultra2 <= 4 && ultra2 >= 0)
  {
    servo.write(pos);
    delay(2000);
  }

  statPin = servo.read();

}
    delay(2000);
  }
  else if (ultra1 >= 5 && ultra1 <= 10 && ultra2 <=10)
  {
    servo.write(posOpen);
    delay(2000);
  }
  else if (ultra2 <= 4 && ultra2 >= 0)
  {
    servo.write(pos);
    delay(2000);
  }

  statPin = servo.read();

}

for the servo I use it works normally as i wanted, and I use the MG955 360 servo. for the project I made using threads to open and close the door. I used if else for the code, and it worked fine by adding a parameter to restrict the movement of the door that I made. and thanks for the answer

if someone have to correct my program code, i'm grateful