How do I trigger a continuous servo motor (FS90R) with an ultrasonic sensor (HC-SR04)?

Hi everyone!

I'm working on an artistic installation for a college exam and I wanted to activate a continuous servo motor if something is in the proximity (let's say 50 cm) of an ultrasonic sensor and then stop the rotation if nothing is detected.
I think I'm done with the wiring, but I'm in such a mess with the coding, since I've been failing to program it with Maxuino multiple times and I have barely a basic idea on how Arduino language works.
I really hope anyone could help me :confused:

Cheers

Have you managed to read the distance using the sensor and print it ? Please post your best efforts at the coding following the advice given in How to get the best out of this forum

Hi, thanks for answering!

By printing the distance you mean something like this?

const int trigPin = 6; const int echoPin = 7; long timeFront; int distanceFront; // Ultra Sensor on pins D6 & D7
void setup() {
  pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
  Serial.begin(57600);
}

void loop() {
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
  timeFront = pulseIn(echoPin, HIGH, 2940); distanceFront = timeFront * 0.034 / 2; // 50 cm maximum distance
  if ( distanceFront > 0) {
    Serial.println(distanceFront);
  }
  delay(50);
}


If that prints the distance then you are half way there. Test the distance and of it is greater than the target distance then do what you want with the servo.

Where are you stuck ?

This is great news, thank you!

I just tested the sketch and in the serial monitor are displayed reversed question marks (horizontally, not upside down like spanish punctuation) every time I put my hand in the proximity of the sensor.
Is that ok? Sorry you are dealing with a real dummy here :stuck_out_tongue:

Make sure the baud rate of the Serial monitor (bottom right) matches that of the Serial.begin() in the sketch

Ok, I now am able to make the servo to rotate in CCW motion, but i can't stop it from moving CW.
Is there something wrong?

#include <Servo.h>

Servo myservo;

const int trigPin = 6; const int echoPin = 7; long timeFront; int distanceFront = 0; // Ultra Sensor on pins D6 & D7

void setup() {

  pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
  Serial.begin(57600);
  myservo.attach(8); // attaches the servo on pin 8 to the servo object
}

void loop() {
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
  timeFront = pulseIn(echoPin, HIGH, 2940); distanceFront = timeFront * 0.034 / 2; // 50 cm maximum distance

  if (distanceFront > 0)  {
    // read the incoming byte:
    distanceFront = Serial.read();

    //say what you got

    Serial.print (distanceFront);
    if (distanceFront < 33); {
      Serial.println(" sent 180 Rotaing CCW ");
      myservo.write(180);
    }
  } else {
    Serial.println(" sent Stopped ");
    myservo.write(60);
  }
}


if (distanceFront < 33); 

Lose the semicolon

You are using a continuous rotation "servo" and they stop when 90, or something close to it, is written to them, but there is something very odd about your code quite apart from that

You seem to calculate the distance and if it is greater than zero (when wouldn't it be ?) you read a single byte from Serial without checking whether there is anything to read or not and make decisions based on the byte read. All very strange

Please describe in words what you are trying to do

@pete.caramelli, your topic has been moved to a more suitable location on the forum.

... and the cross-posting HERE has been closed.

Guglielmo

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