Hi there,
I am doing a project recently, it's about control a robot remotely and get the distance (taken by a distance sensor) back to the client interface.
As you can see, one of the functions is to get the distance number, my design is : when the servo motor under the distance sensor sweeps, it get the according distance. For example, when the angle is 0, get 23.44, when it sweeps and the angle is 20, get distance 34.22... I transport the angle and the distance number by Serial.print() function.
Following is my code about get the distance number and sweep servo and then Serial.print the numbers.
//**************************** DISTANCE SENSOR **************************************************
float distanceReturn() {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
// Serial.println(distance); // print the distance
return distance;
//delay(1000); // arbitary wait time.
}
//**************************** SERVO SWEEP ROUTINE **************************************************
void sweep()
{
float d[19];
float p[19];
for(pos = 0; pos <= 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
d[pos/18]=distanceReturn();
Serial.print(pos);
Serial.print(" -- ");
Serial.println(d[pos/18]);
delay(105); // waits 105ms for the servo to reach the position
}
delay(1000);
for(pos = 180; pos>=0; pos-=10) // goes from 180 degrees to 0 degrees
{
myServo.write(pos); // tell servo to go to position in variable 'pos'
d[pos/18]=distanceReturn();
Serial.print(pos);
Serial.print(" -- ");
Serial.println(d[pos/18]);
delay(105); // waits 105ms for the servo to reach the position
}
}
My question is, now it's output the pair(distance and angle) one by one, but I want to print them like a package, namely, I want print the angle(from 0~180) and the according distance together.
I tried array, but don't know how to output a whole array, is there any method like Serial.print(d)? If not, how can output the 19 pairs numbers to the client?
Sorry my English is poor, if it's not clear about the question, please talk to me and I'll try to make it clear.
Thank you in advance!