I'm having problems in communication between arduino and my app

Hello to the community! I'm new here so I apologise I'm posting this in the wrong category. I'm having problems in communication between my arduino Uno and app. the app and arduino are not responding when commands are sent. I do not have much knowledge in either arduino or app development so I apologise if it is a simple issue. I have embedded both the arduino and App inventor code here. Thanks in advance!

#include <SoftwareSerial.h>
#define IR_RIGHT 12
#define IR_LEFT 11
#define BL_RX 9
#define BL_TX 10
#define ALCOHOL 8
#define LED 7
#define LEFT_BLINKER 6
#define RIGHT_BLINKER 5
#define LDR 4
#define BUZZER 3

bool engine = false;
bool ir_right;
bool ir_left;
int mag;
int joyx;
int joyy;
int press;
int crashcount;
bool alc_s;
int acc_x;
int acc_y;
int acc_z;
char blvalue;
volatile int BT;
int st;
int time;

SoftwareSerial mySerialBT(BL_TX,BL_RX);

void BL_READ() {
  if (mySerialBT.available() > 0) {
      BT = mySerialBT.read();
      Serial.println(BT);
      if (73 == BT) {
        engine = true;
        mySerialBT.print("G");
        mySerialBT.println();
      } else if (82 == BT) {
        mySerialBT.print(blvalue);
        mySerialBT.println();
        mySerialBT.print("G");
        mySerialBT.println();
      } else if (74 == BT) {
        engine = false;
        mySerialBT.print("G");
        mySerialBT.println();
      } else {
        mySerialBT.print("R");
        mySerialBT.println();
      }
    }
}

void blink_right() {
   analogWrite(RIGHT_BLINKER, 255);
  delay(500);
   analogWrite(RIGHT_BLINKER, 0);
  delay(500);
}

void check() {
  value_checks();
  if ((ir_left | ir_right) == true) {
    alarm();
  } else {
     analogWrite(5, 0);
  }
  if (acc_x < 390) {
    blink_left();
  }
  if (acc_x > 470) {
    blink_right();
  }
  if ((acc_x > 600 | acc_y > 600) | acc_z > 600) {
    alarm();
    delay(1000);
    crashcount = crashcount + 1;
    if (crashcount > 10) {
      mySerialBT.print("C");
      mySerialBT.println();
      blvalue = "C";
    }
  }
  if (alc_s == false) {
    alarm();
  }
}

void value_checks() {
  press = analogRead(A5);
  joyx = analogRead(A2);
  joyx = analogRead(A3);
  acc_x = analogRead(A0);
  acc_y = analogRead(A1);
  acc_z = analogRead(A4);
  ir_left = digitalRead(IR_LEFT);
  ir_right = digitalRead(IR_RIGHT);
  alc_s = digitalRead(ALCOHOL);
}

void alarm() {
   analogWrite(3, 255);
  delay(500);
   analogWrite(3, 0);
  delay(500);
  pinMode(LED, OUTPUT);
   digitalWrite(LED, 1);
  delay(500);
  pinMode(LED, OUTPUT);
   digitalWrite(LED, 0);
  delay(500);
}

void blink_left() {
   analogWrite(LEFT_BLINKER, 255);
  delay(500);
   analogWrite(LEFT_BLINKER, 0);
  delay(500);
}

void joystick_checks() {
  if (press < 7) {
    mySerialBT.print("P");
    mySerialBT.println();
    blvalue = "P";
  }
  if (joyx < 30) {
    mySerialBT.print("A");
    mySerialBT.println();
    blvalue = "A";
  }
  if (joyx > 800) {
    mySerialBT.print("D");
    mySerialBT.println();
    blvalue = "D";
  }
  if (joyy < 30) {
    mySerialBT.print("W");
    mySerialBT.println();
    blvalue = "W";
  }
  if (joyy > 800) {
    mySerialBT.print("S");
    mySerialBT.println();
    blvalue = "S";
  }
}


void setup() {
  pinMode(RIGHT_BLINKER, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(LEFT_BLINKER, OUTPUT);
  pinMode(IR_LEFT, INPUT_PULLUP);
  pinMode(IR_RIGHT, INPUT_PULLUP);
  pinMode(ALCOHOL, INPUT_PULLUP);
  Serial.begin(9600);
  mySerialBT.begin(9600);
}

void loop() {
    BL_READ();
    if (engine == true) {
      check();
      joystick_checks();
    }
} 

Sorry I cannot read that colored blob. Try posting it using code tags.

I have found it is best to follow this development process.

  1. Get your program working exactly as desired with input from the Serial monitor or terminal program of your choice.

  2. Transfer the Serial input/output to Bluetooth using a Bluetooth Serial Terminal program App. I prefer to use Kai Morich's Serial Bluetooth Terminal

  3. Develop the custom app on App Inventor.

this app is part of a smart helmet me and my friend are making. It has two ir sensors to check if the driver is wearing the helmet, an alcohol sensor, a joystick to remotely control the phone ( answer, decline calls) three led. Two for the indicators and one alarm led. It also has an accelerometer for indicators and buzzer for alarm. I originally programmed the code in ardublock, so I don't think that it is syntax error. However , because of the lack of blocks in ardublock to fulfill all requirements, I switched to Arduino ide for some final editing

Ok I'll try it.

Sure is it the Arduino or app code?

The start engine button works. The helmet starts the checks and rings alarm if necessary. But then communication stops working. Nither the phone or helmet recieve Bluetooth messages.

Can I upload the app file instead of the image?

skip the first half:

presentation:

its for a robotics compitition.

app:
https://community.appinventor.mit.edu/uploads/short-url/uyqItx8AVMKCeZh7GqVkEKV2i2Q.aia

@Delta_G I am using an arduino uno the program is using around 20% of storage and memory. the code is working fine apart from Bluetooth communication at least. I also check for all the sensors they are all giving accurate readings the wiring is correct with the mapping of pins in the code there are no other issues power supply is normal 5 volts. The main components are on like a helmet that all strap together there and none of the wires are loose. Bluetooth hc05 wiring is correct. One way communication from phone to arduino is working

Is there communication from the the Arduino to the phone when using Serial Bluetooth Terminal app?

@cattledog Yes through the Bluetooth app, two way communication is working.

through the Bluetooth app, two way communication is working.

In that case, you certainly know to focus your efforts on the receiving side of the App inventor code.

Not an Arduino issue, and you might do better on the App Inventor forum for assistance.

@cattledog Ok thankyou. One question: how often do I need to read from the Bluetooth module using bluetooth.read(); because sometimes the Arduino does not respond?

how often do I need to read from the Bluetooth module using bluetooth.read(); because sometimes the Arduino does not respond?

if (mySerialBT.available() > 0) {

You are set up to read when anything shows up. But there are so many delays in your program, that the software serial input buffer can be loaded up depending on how fast the app is sending commands.

Do you see the Arduino fail to respond when the Morich Serial Bluetooth App is the sender?

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