Using ultrasonic sensor with motor stepper

i want to create a radar which is at end of each rotation show on lcd all distance object which is less than 30 cm with the degree ( or steps) of the moment of detection of those object
i have starrted a code

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Stepper.h>

#define echoPin 13 // Broche Echo du capteur ultrasonique
#define trigPin 12 // Broche Trig du capteur ultrasonique
#define STEPS 96   // Nombre de pas par tour du moteur pas à pas
const int stepsPerRevolution = 96; // Nombre de pas par tour du moteur pas à pas

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Initialisation du moteur pas à pas
long duration;
int degree;
int distance;



void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.clear();
  lcd.backlight();

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  myStepper.setSpeed(60);
}

void loop() {
 
  for(int i=STEPS;i;)
  {  
  myStepper.step(STEPS);
  delay(30);
  distance = calculateDistance();
  //Serial.print(i); 
  Serial.print(distance);
  Serial.println(" cm");

    
  myStepper.step(-STEPS);
  delay(30);
  distance = calculateDistance();
  //Serial.print(i);
  Serial.print(distance);
  Serial.println(" cm");
delay(100);
  }
}
  // Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){ 
  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  distance= duration*0.034/2;
  return distance;
}






but i have a prbleme i don't know how to save data and display it for later

Welcome to the Arduino forum. Nice to see you have correctly posted your code! I have questions about your reference to making a RADAR device, but first, why do you need to store data so you can show it later? What not show it immediately?

But, my concern. Using ultrasonic cannot be equated with RADAR. Ultrasonic devices report the time of receipt of the very first echo and never look for subsequent echos. RADAR may see 10,000 or more echos and process the phase, frequency and timing of echo return. That is one reason for the high frequency of RADAR.
A second concern is your ultrasonic has a "cone" of response and you can look at the specifications for your device to find that. What that means if there is a reflecting surface at one edge of that cone you will get a reflection return for your processing. If you turn the sensor and repeat the search, the same object will be reflected and reported, until the cone of response has moved COMPLETELY away from that same object.
A third concern is your ultrasonic will never distinguish between two or more reflecting objects. Only the first echo return is the one reported.
A forth concern is many objects will absorb the sound pulse and never be reported, or the sound pulse may be reflected away from the receiver and never be reported.

An HCSR04 cannot distinguish any more that that there is something within its sensing cone. The cone is about 15 degrees. That is, you cannot pick one thing from a group. The best that you can say is that there is something in the sensing cone.

So, what data to record?

How much data to save?
Where to store the data?
How long to keep the data?
How often to refresh displayed data?
Format of displayed data?

Can you draw a diagram showing how the data should be displayed?

sr04-angle

Beam-pattern-of-the-HC-SR04-sensor-modeled-on-the-simulator

Here is a Serial Monitor "bar graph SONAR"... the only problem is with the IDE... you can not copy from the Serial Monitor... but if you attach an SDcard to your project, you can store the data.

// bar graph "sonar"

#define trigPin 12
#define echoPin 13

long duration;
int distance;

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  delay(1000);
}

void loop() {
  // trigger the pulse train
  digitalWrite(trigPin, LOW);   // turn off pulses
  delayMicroseconds(2);         // pause before transmitting
  digitalWrite(trigPin, HIGH);  // transmit pulses...
  delayMicroseconds(10);        // ...for 10 microseconds
  digitalWrite(trigPin, LOW);   // turn off pulses

  // measure the return time
  duration = pulseIn(echoPin, HIGH);  // time the echo

  // calculate centimeters
  int cm = duration / 58;

  // plot the bar graph
  for (int i = 0; i < cm / 5 + 1; i++) {  // create graph of echo
    Serial.print("*");
  }

  // add a right-boundary
  for (int i = 0; i < 81 - (cm / 5 + 1); i++) {  // fill-in spaces after the bar
    Serial.print(" ");
  }
  Serial.println("|");

  delay(200);
}

Files for WOKWI.COM

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic1",
      "top": -113.7,
      "left": -61.7,
      "attrs": { "distance": "330" }
    }
  ],
  "connections": [
    [ "ultrasonic1:GND", "nano:GND.2", "black", [ "v0", "h94.8" ] ],
    [ "ultrasonic1:ECHO", "nano:13", "green", [ "v0" ] ],
    [ "ultrasonic1:TRIG", "nano:12", "green", [ "v0" ] ],
    [ "ultrasonic1:VCC", "nano:5V", "red", [ "v9.6", "h96" ] ]
  ],
  "dependencies": {}
}

I see but my concept was to display after because it will be too quick to read during the rotation and i wanted the rotation to be quick . I was wondering to at least i show if the distance is <30 because the ultrasonic
sensor turn and know if its possible to save a value a show it at the moment we want

Thank you for helping so its not possible to store only a few distance with a "if" statement ?
I wanted to show my data like in the photo

Okay thank you but is it possible to use array
Or a struct

That would be the only way to save both the step number and the distance. Make a struct of the two variable types you want to save and then create an array of some number of those structs. Of course, you never can tell which step that might be and will be different every time you run the program, but is a good beginning.

It is always possible to use arrays or structures.

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