problem onparsing two data sets from ascii serial string

Please could anyone help me sort this out for a newbie..

I am sending an ascii stream from RoboRealm consisting of a camera view with centre at zero for both vertical and horizontal.

Any deviation from centre can give a number from 120 to -120 for each V or H.

I have the Horizontal working OK but no Vertical control of the servo.

I do not yet seem to grasp how to extract the Vertical data set from the stream.

I have at the Send end added a "cr" after the first Hor data set to see if I can use that as a marker, but got completely messed up.

Any late Christmas present out there that may get me moving forward...

Thanks in anticipation.

#include <Servo.h> // Include the servo library

Servo Horizontal; // Create a new servo object (Horizontal)
Servo Vertical;   // Create a new servo object (Vertical)
Servo SideTilt;
char incomingData[8] = {
  0, 0, 0, 0, 0, 0, 0, 0}; // A buffer to store the ASCII value read in from the serial port

int distanceH = 0; // The distance of the object from the horizontal centre of the screen
int distanceV = 0;//  The distance of the object from the vertical centre of the screen
int currentHorAngle = 90; // The current horizontal angle of the servo
int currentVertAngle = 80; // The current vertical angle of the servo
int i = 0; // counter

void setup(){
  Serial.begin(14400); // Open the serial port with a 9600 baud rate
  Serial.println("Serial port ready"); // Print on screen
  Horizontal.attach(9); // Attach the servo signal line to pin 9
  Horizontal.write(currentHorAngle); // Set the starting angle at 90 degrees
  Vertical.attach(12);// Attach the servo signal line to pin 12
  Vertical.write(currentVertAngle);//Set the starting angle at 80 degrees
 // SideTilt.attach(10);
 // SideTilt.write(90);
}

void loop(){
  // Wait for data to become available at the serial port
  if (Serial.available()){
    // Get the data coming through the serial port and store it in the buffer
    while (i < 8){
      incomingData[i] = Serial.read(); // Assign the input value to the incomingData buffer
      i++; // Increment the counter
    }

   
    distanceH = atoi(incomingData); // Convert ASCII to Int
    distanceV = atoi(incomingData); // Convert ASCII to Int
   
    // If the distance is negative then add 5 to the currentAngle(s) and update the servo, and vise versa
    
    if (distanceH < -15){
      currentHorAngle--;
      currentHorAngle = constrain(currentHorAngle, 45, 160); // Constrain the value of currentHorAngle to within 0-160 degrees
      Horizontal.write(currentHorAngle);
  
    }
    else if (distanceH > 15){
      currentHorAngle++;
      currentHorAngle = constrain(currentHorAngle, 45, 160); // Constrain the value of currentHorAngle to within 0-160 degrees
      Horizontal.write(currentHorAngle);
   
    }
  
   if (distanceV < -15){
      currentVertAngle--;
      currentVertAngle = constrain(currentVertAngle, 45, 90); // Constrain the value of currentVertAngle to within 0-90 degrees
      Vertical.write(currentVertAngle);
  
    }
    else if (distanceV > 15){
      currentVertAngle++;
      currentVertAngle = constrain(currentVertAngle, 45, 90); // Constrain the value of currentVertAngle to within 0-90 degrees
      Vertical.write(currentVertAngle);
   
    }
  
  
  
  }

  i = 0; // Reset the counter
  delay(20); // Delay 20ms to allow the servo to rotate to the position
}

See if there's at least one character available to read, then go ahead and read all eight of them?
No.

Try Robin2's serial handling basics tutorial.

Well, what a plonker I have been, too much festive cheer.. :slight_smile:

It does work OK, I had altered the VB script on RoboRealm and not followed it through on the Serial send..Doh!

Perhaps better doing the codes not at any festive time...

Happy New Year to all

John

Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

ERRRR....Still got a problem, due to the fact that both servo's move around the same distances I now realise that the first four data sets are controlling both servos.

The Horizontal is perfect, but the Vertical is reading the Horizontal data set and ignoring the following four which should control the Vertical Servo.....so still needs parsing..

Perhaps do need some festive cheer....any help would be appreciated.

Can't see your code.

assuming you incomming data looks like "-120+120" try

   distanceH = atoi(incomingData); // Convert ASCII to Int
    distanceV = atoi(&incomingData[4]); // Convert ASCII to Int

convert the first four characters to distanceH and second four to distanceV

Horace, thank you for your input much appreciated, though I still get the Vert reading the Hor data.

On further tests, I have found that the Vert servo only responds to the Hor "-" intergers.
Not that I want it to respond to any Hor data, but may be a clue.

If I was to place a CR between the Hor and Vert data would this work, allowing some how to use this in a sub routine..(sorry old basic terms) and also would it then need another "0" adding to the data buffer?

horace:
convert the first four characters to distanceH and second four to distanceV

I think you will find that atoi() does not work like that.

...R

atoi should stop parsing on the first non-decimal digit character.

could you give us some samples of the data? e.g. is a positive value sent as "+120" or " 120" - the later probably won't work as it starts with a space not a digit
sending as two seperate lines terminated with carriage Return may be a good idea
you could use parseInt() to read the integers

but set the timeout() as required or check for parseInt() returning 0

You need to synchronise the receiver. If one character goes missing due to any glitch on the wire, how does it (1) deal with that error and (2) wait for the next real data to start. Think about dropping in to that data stream at any point and discovering which value is intended for the vertical.

Data sent as seen on the RR program eg -12044 ,29-54 or any combination of.

I do not get errors or should I say not seen when only the Horizontal data is sent but yes I can see that any dropouts would/ could give erroneous errors.

Now I am defiantly moving away from comfort zone and anything I even have a clue about with your suggestions.
I was just hoping to get it broken up into to data sets and perhaps once that is understood and then attempt to error check now that has been pointed out.

eg -12044 ,29-54 or any combination of. Clarification on this...

(-12044 ) -120 Hor data, 44 Vert data.............. (29-54) 29 Hor data, -54 Vert data.............

But you can read it, right? If you got -12044 ,29-54-12044 ,29-54-12044 ,29-54-12044 ,29-54-12044 ,29-54-12044 ,29-54-12044 ,29-54 then nobody can tell what is what.

But it's probably
-12044 ,29-54
-12044 ,29-54
-12044 ,29-54
-12044 ,29-54
-12044 ,29-54
-12044 ,29-54

And that looks "obvious" to a human. It's also obvious to an Arduino. You just have to make it look for the carriage-return character '\r' or newline character '\n' which will tell you where the data starts and ends. Apply that knowledge to the Serial Input Basics tutorial.

I suppose if it was "obvious" then I have wasted an evening... :o

Due to the port being used by RR therefore the Serial Monitor being inoperative I can only see what is supposedly be sent and that is as seen at the send end.

-16055.... -160 hor....55 vert
1030....... 10 hor....30 vert
2-27........ 2 hor.....-27 vert

I will read again, I have tried to absorb in my 72 year old grey matter the Serial processing,thankfully the electronics side is "obvious " too me and my soldering iron.

I have tried a 'CR' inserted between the end of the Hor to use as an end of Hor data but parsing that too the correct Servo's I am finding more than challenging, should I dare say, not obvious too me.

The total max data that is required is 8 bytes, I extended the buffer to accommodate the 'CR' to 9 bytes..that is where I am at present.

Any help without any distracting comments would be appreciated, but I will press on with what I have got.

Paste the exact output here between [ code ] tags. Don't include any explanation or comments inside the tags. But explain what each number is supposed to represent.

I still haven't worked out if the transmission format is something you control or you're stuck with a fixed format.

I have no way of seeing the data received as the com port is in use with RR sending the data.
I have (I think) attached screenshots of the RR comms interface and the VB Module showing the format of the data sent.
Is there anyway of seeing the data at the Arduino as received ?

Before Constraints the data for each could be Horizontal 160 to -160 and the Horizontal 120 to -120. This allows for a 320 x 240 camera frame to be analysed.

RR Comms.JPG

JohnWood337:
Is there anyway of seeing the data at the Arduino as received ?

The examples I linked to in Reply #3 do that.

...R

Robin, I did look at the parsing code as you suggested and ran it but I as mentioned and asked, how do I see the received data response at the Arduino end.

If I use the Arduino Serial Monitor I obviously get the error that the port is being used, is there another way to see the stream response at the Arduino.

I hope so as it then proves what and what is not there or corrupted ...

Do I use a third party or something like a virtual port or is it me not understanding the full workings of the Arduino program....I would think the latter.. :confused: