need help, hint and tricks on taking serial data

is there any tricks to get exact data from serial.print to another program?

example : the arduino serial. print result are

x y a b 89 30 329 277 124 5 60 89

there are 4 numbers, x, y, a, b.. all of them can be 1 or 2 or 3 digits.

how to take exactly what is x, y, a, b?

i`m using VB to take these data.. all i do now is to use mid and take data from specified position and length.. but if the digits are vary, mid function are hard to use.. the serial.print should look like this if we want to use mid available for all numbers

xxx yyy aaa bbb

020 005 390 270

the logic is to make all 3 digits no matter the number is..

the question is, is there any other way than making all numbers has the same digits?or is there any tricks in taking serial data?

The Arduino can use the sprintf function to format a string to be sent, with all "numbers" having the same number of digits.

int x=89;
int y = 30;
int a = 329;
int b = 277;
char buff[24];
sprintf(buff, "%.3d %.3d %.3d %.3d", x, y, a, b);
Serial.print(buff);

In VB, the String class has an InStr() function that can be used to locate strings withing strings, like a space in the serial data. Knowing where the space is would allow you to extract the tokens regardless of their length.

The typical thing to do is use delimiters to seperate data. In the exaple you provided you could have your VB application look for the "space" between the character strings to seperate them.

thanks a lot.. really helpful..

i’ve tried the sprintf,

my code is

void printing()
{
  //getting mouseinfo for printing subroutine.
  MouseInfo mouseInfo;
  mouse.getData(&mouseInfo);
  mouseX=(mouseInfo.cX, DEC);
  mouseY=(mouseInfo.cY, DEC);
  sprintf(buff, "%.3d %.3d %.3d %.3d", cmKanan, cmKanan, mouseX, mouseX);    //printing with the same digit for all numbers
  Serial.println(buff);   //print numbers
  delay(500);
}

the result,

015 000 230 000

the result always shows the second and fourth number 000. what is wrong? any idea? even though the number should be same but the output is different…

Would you please explain what you think this code is doing.

  mouseX=(mouseInfo.cX, DEC);
  mouseY=(mouseInfo.cY, DEC);

You could print put the values of mouseX and mouseY, and see that they are not what you think they should be.

  sprintf(buff, "%.3d %.3d %.3d %.3d", cmKanan, cmKanan, mouseX, mouseX);

Why are you printing the same values twice?

Finally, how are mouseX and cmKanan defined? What type are they?

  mouseX=(mouseInfo.cX, DEC);
  mouseY=(mouseInfo.cY, DEC);

its taking data from the ps/2 mouse..

when i try print all these data without sprintf, it all went well without mistakes. but when i use sprintf, the second and fourth data always turn zero..

at first i use all different data, cmKiri, cmKanan, MouseX, MouseY..but it turns the second and fourth data is always zero... i thought that my sensor are broken. but when i try to use all the same data like cmKanan, cmKanan, MouseY, MouseY they should turn out like this...

if cmKanan = 50 MouseY=100

so the print will be like this

050 050 100 100

but its not printing like it should be..

the print always like this

050 000 100 000

what is happening?

is there any mistakes in my printing?

MouseInfo is a structure. You are creating an instance of this structure, called mouseInfo.

The mouseInfo instance has members cX and cY.

mouseX = mouseInfo.cX; would make sense.

What do the parentheses and ,DEC do? Probably not what you think they do.

I'm not able to help you more unless you answer all the questions. Posting code snippets that illustrate a problem is great, as long as the snippet is complete. Yours is not, because the types of cmKanan and mouseX are unknown, and may be critical to understanding the problem.

sorry for the lack of info. here is my full code,

//initialization
#include <ps2.h>

//mouse pin
PS2Mouse mouse(11, 12); //pin 11 as SCLK, pin 12 as SDIO


//pin initialization
//motor
int rmotorpin1=5;
int rmotorpin2=6;
int rena=4;
int lmotorpin1=9;
int lmotorpin2=10;
int lena=8;

//ultrasonic sensor
int pingKiri=2;
int pingKanan=3;


//LED
int LED=A5;



//variable initialization
//ultrasonic sensor
long durationKiri, durationKanan, cmKanan, cmKiri;
//mouse coordinates
int mouseX;
int mouseY;

//serial declaration to show serial.read
int serial;
char buff[24];

void setup()
{
  //pin declaration
  //motor
   pinMode(rmotorpin1,OUTPUT); 
   pinMode(rmotorpin2,OUTPUT); 
   pinMode(rena,OUTPUT); 
   pinMode(lmotorpin1,OUTPUT); 
   pinMode(lmotorpin2,OUTPUT); 
   pinMode(lena,OUTPUT); 
   digitalWrite(rena, HIGH);
   digitalWrite(lena, HIGH);
  
  //LED
  pinMode(LED, OUTPUT);

  //mouse initialization
  mouse.init();
  
  //open com
  Serial.begin(38400);
}

void loop()
{
  if (Serial.available() >0)      //looking for serial input
  {
  serial = Serial.read();          //take the serial input data
  switch (serial)
  {
    case 49:    //input 1 : automatic mapping vehicle
    do
    {
      analogWrite(LED, 200);
      ultrasonic();
      //motor();
      printing();
    }
    while (serial == 49);
  }
 }
}


void motor() 
{  
   //motor kanan &kiri maju
   analogWrite(rmotorpin1, 200);
   digitalWrite(rmotorpin2, LOW);
   analogWrite(lmotorpin1, 200);
   digitalWrite(lmotorpin2, LOW);   
} 


void ultrasonic()
{
  // The ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  //Kiri
  pinMode(pingKiri, OUTPUT);
  digitalWrite(pingKiri, LOW);
  delayMicroseconds(2);
  digitalWrite(pingKiri, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingKiri, LOW);

  // The same pin is used to read the signal from the ultrasonic sensor: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingKiri, INPUT);
  durationKiri = pulseIn(pingKiri, HIGH);

  //doing the same routine for right sensor
  //Kanan
  pinMode(pingKanan, OUTPUT);
  digitalWrite(pingKanan, LOW);
  delayMicroseconds(2);
  digitalWrite(pingKanan, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingKanan, LOW);

  pinMode(pingKanan, INPUT);
  durationKanan = pulseIn(pingKanan, HIGH);
  
  // convert the time into a distance
  cmKiri = microsecondsToCentimeters(durationKiri);  
  cmKanan = microsecondsToCentimeters(durationKanan); 
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

void printing()
{
  //getting mouseinfo for printing subroutine.
  MouseInfo mouseInfo;
  mouse.getData(&mouseInfo);
  mouseX=(mouseInfo.cX, DEC);
  mouseY=(mouseInfo.cY, DEC);
  sprintf(buff, "%.3d"" ""%.3d"" ""%.3d"" ""%.3d", cmKanan, cmKanan, mouseX, mouseX);    //printing with the same digit for all numbers
  Serial.println(buff);   //print numbers
  delay(500);
}

the logic of the code is whenever 1 is sent via serial connection the program will start, the program consist of running the motor, running ultrasonic sensor, taking mouse coordinates and printing all the data needed.

ultrasonic sensor code i got from PING sensor library and mouse coordinates code i got from ps2 mouse library from arduino playground and modified a bit.

the problem comes when i try to use sprintf as i mentioned before.

What do the parentheses and ,DEC do? Probably not what you think they do.

i don’t really get what ,DEC do but what i know when i run the library the coordinates is fine… in my opinion, DEC is to make the data decimal…

MouseInfo is a structure. You are creating an instance of this structure, called mouseInfo.

The mouseInfo instance has members cX and cY.

mouseX = mouseInfo.cX;
would make sense.

i don’t understand much about instance, members, or structure much. but what i had in mind that mouseInfo had its own library that takes data, the data taken is cX which means mouseInfo.cX = taking cX data from mouseInfo library. am i correct?

for your convenience, here is the ps2_mouse library

#include <ps2.h>

/*
 * an arduino sketch to interface with a ps/2 mouse.
 * Also uses serial protocol to talk back to the host
 * and report what it finds.
 */

/*
 * Pin 2 is the mouse data pin, pin 3 is the clock pin
 * Feel free to use whatever pins are convenient.
 */
 
PS2Mouse mouse(11, 12);

void setup()
{
  Serial.begin(9600);
  mouse.init();
}

/*
 * get a reading from the mouse and report it back to the
 * host via the serial line.
 */
void loop()
{
  MouseInfo mouseInfo;
  mouse.getData(&mouseInfo);
  
  Serial.print(mouseInfo.status, DEC);
  
  //X change
  Serial.print("\tX=");
  Serial.print(mouseInfo.x, DEC);
  
  //Y change
  Serial.print("\tY=");
  Serial.print(mouseInfo.y, DEC);
  
  //Scroll change
  Serial.print("\tSc=");
  Serial.print(mouseInfo.scroll, DEC);
  
  //Left button down
  Serial.print("\tLB=");
  Serial.print(mouseInfo.leftClick, DEC);
  
  //Middle button down
  Serial.print("\tMB=");
  Serial.print(mouseInfo.middleClick, DEC);
  
  //Right button down
  Serial.print("\tRB=");
  Serial.print(mouseInfo.rightClick, DEC);
  
  //Cumulative X position
  Serial.print("\tCX=");
  Serial.print(mouseInfo.cX, DEC);
  
  //Cumulatie Y position
  Serial.print("\tCY=");
  Serial.print(mouseInfo.cY, DEC);
  
  //Cumulative scroll postion
  Serial.print("\tCS=");
  Serial.print(mouseInfo.cScroll, DEC);
  Serial.println();
  delay(500);
}

the code has many data to print, but i only need some, cumulative X and Y position. so i take only that part of the code…

sorry for the long reply, thank you very much for helping me…hope you enjoy reading my long code and explanation ;D ;D ;D

  Serial.print(mouseInfo.cX, DEC);

The DEC keyword is optional. It tells the Serial.print function that the value to be printed should be printed in base 10 format.

The comma is an argument separator.

The comma has multiple meanings in C. Another meaning is as an operator. I haven't found a good explanation of what the comma operator does. I do know that is often mis-used, as you are doing.

What happens to the sprintf output if you value mouseX correctly, as in mouseX = mouseInfo.cX; ?

even i`m using ,DEC or not the value is still the same,.. that is one of the reason why i don't give much attention to it..

the value of buff is still zero on the second and fourth number..

Would you try this code on your Arduino?

void setup()
{
  Serial.begin(9600);
  
  int a = 37;
  int b = 64;
  int c = 910;
  int d = 1;
  
  char buff[24];
  
  sprintf(buff, "%.3d %.3d %.3d %.3d", a, b, c, d);
  Serial.print("buff = [");
  Serial.print(buff);  Serial.print("]");
}

void loop()
{
}

I get exactly what I expect - buff = [037 064 910 001].

Note in that code that the sprintf format statement does not look like this:

  sprintf(buff, "%.3d"" ""%.3d"" ""%.3d"" ""%.3d", cmKanan, cmKanan, mouseX, mouseX);    //printing with the same digit for all numbers

okay let me try.. for the sprintf format statement, i`m trying different kind of statement to know where is the problem and i forgot to write it back to the original, the one that you showed me.

i’ve tried the code… if i use sprintf just like that, i got the data correctly… but why if i combine it with my program it would turn different?

what is the meaning of int buff[24]?

declaring buff that has 24 arrays? i tried changing it to buff[10] and it still print the same data.. so what does that number do?

char buff[24]; declares an array that has 24 elements (numbered 0 to 23).

Changing the 24 to 10,, when you are trying to store 3 * 4 + 3 characters in it is going the wrong way.

The next thing I would try would be Serial.print() each value before the call to sprintf.

ooh i see…

i’ve already tried using Serial.print for each value… the numbers are all fine and giving the expected value…since i tried using sprintf for the ease of serial reading, the numbers began to shows wrong value… so i assume i`m using the wrong sprintf maybe?

i`m using arduino uno and arduino-021… if that may be useful…

the numbers are all fine and giving the expected value

And those values are?

Next test.

sprintf(buff, "%.3d", cmKanan);
Serial.print("cmKanan: [");Serial.println("]");

sprintf(buff, "%.3d", cmKanan);
Serial.print("cmKanan: [");Serial.println("]");

sprintf(buff, "%.3d", mouseX);
Serial.print("mouseX: [");Serial.println("]");

sprintf(buff, "%.3d", mouseX);
Serial.print("mouseX: [");Serial.println("]");

What does this produce?

let me try...