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.
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);
}
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
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.