Prototype of radar scanning is slow ¿Can someone give me an advice? / Prototipo de radar escanea muy lento ¿Alguien me puede ayudar?

Greetings to all. My project It is a robotics project, a radar prototype made with an ultrasonic sensor and a HC-SR04 bipolar stepper motor, but it turns out that the stepper motor advance process (AND the delays) (from 25 to 25°) it slows down the sensor scan, I could really hand it over like this and there would be no problem, but I would like to know the solution to this problem. By the way, I am using an Arduino UNO

es un proyecto de robótica, un prototipo de radar hecho con un sensor ultrasonido y un motor paso a paso bipolar HC-SR04, pero resulta que el proceso (Y los delays) de avance del motor paso a paso (de 25 a 25°) ralentiza el escaneo del sensor, realmente podría entregarlo así y no habría problema, pero me gustaría conocer la solución a este problema. Por cierto, estoy usando un Arduino UNO

Code/Código

#include <Stepper.h>
#define STEP 4			// pin STEP de A4988 a pin 4
#define DIR 6			// pin DIR de A4988 a pin 5
const int Trigger = 2;   
const int Echo = 3;   
void setup(){
  Serial.begin(9600);
  pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT); 
  digitalWrite(Trigger, LOW);  
  pinMode(STEP, OUTPUT);	// pin 4 como salida
  pinMode(DIR, OUTPUT);		// pin 5 como salida
}
void loop(){
    MotorP();
    sensor();  
			  	// demora de 2 segundos
} 
    void MotorP(){
    int i = 0;
    digitalWrite(DIR, HIGH);		// giro en 25
  for(int i = 0; i < 25; i++){   	// 100 pasos para motor de 0.9 grados de angulo de paso
    digitalWrite(STEP, HIGH);
    delay(10);     	// nivel alto		  	
    digitalWrite(STEP, LOW);
    delay(10);      	// nivel bajo
    }			  	// por 10 mseg
    delay(1000);

    digitalWrite(DIR, HIGH);		
  for(int i = 25; i < 50; i++){   	// 100 pasos para motor de 0.9 grados de angulo de paso
    digitalWrite(STEP, HIGH);
    delay(10);     	// nivel alto		  	
    digitalWrite(STEP, LOW);
    delay(10);      	// nivel bajo
    }			  	// por 10 mseg
    delay(1000);
    
    digitalWrite(DIR, HIGH);		
  for(int i = 50; i < 75; i++){   	// 100 pasos para motor de 0.9 grados de angulo de paso
    digitalWrite(STEP, HIGH);
    delay(10);     	// nivel alto		  	
    digitalWrite(STEP, LOW);
    delay(10);      	// nivel bajo
    }			  	// por 10 mseg
    delay(1000);

    digitalWrite(DIR, HIGH);		
  for(int i = 75; i < 100; i++){   	// 100 pasos para motor de 0.9 grados de angulo de paso
    digitalWrite(STEP, HIGH);
    delay(10);     	// nivel alto		  	
    digitalWrite(STEP, LOW);
    delay(10);      	// nivel bajo
    }			  	// por 10 mseg
    delay(1000);
    
    i="";
    
  digitalWrite(DIR, LOW);	  	// giro en sentido opuesto
  for(int i = 0; i < 25; i++){
    digitalWrite(STEP, HIGH);
    delay(5);     
    digitalWrite(STEP, LOW);
    delay(5);    
    }
    delay(1000);
    
  digitalWrite(DIR, LOW);	  	// giro en sentido opuesto
  for(int i = 25; i < 50; i++){
    digitalWrite(STEP, HIGH);
    delay(5);     
    digitalWrite(STEP, LOW);
    delay(5);    
    }
    delay(1000);

digitalWrite(DIR, LOW);	  	// giro en sentido opuesto
  for(int i = 50; i < 75; i++){
    digitalWrite(STEP, HIGH);
    delay(5);     
    digitalWrite(STEP, LOW);
    delay(5);    
    }
    delay(1000);
    
  digitalWrite(DIR, LOW);	  	// giro en sentido opuesto
  for(int i = 75; i < 100; i++){
    digitalWrite(STEP, HIGH);
    delay(5);     
    digitalWrite(STEP, LOW);
    delay(5);    
    }
    delay(1000);
  i="";
  }
  
  void sensor(){
  long t; 
  long d;  
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(5);          
  digitalWrite(Trigger, LOW);
  t = pulseIn(Echo, HIGH); 
  d = t/59;            
  Serial.print("Distancia: ");
  Serial.print(d);      
  Serial.print("cm");
  Serial.println();
  if(d<20){
  Serial.print("Obstácuo adelante ");
    }        
  }

Do you understand that you cannot move the sensor until the echo has been received or the time-out for receiving the echo has expired. Meaning that NO echo has been returned. If you move the sensor during a sense, the return time will be wrong.

If you are just turning for effect, ok, but don't try ultrasonic distance measuring.

I built a similar rader demo a few years ago with an Arduno controlling a stepper motor and using a ultrasonic sensor to detect objects and a raspberry Pi to disdplay the "Radar Sacn" on a monitor
e.g.

loop
      generate trigger pulse
      wait for echo
      calculate distance from echo time
      transmit distance to RPi for display
      move stepper motor to next position

the closer objects are to the ultrasonic sensor the shorter the return time of the "echo"
this can cause the stepper motor to speed up and slow down as objects move in the field, i.e. the stepper moves faster when objects are closer to the sensor
a possible fix

  1. determine the longest trigger>echo time, i.e. the longest possible time between trigger and echo from most distant object
  2. when getting echos from closer objects add a short delay to bring the total time trigger/echo + delay to that of longest trigger>echo time

RPi_radar_display

Hi, @quian34
Welcome to the forum.

How fast is the sweep now?
How fast do you want it to sweep?, frpm 25 left to 25 right?

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

Oh, yes, the idea was to do the read between the pauses in the stepper, but scanning is so slow that can't do thad without solving it before....

btw. Ty for your contribution

Neat, i will try that and tell you how it was...

Ty for your contribution

Hi,
Can I suggest you try the NewPing library?

It will streamline your code and make the ultrasonic process easier to code.

Tom... :smiley: :+1: :coffee: :australia:

delay(1000); times 10 one cannot really complain about it being very slow.

Possible solution Finate State Machine.