Triggering 3 Separate Servos with 3 Separate Sensors

Hi everyone. Forgive me - I'm on day 2 of experimenting with Arduino. I'm really enjoying it and have found everyone's projects and feedbacks inspiring.

I'm trying to power 3 servo motors:
1 using a button (tested and works)
1 using a ultrasonic sensor (having trouble)
1 using a temperature module (haven't started)

I found some code that I really like that rotates the servo back and forth 180 degrees while the button is pressed. I'm trying to apply the same logic to the ultrasonic servo so that it'll rotate continuously 180 degrees while detected something very close.

I simply can not get the servo "Wind Servo" to detect anything. I'd be grateful for any help. I'm sure it is something obvious that I haven't learned yet. Thank you in advance.

I'm not sure how I can link my code as a new user..

The forum guidelines. tells how to post code.

There's more than one way to add code tags, you can:

  • Paste your code in the box where you are editing your post, select all the code and click on </>.
  • In the Arduino IDE under the 'Edit' menu there is 'Copy for Forum', this puts your code on the clipboard with code tags added ready to paste into your question.

For the button you probably use something like

 if(buttonState == LOW)  // button pressed
{
  moveServo(0);
}
else
{
  maveServo(180);
}

To do it with a range finder, something like

if(range < 20) // centimeters
{
  moveServo(0);
}
else
{
  maveServo(180);
}

Thank you, @groundFungus !!

To try to isolate my issue, I started a new sketch for just my ultrasonic sensor.

Main issue: I can get the ultrasonic sensor to detect the proper distance and the servo to begin moving exactly as I want. However, it doesn't stop when it no longer detects the close distance.

Here is my code:

#include <Servo.h>
Servo WINDservo;  
int pos = 20;  
const int trigPin = 7;
const int echoPin = 6;
int angle =90;    // new
int angleStep =5; // new
const int minAngle = 0; // new
const int maxAngle = 180; // new


long duration;
float distance;

void setup() 
{
  WINDservo.attach(5);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT_PULLUP); 
  Serial.println("wind servo");
  WINDservo.write(90); // initial position
  
}

void loop() 
{
  Serial.begin(9600);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = 0.034*(duration/2);
  Serial.println(distance);
  if (distance <3)
  {
   
    //WINDservo.write(pos+160);
    digitalWrite(trigPin, HIGH);
    delay(1000);
  }
  else 
  {
 
      WINDservo.write(pos);
      digitalWrite(trigPin, LOW);
  }
  delay(300);
   while(digitalRead(trigPin) == HIGH) // original code says HIGH
  {
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= minAngle || angle >= maxAngle) { 
      angleStep = -angleStep;
    } 
    WINDservo.write(angle); // move the servo to desired angle
      Serial.print("wind servo Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there  
  }}

You check the distance using the usual ultrasonic sensor code.

If the distance is small, you set the trigger pin high.

If the trigger pin is high your while loop runs forever moving the servo and you never check the distance again.

Try replacing the while with an if.

Thanks for your response, @wildbill .

When looking at the Serial Monitor, I'm receiving readings from the ultrasonic.

Is there a much easier way to set the trigger pin to high? I know that its happening, because I do get the servo to trigger.

How can I continuously monitor the Trig Pin to see if my distance has gone back to LOW (aka "far)?

You can't. The trigger pin is controlled by you, so if you set it high, it's high. If you set it low, it's low, it won't change of its own accord.

The trigger pin makes the sensor send out a pulse of ultrasonic sound which you use the echo pin to detect. Since you likely copied that code, I expect it works.

To work with distance, you need to run all the code at the top of loop that culminates in you setting the distance variable. Then you can decide what to do with the servo. The trigger pin has nothing to do with it.