Sending Integers through serial and reading them on the otherside

Hello.

I am trying to send Integer data across a serial link between two Arduino Uno’s. (new to Arduino) The problem I am running into is the Serial.read function systems to only work on bytes but the Int class is 2 Bytes long. How do you get around this issue? Im trying to send a serials of integers and use those integers on the other side to eventually set motor speed.

I have taken some code from other posts and tried smashing them together. The code on the Sending side seems to work correctly, but the values on the receiving side go negative once they get above 128.

If there is an easier way to send value data between two arduino’s, I have not found it yet. Thanks in advance for any help you can provide. I know there is more receive side code that is needed to dump the received info if it receives another < but I haven’t made it past the int-to-byte-to-int thing yet.

RECEIVING SIDE*****

const byte numBytes = 10;
int receivedBytes[numBytes];
static byte ndx = 0;
int i = 0;
boolean newData = false;

void setup() {
Serial.begin(9600);
Serial.println("");
}

void loop() {
recvWithStartEndMarkers();
showNewData();
}

void recvWithStartEndMarkers() {
static boolean recvInProgress = false;

char startMarker = ‘<’;
char endMarker = ‘>’;
char rc;
int rb;

while (Serial.available() > 0 && newData == false) {

if (recvInProgress == true) {
rb = Serial.read();
receivedBytes[ndx] = rb;
ndx++;
if (ndx >= numBytes) {
newData == true;
}
}
else {
rc = Serial.read();
if (rc == startMarker) {
recvInProgress = true;
Serial.println(“starting”);
ndx = 0;
}
}
}
}

void showNewData() {
if (newData == true) {
Serial.print(“This just in … “);
Serial.print(”<”);
for (i = 0; i >= ndx; i++) {
Serial.print(receivedBytes*);*

  • }*

  • Serial.print(">");*

  • ndx = 0;*

  • newData = false;*

  • }*
    }
    SENDING SIDE*******
    int lxPin = A1;
    int lyPin = A0;
    int lbuttonPin = 2;
    int rxPin = A2;
    int ryPin = A3;
    int rbuttonPin = 4;
    int lxPosition = 0;
    int lxPositionInt = 0;
    int lxDir = 0;
    int lyPosition = 0;
    int lyPositionInt = 0;
    int lyDir = 0;
    int lbuttonState = 0;
    int rxPosition = 0;
    int rxPositionInt = 0;
    int rxDir = 0;
    int ryPosition = 0;
    int ryPositionInt = 0;
    int ryDir = 0;
    int rbuttonState = 0;
    void setup() {

  • // initialize serial communications at 9600 bps:*

  • Serial.begin(9600);*

  • pinMode(lxPin, INPUT);*

  • pinMode(lyPin, INPUT);*

  • pinMode(rxPin, INPUT);*

  • pinMode(ryPin, INPUT);*

  • //activate pull-up resistor on the push-button pin*

  • pinMode(lbuttonPin, INPUT_PULLUP);*

  • pinMode(rbuttonPin, INPUT_PULLUP);*
    }
    void loop() {

  • readpins();*

  • convert();*

  • sendstuff();*

  • delay(100); // add some delay between reads*
    }
    void readpins() {

  • lxPosition = analogRead(lxPin);*

  • delay(100); // add some delay between reads*

  • lyPosition = analogRead(lyPin);*

  • delay(100); // add some delay between reads*

  • lbuttonState = digitalRead(lbuttonPin);*

  • delay(100); // add some delay between reads*

  • rxPosition = analogRead(rxPin);*

  • delay(100); // add some delay between reads*

  • ryPosition = analogRead(ryPin);*

  • delay(100); // add some delay between reads*

  • rbuttonState = digitalRead(rbuttonPin);*
    *} *
    void convert() {

  • if (lxPosition > 520)*

  • {*

  • lxDir = 1;*

  • lxPositionInt = map(lxPosition, 520, 1023, 0, 255);*

  • } else if (lxPosition < 504)*

  • {*

  • lxDir = 2;*

  • lxPositionInt = map(lxPosition, 0, 504, 0, 255);*

  • } else*

  • {*

  • lxPositionInt = 0;*

  • lxDir = 0;*

  • }*

  • if (lyPosition > 520)*

  • {*

  • lyDir = 1;*

  • lyPositionInt = map(lyPosition, 520, 1023, 0, 255);*

  • } else if (lyPosition < 504)*

  • {*

  • lyDir = 2;*

  • lyPositionInt = map(lyPosition, 504, 0, 0, 255);*

  • } else*

  • {*

  • lyPositionInt = 0;*

  • lyDir = 0;*

  • }*

  • if (rxPosition > 520)*

  • {*

  • rxDir = 1;*

  • rxPositionInt = map(rxPosition, 520, 1023, 0, 255);*

  • } else if (rxPosition < 504)*

  • {*

  • rxDir = 2;*

  • rxPositionInt = map(rxPosition, 504, 0, 0, 255);*

  • } else*

  • {*

  • rxPositionInt = 0;*

  • rxDir = 0;*

  • } *

  • if (ryPosition > 520)*

  • {*

  • ryDir = 1;*

  • ryPositionInt = map(ryPosition, 520, 1023, 0, 255);*

  • } else if (ryPosition < 504)*

  • {*

  • ryDir = 2;*

  • ryPositionInt = map(ryPosition, 504, 0, 0, 255);*

  • } else*

  • {*

  • ryPositionInt = 0;*

  • ryDir = 0;*

  • } *
    }
    void sendstuff() {

  • Serial.print("<");*

  • Serial.write(lxPositionInt);*

  • Serial.write(lxDir);*

  • Serial.write(lyPositionInt);*

  • Serial.write(lyDir);*

  • Serial.write(lbuttonState);*

  • Serial.write(rxPositionInt);*

  • Serial.write(rxDir);*

  • Serial.write(ryPositionInt);*

  • Serial.write(ryDir);*

  • Serial.write(rbuttonState);*

  • Serial.println(">");*

}

Save everyone the hassle of deciding where you left in the [i] indices, and edit your post to add in the code tags you missed out.

If there is an easier way to send value data between two arduino’s, I have not found it yet.

Below is receiving code I use for servos that captures incoming characters as a String, then converts the captured String into an integer for servo position command.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        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);
          myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          myservo.write(n);
        }

        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

PhatFarm:
but the values on the receiving side go negative once they get above 128.

Almost certainly that is because you are using a char when you should be using a byte or an int

A byte is an unsigned 8-bit number. A char is a signed 8-bit number - although people usually think of it just as a receptacle for Ascii characters - which are all between 0 and 127

You are using code from serial input basics and it was written with the assumption that the data would be printable characters. But it is the work of a moment to change char to byte.

If you want a more comprehensive way to receive all values from 0 to 255 have a look at the Arduino code in this Python demo.

As others have said, please use the code button </> so your code looks like this

…R