Help with my latest project

I am relativly new to arduino and I am trying to make a radar with the ultrasonic sensor and a servo.

I don't know how to make the ultrasonic readings constant while to the servo is turning, can anyone help.

Below is the code that I have written so far. Currently, the ultrasonic sensor only takes readings after one sweep.

#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
long duration;
int distance;

const int trigPin = 5;
const int echoPin = 4;

Servo radTurn;
int pos = 0;

void setup() {
  
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Loading .  .  .");
  delay(5000);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(115200);
  radTurn.attach(9);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Tracking ready  .  .  .");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;

  loop2();
}

void loop2() {

  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    radTurn.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    radTurn.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);     
  }
}

Thank you so much.

  • Do you understand you are not using the ultrasonic whilst moving the servos ?

You want your readings to be constant?
What is your idea of a radar?

Give loop2 a reasonable name...

This code ENSURES there will be NO servo movement while taking an ultrasonic reading!

No I mean that they are happening while the servo is turning. Sorry for the bad wording.

Yes and I don't know how to fix that

How would you recommend I fix that.

There is no fix! The documentation for pulseln() tells you it blocks until a return pulse is read or the time-out setting occurs.

Is there something that I could replace pulse in with, like some thing that can still take a reading while the servo turns

  • Do some research into VL53L0X

Just to make clear: You want to take readings continuously during movement of the servo?
In that case: do your reading inside the for loop that moves the servo.
So: move a bit, take a reading, move a bit further...

I started to try that but it didn't give me the readings like I wanted.

What did it give and what did you want? We are not able to read minds!

You need to be more precise.
What did you try (code, schematic), what did you expect, what did you get instead?
Without that info, we cannot help you...

I would want it to take readings continuously as it is turning, not just when it stops on the sides.

Are you saying that the way you were shown does not work?

You can not want. You must learn. Then you can do.

When the UltraSonic sensor does this:

  • trigger pin is pulsed HIGH
  • transmits a pulse train
  • receives an echo

No other processing occurs... including "no servo."

What you can do to make it seem "continuous" is to let the SR04 squirt, then move the servo 15 degrees (the angle of the transmitter cone) then squirt again... and repeat. You probably want to move the motors forward a bit, by deciding which is the best time to move (between squirt then servo, or servo then squirt).

This shows that you have not read anything about the HC-SR04 or the servo motor, and ignorantly demand you get what you want. Cows don't make chocolate milk, regardless of how much you want.

Take the advice of everyone and get a little reading done. You will have a much better time when you understand the subject of your project and here on the forum.