Go Down

Topic: Serial output float array~(Help) (Read 440 times) previous topic - next topic

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.
Code: [Select]
//**************************** 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!

robtillaart

Code: [Select]

for (int i=0; i< 19; i++)
{
  Serial.print(d[i], 2); // nr of decimals
  Serial.print(" --  ");
  Serial.print(p[i], 2);
  Serial.println();  // note the diff between print and println
}


Can you manually type in how you would like your output?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Or, more succinctly
Code: [Select]
for (int i=0; i< 19; i++)
{
  Serial.print(d[i], 2); // nr of decimals
  Serial.print(" --  ");
  Serial.println(p[i], 2);
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

I want to output the array, I mean, not one by one, but the whole array.
I have found a way now, I know it's impossble to output a float array once(just the char array can do that). So I firstly converse the float type to String(using the dtostrf() method), then connect the many String together to one String, then output the long String. So that I can get the whole array.

Thanks for you attention.
Quote from: robtillaart link=topic=68545.msg505600#msg505600 date=1312556312

Can you manually type in how you would like your output?
[/quote

AWOL

Quote
I want to output the array, I mean, not one by one, but the whole array

I don't understand your concerns - whichever way you do it, it is relatively slow because it is serial.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up