question about baud rate for Serial.begin(baudrate

i`m confuse about the use of baudrate and how to manipulate that..

on my project i use the example about PING sensor and combine it with mouse interfacing based on martijnthe's mouse interfacing. the baud rate used for PING sensor example is 9600 and for the mouse is 38400..

if i want to use both coding, how to make it possible? do i have to rework the library or what?

any suggestion??

thank you very much...

You should post the 2 sketches of interest, so we can see how the serial port is being used. You can only have one device on each end of a serial port, so you can't talk to a ping sensor AND a mouse using the same serial port. I doubt that the sketch is using the serial port to talk to the ping sensor, though.

If you are using a MEGA or have multiple serial ports otherwise you could use NewSoftSerial http://sundial.org/arduino/index.php/newsoftserial/ to have another one. Check the examples!

With two serial ports you can connect one with the sensor and the other to the mouse and set the baudrate for each port to the appropiate speed.

this is the code for initialization and printing.

//ultrasonic sensor pin
const int pingPin = 7;

signed long x = 0;                        // Variables for our 'cursor'
signed long y = 0;                        //

int c = 0;                                // Counter variable for coordinate reporting



//variable initialization
long duration, inches, cm;


void setup()
{

  Serial.begin(9600);
}

void loop()
{
  mouse;
  ultrasonic;
  
  //serial printing
    if (c++ & 0x80)
  {                                       // Report the coordinates once in a while...
    Serial.print("x=");
    Serial.print(x, DEC);
    Serial.print(" y=");
    Serial.print(y, DEC);
    Serial.println();
    c = 0;                                // Reset the report counter
  }
  Serial.print(cm);
  Serial.print(" cm");
  Serial.println();
  
}

this is the ultrasonic function

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

  // The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  delay(100);
}

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;
}

this is the mouse coordinates code,

#include "PAN3101.h"
// #include "ADNS2051.h"
// #include "ADNS2610.h"
// #include "ADNS2083.h"

#define SCLK 2                            // Serial clock pin on the Arduino
#define SDIO 3                            // Serial data (I/O) pin on the Arduino

PAN3101 Optical1 = PAN3101(SCLK, SDIO);   // Create an instance of the PAN3101 object
// ADNS2051 Optical1 = ADNS2051(SCLK, SDIO);
// ADNS2610 Optical1 = ADNS2610(SCLK, SDIO);
// ADNS2083 Optical1 = ADNS2083(SCLK, SDIO);

void setup()
{
  Serial.begin(38400);
  Optical1.begin();                       // Resync (not really necessary?)
}

void mouse()
{

//  The status commands are available only for the PAN3101 and the ADNS2051:

//  Optical1.updateStatus();                // Get the latest motion status
//  if (Optical1.motion())                  // If the 'Motion' status bit is set,
//  {

    x += Optical1.dx();                   // Read the dX register and in/decrease X with that value
    y += Optical1.dy();                   // Same thing for dY register.....

//  }
  

}

the problem is still there, the serial begin for ultrasonic and mouse coord is different... how to use this both of example together??

i`m using arduino uno btw...

In the first bit of code:

  mouse;
  ultrasonic;

mouse and ultrasonic are undefined at this point. If they are meant to be function calls, the () are missing.

In all the code you posted, the Serial class is talking to the PC. Just pick one value. It doesn't matter which one. You could even pick a different value, like 57600 or 115200.

Just make sure the receiving application uses the same rate.

yeah missing () thanks for the correction...

ill try to pick one baudrate for both codes then.. ill let you know the progress..thank you very much for the help..