Serial Joystick

Hello, I am doing a project, this project is about two arduinos:

Arduino 1: It has some joysticks, and buttons, and 1 antenna to comunicate to arduino 2

It sends this:

0,0,0,0,0,1234,1234,1234,1234,1234,1234,1234,1234,1234,1234,0,0,0,0,0,0,0,0,0,0,0. \n
  [states]                          [joysticks positions]                        [buttons]
                                              0 to 1024                                   0 or 1

Arduino 2: It has got motors, and antenna too. In this arduino I have the problem. I don't know how make a code to read the caracthers.

Help me please! Thanks!

Can you read one character and print it ? would be a good place to start.

Probably the easiest thing to do is develop your decoding approach first using the serial monitor, then develop the sending code that fits the decoding method. It may be easier to send individual commands in a single packet such as 0,1024,0\n instead of a single large packet with everything in it.

One of the examples in serial input basics will probably do what you want.

The 3rd example (with start- and end-markers) would be best. There is also a parse example.


It sends this:

Code: [Select]

0,0,0,0,0,1234,1234,1234,1234,1234,1234,1234,1234,1234,1234,0,0,0,0,0,0,0,0,0,0,0. \n [states] [joysticks positions] [buttons] 0 to 1024 0 or 1

Did you decide that format or is it fixed in some way and cannot be changed ?

You could use a struct. Store the values, send it. Then on the other side using the exact same struct, you receive the data and unpack it.

struct JoyStick { byte states; // bit shift in data int J_stick_pos[10]; // drop the values in by index int buttons; // bit shift in data }

I made the below code to do something similar with joysticks and servos.

tx code

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
  Serial.println("testing multi pot servo");  

void loop()
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    oldval1=newval1; //set the current old value

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
  delay(50);  //to slow loop for testing, adjust as needed

rx code

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
          Serial.print("writing Microseconds: ");
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
          Serial.print("writing Angle: ");
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
         readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString