Array related

Hello all
I have a hr sc04 ultrasonic sensor (ping sensor) and a servo motor
Sensor attached on servo
What I want to do.
While servo rotate 0 180 degree sensor takes reading like a radar.
I want to record in array 0 to 180
And 180 to 0
And compare both array

You didn't actually ask a question. Are you asking for someone to write the code for you?

Do you really want to take a measurement every time the servo moves 1 degree? I do a similar thing in my robot car code, but only take measurements in 5 positions as the servo sweeps. 5 positions might not be enough for you, but 180 is possibly too many.

For 180 measurements per sweep, all you need to do is create an array with 180 elements, set the servo to each position, then assign the result of each 'ping' to the relevant position in the array, using the servo's angular position as an index into the array.

Keep in mind, though, that most hobby servos are not actually capable of moving a full 180 degrees and will run into the end stops.

Edit: If you want to measure in both directions of servo movement independently, then compare results, you'll need two arrays. For example:-

int cwRanges[180];
int ccwRanges[180];

Your sensor will have a beam angle of around 15 to 20 degrees; it simply makes no sense to read it at one degree intervals.


cwranges[5] = {0,10,20,30,40,50,60};

for (int i=0;i<=5;i++)
if ( cwranges_!=ccwranges )_
{Serial.println(“not equal”) } //but how to know which element of array is not equal…??


Good luck with that.

//but how to know which element of array is not equal…??

The one at index “i”, of course.

Now can you see why we ask you to use code tags when you post code?

sorry frustrate with that extra element..

and thanx to focusing me on right way

Perhaps you should re-read what I wrote and think a little. An array for the positions is a good idea, but what I was talking about with cwRanges and ccwRanges was arrays for the actual distance readings. (Hence the word "Ranges".) "cw" for the clockwise sweep readings and "ccw" for the counterclockwise sweep readings. (Just a hint to use variable names that relate to their values.)
Perhaps a more apt name might be appropriate for the array holding the positions at which you want to take readings?
Maybebyte scanAngles[5];or similar.


sorry frustrate with that extra element.

Two extra elements, actually. :slight_smile:

if ( cwranges!=ccwranges )Also, you'll need to compare the individual elements, not entire arrays.