1

My code represents a water level sensor, and the components are the Arduino Uno, a breadboard, an ultrasonic servo and two LEDS in which one is red and the other is green.

The condition that I don't know how to include in my code properly to make the servo rotate and activate the LEDs simultaneously:

When the distance is <= 50, servo rotates 180 and activates the red LED else servo goes to zero and activates the green LED.

My code:

#include <Servo.h>
int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

// **************************************//
void setup()
{
 
 Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin);
 
}

void loop() {
  
  int duration, distance;
  pinMode(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 distance = pulseIn(echoPin, HIGH);
 distance = duration *30/2500;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  
  
 if (distance <= 50)
 
{
  digitalWrite(red, HIGH);
  srv.write(180);
   delay(1000);
} 
else 
{
  digitalWrite(green, LOW);
  delay(1000);
  srv.write(0);
}
}



```



[![System][1]][1]


  [1]: https://i.stack.imgur.com/6YXgf.png
11
  • 2
    if the servo continuously turns without stopping, then you are missing an intermittent motion servo ... you have a continuous rotation servo
    – jsotola
    Apr 23 at 14:54
  • please describe in detail what actually happens
    – jsotola
    Apr 23 at 14:55
  • @jsotola my servo rotates continuously, but I want to control the rotation myself when I start the simulation in which I use the ultrasonic sensor and the distance. Apr 23 at 14:59
  • Re “distance = pulseIn(...);”: You mean duration = pulseIn(...);. Apr 23 at 15:08
  • 2
    if the servo spins in one direction without stopping, then it is not the kind of servo that rotates to a specific angle and stops ... no amount of programming will fix that
    – jsotola
    Apr 23 at 15:26

1 Answer 1

1

Checking your program I found two errors IMHO. Please see the comments in the copied code below. The one also pointed out by @EdgarBonet definitely prevents your program from working correctly. The other one only has impact of the correctness if the values of the constants OUTPUT and LOW differ. I don't know if that is the case.

I'm pretty sure, that the continously turning servo is not caused by the program. There are two kinds of servos. One type that gets a signal which indicates the angle of the turn of the servos arms. The other type gets the same signal (called PWM = Pulse Width Modulation) and interprets it as the speed with that it continously turns the servo arms.

If you use the latter one the arduino sends a signal like turn about 180° but the servo understands turn with full speed. There is nothing you can do programatically to avoid that.

There are workarounds like build a feedback loop with a potentiometer that measures the angle of the turn or use stopper sensors. But the simplest way is to use a servo that is made for your usecase. (A non continous or standard servo for e.g. RC Cars, RC Ships, RC Air Planes)

BTW: The picture your provided shows a simulation. If the Servo in the simulator turns continuously, perhaps there is a property you can use to change the servo type from continuous to standard. ??

#include <Servo.h>

int position = 0;
Servo srv;

const int trigPin = 4;
const int echoPin = 2;
const int ServoPin = 3;
const int green = 5;
const int red = 6;

int durantion;
int distance;

void setup()
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  srv.attach(ServoPin); 
}

void loop() 
{
  
  int duration, distance;
  // error: you already set the pinmode to OUTPUT
  // pinMode(trigPin, LOW);
  // perhaps you mean 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // error: duration or distance ? ;-)
  // distance = pulseIn(echoPin, HIGH);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 30 /2500;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance <= 50)
  {
    digitalWrite(red, HIGH);
    srv.write(180);
    // improvement: delay necessary?
    // delay(1000);
  }  
  else 
  {
    digitalWrite(green, LOW);
    // improvement: delay necessary?
    // delay(1000);
    srv.write(0);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.