I Just want to drive my servo to 180 degree ... ,1 degree per step, ..... with an Ultra sonic sensor on it which takes reading every degree.... And then move it in opposite direction to 0 degrees...
The main Problem is that i want to take the average of both the ultrasonic readings i.e from 0 to 180 and from 180 to 0 ..... and send that average on the Serial port .. Can anyone help me with that ....
Please put short programs in your Post so we don't have to download them. Like this
g#include <LiquidCrystal.h>
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;
int pos = 0;
int it = 10;
int lld;
LiquidCrystal lcd(6,5,7,4,3,2);
void setup() {
lcd.begin(16, 2);
myservo.attach(9);
Serial.begin(9600);
delay(3000);
}
void loop() {
int i = 0;
int t = 0;
float a = 0;
for (i = 0; i < 180; i ++)
{
unsigned int uS = sonar.ping();
myservo.write(i);
delay(20);
for (t = 0; t < it; t++)
{
uS = sonar.ping();
a = uS/US_ROUNDTRIP_CM + a;
delay(30);
}
a = a / (it-1);
t = 0;
Serial.println(a);
a = 0;
lcd.print(uS/US_ROUNDTRIP_CM);
delay(500);
lcd.clear();
}
}
AHaseeb:
The main Problem is that i want to take the average of both the ultrasonic readings i.e from 0 to 180 and from 180 to 0 ..... and send that average on the Serial port .. Can anyone help me with that ....
I don't understand this. Can you give an example. The average from 0 to 180 is 90 - but I suspect that is not what you want.
U didnt understood ..... In this code which ive uploaded i`ve moved my servo from 0 to 180 and sensor takes reading at every degree... and prints distance(a) on the serial port ....
what i want to do is that once the servo has moved to 180 deg i want to move it in opposite direction to 0 deg... and repeat the same procedure with the sensor... but what i want to print on the Serial port is the average distance of both cycles ...
void loop() {
int i = 0;
int t = 0;
float a1 = 0;
float a2 = 0;
float a;
for (i = 0; i < 180; i ++)
{
unsigned int uS = sonar.ping();
myservo.write(i);
delay(20);
for (t = 0; t < it; t++)
{
uS = sonar.ping();
a1 = uS/US_ROUNDTRIP_CM + a1;
delay(30);
}
a1 = a1 / (it-1);
t = 0;
}
for (i = 180; i > 0; i --)
{
unsigned int uS = sonar.ping();
myservo.write(i);
delay(20);
for (t = 0; t < it; t++)
{
uS = sonar.ping();
a2 = uS/US_ROUNDTRIP_CM + a2;
delay(30);
}
a2 = a2 / (it-1);
t = 0;
a = (a1+a2)/2;
Serial.println(a);
a = 0;
a2 = 0;
a1 = 0;
}
Hi,
I think what the OP wants and isn't doing is.
Scan the Ultrasonic form 0 to 180deg in 1deg steps, each step record the Ultrasonic distance reading.
Pass each reading to the monitor.
Then scan the Ultrasonic form 180 to 0deg in 1deg steps, each step record the Ultrasonic distance reading.
But this time calculating the average of the readings of each step with the corresponding step from 0 to 180deg.
He will have to use arrays.
array1[] for 181 steps 0 to 180
array2[] for 181 steps 180 to 0
arrayaverage[] steps 0 to 180
so I would assume the OP can construct on screen a sonar map.