How to Read only one specific character on a line from Serial.

I want to be able to use two Arduino’s, one as a transmitter and one as a receiver for information when I move an Analog Joystick and a Rotary Encoder via the TX and RX pins on the Arduinos. But to get the X and Y axis to send together (more or less) you would at least have to send two characters along the serial pins and for the receiver to act, you’ll have to separate out the two signals.

How would you be able to (if you was printing them along one line,) get the receiving Arduino to separate the characters from one and other and just to focus on one character in a line rather than all of them at once.

If you want to see the code just ask and for any other information. :slight_smile:

Is this character always in the same place?
If so read n characters from the start of the line and then read the character you want into a variable and then process that variable.

If it is not always in the same place how do you recognise it when you look at it?

Yeah it is always constantly in the same place along the line. And could this work if I wanted to read two characters? Like instead of just reading 1, could I get it to read something like 20?

Serial.read reads one character from the serial buffer. You can use it as many times as you like.
The best bet is to read all the data into an array, and then use the array index to pick out as many or few characters as you want.

Joystick servo test code for arduino sending and arduino receiving:

Tx

//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.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  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 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

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

Rx

//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() {
  Serial.begin(9600);

  //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 = Serial.read();  //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: ");
          Serial.println(n);
          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);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          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
    }
  }
}

Ok. In the Rx code, could you explain the part that's copied from someone else's code? I would like to see if I can understand this so I can rewrite bits if I need to.

This bit. It would be appreciated if you could:

// auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          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);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          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
      }

The examples in serial input basics are simple reliable ways to receive data. There is also a parse example to extract the different parts of the received data.

...R

Very helpful Robin2 and has helped in general for serial communication and thanks for that ;D. However yes I can send and receive characters, but I want to take some of the characters out from the serial and get the receiving Arduino to do something (like move a servo like in zoomkat's example) but I am having a little trouble getting the Arduino to process this and to link these characters as to commands.

These characters will change however, like instead of a value staying at 0 and it could be from -9 to +9 and I want to know how I can get the Arduino to "watch" the character's position to see what that character will change to....... if that makes sense.

Ok. In the Rx code, could you explain the part that's copied from someone else's code? I would like to see if I can understand this so I can rewrite bits if I need to.

The servo library takes two types of numeric commands to control a servo. One format is a general representation of the rotation range of the servo (typically 180 deg) which would be 0-180, and the other format is the microsecond pulse with for the servo control (typically 500us-2500us) which will generally rotate the servo 180 deg. If the command value sent is >= 500, then servo.writeMicroseconds() would be used. If the value sent is less than 500, then servo.write() would be used.

wills265:
but I want to take some of the characters out from the serial

Don’t take the characters “out from serial”.
My examples take all the characters from serial and save them to a char array. When they have all been collected and saved you can easily find the parts you need within the char array. That’s what the parse example does.

Can you send your values as <xxx,yyy>? For example <75,123> to mean an x value of 75 and a y value of 123. The < and > just tell the Arduino when the data starts and ends.

…R

Ahh... got it. I merged your code for the parse and serial reading together and it works :D. And in answer to your question, all the characters are all printed along the same line.
Thanks :slight_smile: