2 variables over serial between 2 arduinos

Hi all, I am a little stumped with a, what I consider to be a simple problem.

I have 2 Arduino Uno's connected via pins 0 & 1 for serial communication. Arduino 1 has 2 potentiometer connected to analog pins A0 and A1. Arduino 2 has 2 motor speed controllers connected through PWM pins 3 and 5.

Arduino 1 needs to read in these values and send them via the serial pins to Arduino 2, which will them output those values accordingly to the motor speed controller via the PWM pins.

I currently have 1 Arduino controlling the motor speed controllers via the potentiometers, but for project expansion and cable limitation, I need to run an Arduino at both ends.

This is what I have so far, and i am definitely open to suggestions with improvements! Arduino 1 (controller)

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

void loop() {
 int motor0 = map(analogRead(A0), 0, 1023, 0, 255);
 int motor1 = map(analogRead(A1), 0, 1023, 0, 255);

  Serial.print("A");
  Serial.print(motor0);
  Serial.print(",");
  
  delay(10);

  Serial.print("B");
  Serial.print(motor1);
  Serial.print(",");

  delay(10);
}

Arduino 2 (receiver) is where I am having trouble, I know of the Serial.parseInt command and I feel its likely the best option or perhaps Serial.readStringUntil but im not sure how to implement these into the sketch.

Any help would be greatly appreciated. Regards, Josh

Best read this thread:- Serial Basics

Thanks Mike,

I'll have a read and post back if I have any questions.

Regards, Josh

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

@OP

1. Connect Arduino-1 and Arduino-2 using Software UART Ports (SUART Ports) and leave the hardware UART Ports (UART Ports) for the Serial Monitor and IDE for debugging and uploading of sketches.

2. Adjust the Arduino-1 sketch as shown below and read the comments for such adjustments.

#include<SoftwareSerial.h>    //needed to create SUART Port
SoftwareSerial SYART(4, 5); //SRX = DPin-4, STX = Dpin-5 RX/TX pin sod SUART Port

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);   //SUART Port is enabled at bd = 9600
}

void loop() 
{
   byte motor0 = map(analogRead(A0), 0, 1023, 0, 255);  //map will return byte size data
   byte motor1 = map(analogRead(A1), 0, 1023, 0, 255);

                                    //Serial.print("A");
   Serial.print('<');        //use < rather than A to follow @Robin2's style as a start mark          
   Serial.print(motor0); //data item-1 as ASCII codes 
   Serial.print(",");        //data item, separator
  
                                   //delay(10);              //unnecessary

                                   //Serial.print("B");     //unnecessary; you have data separator already
  Serial.print(motor1);  //data item-2 as ASCII codes
                                  //Serial.print(",");      //no need
  Serial.print('>');        //end mark of transmission frame
                                  //delay(10);
  Serial.println();         //newline character
  delay(1000);             //1-sec interval as a test interval
}

Arduino-2 Codes: (untested)

#include<SoftwareSerial.h>    //needed to create SUART Port
SoftwareSerial SUART(4, 5); //SRX = DPin-4, STX = Dpin-5 RX/TX pin sod SUART Port
char myData[50] = "";         //keep sufficient space

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);   //SUART Port is enabled at bd = 9600
}

void loop() 
{
   byte n = SUART.available(); //check if character has come from Arduino-1
   if(n !=0)
   {
      SUART.readBytesUntil('>', myData, 50);   //read and save all characters until > and excluding >
      //process myData[] array to retrieve numerical values of motor0 and motor1; you can use atoi() 
      //reset myData[] array 
   }

}

Many thanks for the info peoples!
I read through the Serial basics thread, there is a lot of great info in there, couple of things went over my head, but ill go back over those another day.

#include<SoftwareSerial.h>    //needed to create SUART Port
SoftwareSerial SYART(4, 5); //SRX = DPin-4, STX = Dpin-5 RX/TX pin sod SUART Port

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);   //SUART Port is enabled at bd = 9600
}

void loop() 
{
   byte motor0 = map(analogRead(A0), 0, 1023, 0, 255);  //map will return byte size data
   byte motor1 = map(analogRead(A1), 0, 1023, 0, 255);

                                    //Serial.print("A");
   Serial.print('<');        //use < rather than A to follow @Robin2's style as a start mark          
   Serial.print(motor0); //data item-1 as ASCII codes 
   Serial.print(",");        //data item, separator
  
                                   //delay(10);              //unnecessary

                                   //Serial.print("B");     //unnecessary; you have data separator already
  Serial.print(motor1);  //data item-2 as ASCII codes
                                  //Serial.print(",");      //no need
  Serial.print('>');        //end mark of transmission frame
                                  //delay(10);
  Serial.println();         //newline character
  delay(1000);             //1-sec interval as a test interval
}

Thank you GolamMostafa for hitting the code on the head for me, much appreciated. I have a question regarding the code you have written for me. On the code above, I use Serial.begin(9600) and SUART.begin(9600) in setup, does this mean to transmit I need to use SUART.print to send data across those lines, or is something tricky happening in the background with Serial.print?

Thanks again,
Josh

PsychodelicPenguin: I use Serial.begin(9600) and SUART.begin(9600) in setup, does this mean to transmit I need to use SUART.print to send data across those lines, or is something tricky happening in the background with Serial.print?

There are two separate communication channels. (1) Hardware UART Port which is being used by the MCU to communicate with Serial Monitor at speed 9600 bits/sec.

(2) Software UART Port which the MCU uses to exchange data between Arduino-1 and Arduino-2 at speed 9600 bits/sec. It can be different from that of UART speed; but, the SUART Port speeds of both Arduino should be the same.

does this mean to transmit I need to use SUART.print to send data across those lines,

Yes.

Hi ,
After hours of tweaking this to get it right, it finally works. I have used some code from the examples in the Arduino IDE and some code from GolamMostafa. I left it running for several hours and resulted with no errors. I’m posting this incase it helps someone else and to say thank you for your help thus far. If anyone sees any obvious “that’s the worst way to do that” problems, please let me know. I know in the master code, I didn’t need to add a for loop to read the incoming bytes, but by doing so I think its much easier to expand upon.

Regards,
Josh

Master board

#include <Wire.h>

byte inData[10];            //lots of room in the byte array as recommended
const int  motor0 = 3;    //pin designation for motor 0
const int  motor1 = 5;    //pin designation for motor 1


void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          //Set serial baud to 9600
  Serial.println("Master");    //Print on screen position of board
}

void loop()
{
  Wire.requestFrom(8, 2);      // request 4 bytes from slave device #8

  while (Wire.available()) {
    for (int i = 0; i <= 2; i++) {
      inData[i] = Wire.read(); - '0'; // Read 1 byte from the Wire buffer in to "inData[i]"  -'0' is to convert back to int from char.
    }
    
    Serial.print(inData[0]);                //Print data received on screen
    Serial.print(",");                          //Print seperator
    Serial.print(inData[1]);                //Print data received on screen
    Serial.println();

    analogWrite(motor0, inData[0]);   //PWM to output motor 0 port
    delay(10);                        
    analogWrite(motor1, inData[1]);   //PWM to output motor 1 port

    Wire.flush();
    delay(500);
  }
}

Slave Code:

#include <Wire.h>
const int pot0 = A0;     //pin designation for pot 0
const int pot1 = A1;     //pin designation for pot 1

void setup()
{
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event

  Serial.begin(9600);       //Set serial baud to 9600
  Serial.println("Slave");  //Print on screen position of board
}

void loop()
{
  delay(500);              //0.5-sec interval as a test interval

}

void requestEvent()
{
  byte motor0 = map(analogRead(pot0), 0, 1023, 0, 255);  //map will return byte size data
  byte motor1 = map(analogRead(pot1), 0, 1023, 0, 255);
  
  Serial.print(motor0);     //data item-1 as ASCII codes
  Wire.write(motor0);     //data item-1 as ASCII codes
  Serial.print(",");      //local seperator

  Serial.print(motor1);     //data item-2 as ASCII codes
  Wire.write(motor1);     //data item-2 as ASCII codes
}