Hello, I am making a joystick-controlled robotic car for my project. When an obstacle is detected by the ultrasonic sensors, it should send this data to the mobile app and the app will produce a sound to alert the user.
I am having issues with the app not receiving the data which is sent by the Arduino.
To test a simple scenario, I just want the app to make a 'beep' sound when the joystick is pressed. The Arduino IDE will Serial.println("j") just fine, but it is somehow not received by the app. I watched tutorials which used call BluetoothClient1.BytesAvailableToReceive > 0 but it didn't work. I changed the value to > -1 and got some progress. But still, it is not producing the sound when the joystick is pressed.
I am in need of help and I greatly appreciate any assistance I can get. Thank you
#include <Joystick.h>
#include <AxisJoystick.h>
#include <AFMotor.h>
#include <NewPing.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
//jotstick pins
#define joystick_SWPIN A10
#define joystick_XPIN A8
#define joystick_YPIN A9
//ultrasonic sensor #1 (front center)
#define TRIG1 22
#define ECHO1 24
//ultrasonic sensor #2 (left side)
#define TRIG2 26
#define ECHO2 28
//ultrasonic sensor #3 (right side)
#define TRIG3 30
#define ECHO3 32
//for car movement
int speeds = 100;
Joystick* joystick;
unsigned long debounceDuration = 50;
unsigned long lastTimeButtonStateChanged = 0;
byte lastButtonState = 0; //released
byte MovementMode = 0; //0=translation, 1=rotation
//initiate ultrasonic sensors
NewPing US1(TRIG1, ECHO1, 200);
NewPing US2(TRIG2, ECHO2, 200);
NewPing US3(TRIG3, ECHO3, 200);
void setup()
{
  Serial.begin(9600);
  joystick = new AxisJoystick(joystick_SWPIN, joystick_XPIN, joystick_YPIN);
  motor1.setSpeed(speeds);
  motor2.setSpeed(speeds);
  motor3.setSpeed(speeds);
  motor4.setSpeed(speeds);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  pinMode(joystick_SWPIN, INPUT_PULLUP);
}
void loop()
{
  int US1_distance = US1.ping_cm();
  int US2_distance = US2.ping_cm();
  int US3_distance = US3.ping_cm();
  /*
  Serial.print("Distance US1: ");
  Serial.print(US1_distance);
  Serial.print(" cm\n");
  delay(50);
  Serial.print("Distance US2: ");
  Serial.print(US2_distance);
  Serial.print(" cm\n");
  delay(50);
  Serial.print("Distance US3: ");
  Serial.print(US3_distance);
  Serial.print(" cm\n");
  delay(50);
  */
  //int obstacle = checkdistance(distanceInCm);
  if(MovementMode == 0) //MODE: TRANSLATION
  {
    if(joystick->readVRx()==0 && joystick->readVRy()>=253 && joystick->readVRy()<=759)
    {
      //Serial.println("UP");
      if(checkfront(US1_distance) == 0) //no obstacle
      {
        forward();
      }
      else
      {
        //Serial.println("OBSTACLE FRONT");
        stop();
      }
    }
    if(joystick->readVRx()==1023 && joystick->readVRy()>=253 && joystick->readVRy()<=759)
    {
      //Serial.println("DOWN");
      backward();
    }
    if(joystick->readVRx()>=253 && joystick->readVRx()<=739 && joystick->readVRy()==1023)
    {
      //Serial.println("LEFT");
     
      if(checkleft(US2_distance) == 0)
      {
        left();
      }
      else
      {
        //Serial.println("OBSACLE LEFT");
        stop();
      }
    }
    if(joystick->readVRx()>=253 && joystick->readVRx()<=759 && joystick->readVRy()==0)
    {
      //Serial.println("RIGHT");
      if(checkright(US3_distance) == 0)
      {
        right();
      }
      else
      {
        //Serial.println("OBSACLE RIGHT");
        stop();
      }
    }
    if(joystick->readVRx()>=0 && joystick->readVRx()<=253 && joystick->readVRy()>=0 && joystick->readVRy()<=253)
    {
      //Serial.println("FORWARD RIGHT");
      forwardright();
    }
    if(joystick->readVRx()>=0 && joystick->readVRx()<=253 && joystick->readVRy()>=759 && joystick->readVRy()<=1023)
    {
      //Serial.println("FORWARD LEFT");
      forwardleft();
    }
    if(joystick->readVRx()>=739 && joystick->readVRx()<=1023 && joystick->readVRy()>=759 && joystick->readVRy()<=1023)
    {
      //Serial.println("BACKWARDS LEFT");
      backwardleft();
    }
    if(joystick->readVRx()>=759 && joystick->readVRx()<=1023 && joystick->readVRy()>=0 && joystick->readVRy()<=253)
    {
      //Serial.println("BACKWARDS RIGHT");
      backwardright();
    }
  }
  else //MODE: ROTATION
  {
    if(joystick->readVRx()>=253 && joystick->readVRx()<=739 && joystick->readVRy()==1023)
    {
      //Serial.println("ROTATE LEFT");
      rotateleft();
    }
    if(joystick->readVRx()>=253 && joystick->readVRx()<=759 && joystick->readVRy()==0)
    {
      //Serial.println("ROTATE RIGHT");
      rotateright();
    }
  }
  unsigned long timeNow = millis();
 
  if(timeNow - lastTimeButtonStateChanged > debounceDuration)
  {
    byte buttonState = joystick->isPress();
    if(buttonState != lastButtonState)
    {
      lastTimeButtonStateChanged = timeNow;
      lastButtonState = buttonState;
      if(buttonState == 0) //button has been released
      {
        MovementMode = (MovementMode == 0) ? 1 : 0;
        //send to serial button has been pressed
        Serial.println("j");
      }
    }
  }
  if (moveTitle(joystick->multipleRead()) == "NOT")
  {
    //Serial.println("STOP");
    stop();
  }
}
String moveTitle(const Joystick::Move move)
{
  switch (move)
  {
    case Joystick::Move::NOT:
      return "NOT";
    case Joystick::Move::PRESS:
      return "PRESS";
    case Joystick::Move::UP:
      return "LEFT";
    case Joystick::Move::DOWN:
      return "RIGHT";
    case Joystick::Move::RIGHT:
      return "DOWN";
    case Joystick::Move::LEFT:
      return "UP";
    default:
      return "???";
  }
}
int checkfront(int US1_distance)
{
  if(US1_distance > 20 || US1_distance == 0) //no obstacle
  {
    return 0;
  }
  else //got obstacle
  {
    return 1;
  }
}
int checkleft(int US2_distance)
{
  if(US2_distance > 20 || US2_distance == 0) //no obstacle
  {
    return 0;
  }
  else //got obstacle
  {
    return 1;
  }
}
int checkright(int US3_distance)
{
  if(US3_distance > 20 || US3_distance == 0) //no obstacle
  {
    return 0;
  }
  else //got obstacle
  {
    return 1;
  }
}
void stop(){
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}
void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void backward() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void right() {
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}
void left() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}
void forwardright() {
  motor1.run(RELEASE);
  motor2.run(FORWARD);
  motor3.run(RELEASE);
  motor4.run(FORWARD);
}
void forwardleft() {
  motor1.run(FORWARD);
  motor2.run(RELEASE);
  motor3.run(FORWARD);
  motor4.run(RELEASE);
}
void backwardleft() {
  motor1.run(RELEASE);
  motor2.run(BACKWARD);
  motor3.run(RELEASE);
  motor4.run(BACKWARD);
}
void backwardright() {
  motor1.run(BACKWARD);
  motor2.run(RELEASE);
  motor3.run(BACKWARD);
  motor4.run(RELEASE);
}
void rotateleft() {
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}
void rotateright() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}
            















