Making 2 boards communicate in sync with each other over Bluetooth

Hi there,

Im having an issue where i am sending 3 pieces of data over the serial port through bluetooth from an Arduino Uno, which is being received by Bluetooth on a Arduino Nano.

The 3 pieces of data is a button (sending a 1), X values (range between 0-255 for serial transmission), and Y values (same as X).

The problem is that when these values are sent from the master board, the slave board is not receiving them in sync, meaning that the wrong values are read and sent to the outputs.

This results in my motors receiving the wrong values.

Any ideas on how I can send 3 pieces of integer data and receive so it is all in sync? Would be much appreciated.

Here is my master code:

int xAxis, yAxis, buttonNew;

int buttonPin = 6;
int xPin = A1;
int yPin = A2;

void setup() {
  Serial.begin(38400); 
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin,HIGH);
  pinMode(xPin,INPUT);
  pinMode(yPin,INPUT);
  
}

void loop() {
  buttonNew = digitalRead(buttonPin);
  xAxis = analogRead(xPin);
  yAxis = analogRead(yPin);

  Serial.write(buttonNew);
  delay(10);
  Serial.write(xAxis/4);
  delay(10);
  Serial.write(yAxis/4);
  delay(10);
}

and the slave code receiving these values:

void loop() {
  
  while (Serial.available() > 3) {
  buttonOld = buttonNew;
  buttonNew = Serial.read();
  Serial.print(buttonNew);
  delay(10);
  
  xAxis = Serial.read();
  xAxis = xAxis*4;
  Serial.print(xAxis);
  delay(10);
  
  yAxis = Serial.read(); 
  //yAxis = yAxis*4;
  Serial.print(yAxis);
  }
delay(10);

An example serial monitor line on the receiving end goes as:
112812811281281128128, or 1,128,128,1,128,128,1...
and sometimes out of sync, so 128,128,1,128,128,1...
128,1,128,128,1,128.

I have tried to keep the delays the same between both codes, however no luck to try and keep them in sync. When the programs are running, both bluetooth modules are paired correctly and immediately the master module begins sending the data to the slave

Take a look at the Serial Input Basics Tutorial.

It will show you how to configure, send and read a packet of data sent with start and end markers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.