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!

crashguardfinal_checkpoint1.aia (270.0 KB)

here is the arduino code:

crashguard2_copy_20231230185936.ino (3.2 KB)

Images:

code:

#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();
    }
} 

Dear @Space,
what kind of Bluetooth shield have you connected to your UNO ?
A HC05 or HC06 or something else?
If you are using a HC05 or HC06, have you reversed the Tx and Rx pins between the UNO and the shield ?
Snce you say you are a "beginner", my hint is before trying to make working a complicated code, start with a simple one: just send a character from AI2 to your Arduino board, and show it on the Serial Monitor of the PC. As a second step, instruct your UNO to send back the same character to Ai2 and show this feedback into a label.
Only after you are done with such a basic code, you can build your final one.
The two files here below do the "simple job" (I've just posted them a couple of days ago for another user, don't care if the filename of AI2 .aia refers to an ESP32).
Please be aware that in my AI2 code the BT address is fixed into a text constant: you shall in place write the one of yours.
Best wishes

BT_to_ESP32.aia (24.9 KB)

BT_Send_Monitor.ino (1.9 KB)

He wrote arduino UNO and he use softwerserial to communicate with the BT module so I would guess he use a HC05 or HC06 BT.

I looked at your blocks before looking at your sketch, so this post is only on problems with your blocks independent of whatever is going on in your sketch.



Both these Button Click events have the equivalent of wait loops in them, which won't work in AI2. The global variable being tested in the while loop will never get a chance to change value.

For details, see the articles on Event Based Programming at


Your Clock Timer should check for connectivity in an outer if/then/else test, to get a chance to announce disconnection before looking for incoming bytes.

I see you are using a ListView as an incoming data log.
Are you sure just creating an Element is enough to get that Element into the Elements list?
Also, that comparison to true is not documented, as far as I know.

Further down on the input comparison ladder, you switch from text comparison to numerical ascii code comparison. That won't work, since codes (82, 71) will be compared as two character texts against that single byte incoming text value.

I've connected an HC05 Bluetooth module to the Arduino.

So, before trying with a more complex code (both AI2 and Arduino) I suggest you to use my super-simple code to verify that the BT communication works.
Then check what @ABG has said, correct your code and go further.

PS provided that the hardware connection is correct (Tx-UNO to Rx-HC05 and Rx-UNO to Tx-HC05) and the baudrate as well (9600 ? )

ok. how does listview elements creation work?

I have found it simpler and more conservative to copy the ListView Elements into a variable, add the created Element to that list variable, then copy the whole list from the variable back into ListView.Elements

Ok. I will test the code and get back if there are issues

the app communication is not working

OK, since on my desk it works, this means that you have some differences in your hardware, or release of Android, that cause the malfunction. Any error message ?

Its working now!

You marked "solution" the previous post: is it really so ?
If yes, there is no need to go further.
Otherwise, since you say that it's connecting but not communicating, in my understanding this means that something is still not working.

Therefore, to verify which part is not working (AI2 or Arduino) I suggest to download fron google playstore the (free) app "Serial Bluetooth Terminal" and install it on your phone. With that app you should be in degree to echange messages with Arduino.
Are you using my Arduino code for HC05 ? Have you correctly reversed Tx and Rx pins ? Are you sure that the baudrate between UNO board and HC05 is the "good" one ? If it is not working with 9600, you can try 19200 or 38400.
Can you post a photo of your hardware ? (UNO+HC05+wiring)

no. communication from phone to arduino is working but not arduino to phone
sure.

Whether you are using my code:
image
please be aware that Tx and Rx are intended on UNO side, not on HC05, therefore the wiring you did, is a direct one, and not reversed.
Please cross the blue wire with the yellow one.
(though this is not explaining me how the comm's from AI2 to Arduino works and not the oppposite :flushed:)

@uskiara I reversed the tx and Rx in the code instead of the wiring. That's how the communication worked

Sorry but I don't agree: the HW pins must be reversed otherwise you get a conflict between input and output and you can damage the ports.
Please leave the SW as original, and reverse the wires.The wires MUST be crossed.

@uskiara ok its working now! should i compare for normal text instead of ascii values in my arduino code then?

Sure, if you send a character (i.e. 'A') from your app, you shall compare a character also in Arduino; something like:
at variables initializtaion:
char InputData = ' '; // please note that you cannot initialize an empty char but a space/blank

during receiving:
InputData = BTserial.read();
if (InputData == 'A') {do the job for 'A'};
else if (InputData == 'B') {do the job for 'B'};
and so on.

ok I'll make the modifications. also, what should the encryption type of the bluetoothclient in the app be?