Serial Comunication with Arduino and XBee

Hello all,
I am trying to follow the following guide to make two LEDs change PWM duty cycle based off of two potentiometers: Serial Comunication with Arduino and XBee - Tutorials - RobotShop Community

The code is outdated and I am trying to change it to make it work. I know the SYNC char is no longer used, and I tried replacing these lines of code using Serial.write instead. However, when I look at my serial monitor, The characters are all unrecognized symbols. I could really use some insight on how to fix this. any help would be amazing. Here is the code I have:
Sending Side:

// SENDER

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(2);
analogValue5 = analogRead(5);

val2 = map(analogValue2, 0, 1023, 253, 0); // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);

Serial.write(254); //SYNC char
Serial.write(val2);

Serial.write(255); //SYNC char
Serial.write(val5);

delay(150);
}

Receiving Side:

// RECIEVER

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

pinMode (5, OUTPUT);
pinMode (6, OUTPUT);

delay(1000);

}

void loop() {

if (Serial.available()) {
incomingByte = Serial.read();

Serial.print(int(incomingByte));

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print(" Sensor 1 = ");
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print(" Sensor 2 = ");
Serial.print(int(sensor2));
}
}

analogWrite (5, sensor1);
analogWrite (6, sensor2);
}

Your sender program is writing an 'int' which is two bytes. Your receiver is reading one byte as the value.

If you want good, reliable Serial communcation, read this thread and follow its guidance.

blh64:
If you want good, reliable Serial communcation, read this thread and follow its guidance.

Or better still, you can use this serial transfer library to automatically encode and parse serial packets.

Here’s the example code:

TX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

RX Arduino:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

How would I implement this code for two inputs and outputs then?

westonhayle:
How would I implement this code for two inputs and outputs then?

This should do the trick:

TX:

// SENDER
#include <SoftwareSerial.h>
#include "SerialTransfer.h"

SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;

void setup()
{
  Serial.begin(19200);
  mySerial.begin(19200);
  myTransfer.begin(mySerial);
}

void loop()
{
  // read analog input
  int analogValue2 = analogRead(2);
  int analogValue5 = analogRead(5);

  myTransfer.txBuff[0] = (analogValue2 >> 8) & 0xFF;
  myTransfer.txBuff[1] = analogValue2 & 0xFF;

  myTransfer.txBuff[2] = (analogValue5 >> 8) & 0xFF;
  myTransfer.txBuff[3] = analogValue5 & 0xFF;

  myTransfer.sendData(4);

  delay(150);
}

RX:

// RECIEVER

#include <SoftwareSerial.h>
#include "SerialTransfer.h"

SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;

void setup()
{
  Serial.begin(19200);
  mySerial.begin(19200);
  myTransfer.begin(mySerial);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  int sensor1, sensor2;
  
  if(myTransfer.available())
  {
    if(myTransfer.bytesRead == 4)
    {
      sensor1 = (myTransfer.rxBuff[0] << 8) | myTransfer.rxBuff[1];
      sensor2 = (myTransfer.rxBuff[2] << 8) | myTransfer.rxBuff[3];

      Serial.print("New Data: "); Serial.print(sensor1); Serial.print(" "); Serial.println(sensor2);
      
      analogWrite(5, sensor1);
      analogWrite(6, sensor2);
    }
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}