The Bluetooth receive or code error

Hi,
I am testing the sketch below, the Serial monitor shown the data received jump and the motor is also beating, where the 'xAxis=1' and 'xAxis=-1' came from?
The data transmitted from MIT APP and the ball be moved smoothly.
Thanks
Adam

//#include <SoftwareSerial.h>
//SoftwareSerial BT(2, 3); // RX, TX

#define enA 8
#define in1 4    /// was: 4/5/6/7, MDF: 9/10/11/12 for CAR2 
#define in2 5

#define in3 6
#define in4 7
#define enB 9

int xAxis = 100, yAxis = 100;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600); // Default communication rate of the Bluetooth module

  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);

  delay(500);
}

void loop() {
  // Default value - no movement when the Joystick stays in the center
  //xAxis = 140;
  //yAxis = 140;

  // Read the incoming data from the Smartphone Android App
  while (Serial1.available() >= 2) {  //// was >=2  to >0 not effect 
    xAxis = Serial1.read();

    yAxis = Serial1.read();
 

    Serial.print("L 45xAxis=");
    Serial.println(xAxis);

    Serial.print("L 48yAxis=");
    Serial.println(yAxis);

  }
  delay(10);

  // Makes sure we receive corrent values
  // need add 

  if (xAxis > 90 && xAxis < 110 && yAxis > 90 && yAxis < 110) {
    Stop();
  }


  if (yAxis > 90 && yAxis < 110) {

    if (xAxis < 90) {  ////  seems heading to buttom is Forward, and the left is fight now. when facing the phone's Image. 
      
      turnLeft();     ////// WAS:  turnRight(); 
      motorSpeedA = map(xAxis, 90, 60, 0, 255);
      motorSpeedB = map(xAxis, 90, 60, 0, 255);
    }

    if (xAxis > 110) {
     turnRight();    ////// WAS:  turnLeft(); 
      motorSpeedA = map(xAxis, 110, 220, 0, 255);
      motorSpeedB = map(xAxis, 110, 220, 0, 255);
    }

  } else {

    if (xAxis > 90 && xAxis < 110) {

      if (yAxis < 90) {
        forword();
      }
      if (yAxis > 110) {
        backword();
      }

      if (yAxis < 90) {
        motorSpeedA = map(yAxis, 90, 60, 0, 255);
        motorSpeedB = map(yAxis, 90, 60, 0, 255);
      }

      if (yAxis > 110) {
        motorSpeedA = map(yAxis, 110, 220, 0, 255);
        motorSpeedB = map(yAxis, 110, 220, 0, 255);
      }

    } else {

      if (yAxis < 90) {
        forword();
      }
      if (yAxis > 110) {
        backword();
      }

      if (xAxis < 90) {
        motorSpeedA = map(xAxis, 90, 60, 255, 50);
        motorSpeedB = 255;
      }

      if (xAxis > 110) {
        motorSpeedA = 255;
        motorSpeedB = map(xAxis, 110, 220, 255, 50);
      }

    }
  }

  //Serial.print(motorSpeedA);
  //Serial.print(",");
  //Serial.println(motorSpeedA);

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}


void forword() {
  Serial.println("forword");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void backword() {
  Serial.println("backword");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnRight() {
  Serial.println("turnRight");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnLeft() {
  Serial.println("turnLeft");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void Stop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("stop");
}

Serial monitor:

forword
L 47xAxis=101
L 50yAxis=111

backword
backword
L 47xAxis=1
L 50yAxis=100
turnLeft
L 47xAxis=113
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=-1
forword
forword
forword
L 47xAxis=99
L 50yAxis=-1
L 47xAxis=120
L 50yAxis=-1

forword
forword
L 47xAxis=1
L 50yAxis=-1
forword
forword
L 47xAxis=100
L 50yAxis=-1
forword
forword
L 47xAxis=138
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=101
turnLeft
L 47xAxis=145
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=100
turnLeft
L 47xAxis=150
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=-1
L 47xAxis=99
L 50yAxis=154

backword
backword
L 47xAxis=1
L 50yAxis=-1
L 47xAxis=98
L 50yAxis=158

backword
L 47xAxis=1
L 50yAxis=-1
L 47xAxis=97
L 50yAxis=-1
L 47xAxis=163
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=-1
forword
forword
L 47xAxis=96
L 50yAxis=-1
L 47xAxis=170
L 50yAxis=-1

forword
L 47xAxis=1
L 50yAxis=95
turnLeft
L 47xAxis=179
L 50yAxis=-1
forword

When the read function returns -1 it means that the serial receive buffer is empty. Does your MIT app program send characters like line feed and/or carriage return to terminate the serial data? If so the MIT app may be sending 3 or 4 bytes instead of just 2 (probably 3).

Robin2's serial input basics tutorial may help with your reception and parsing of serial data.

1 Like

Great!
it was a more bytes error.