The app does not connect to bluetooth

sorry man I trolled

what do I need to change in the app?

Dear @minalbero,
what @ABG has already said is that in your Arduino code you shall remove any "bluetoot.write" and substitute them with a bluetotth.println(character); this accomplishes to the requirement of closing any transmission toward your app with a Linefeed character (i.e. 10 decimal or 0x0A hexadecimal). Further, I see that you've followed my previous post's hints, and you've used my blocks where the delimiter byte '10' is set in the screen1 initialize event, so that's fine.
But, in your last code you always use bluetooth.println('1'); despite the value of distanza 1,2,3 . The question is: shouldn't you transmit '1' , '2', '3' ? The logic is not clear to me.

What @ABG has already also said is that you cannot initialize a global variable with a value that will be available only after a list selection has been done. This means that the global variable shall be initialized with an empty value, and only after the selection has been done, you can then assign the (selected) value to that variable. Otherwise the behaviour can be unexpectedly random.

Lastly:
p1, p2,p3 are integers, incremented by your algorithm, so before sending them via BT to your app you shall convert them in strings. Then you'd ,better use the String conversion function.
Something like:
string ToBeSent = String(p1); // the same applies to p2 and p3
bluetooth.println(ToBeSent);

Of course if you update the number of parts in yourArduino code, you shall not update them also in your app.... :flushed:

With a blank value, is one of these two examples okay or neither?
image
image

Neiher of the two,
I've said an "empty" variable. Thiat means a variable of the wanted type, but empty.
In this case the type is text, therefore this is a textual empty variable :
image

Thank you very much, when I try with the physical project I will update you.

Incrociamo le dita !! :crossed_fingers: :crossed_fingers: :crossed_fingers: :grin: :grin: :grin:

Grazie e speriamo bene😁

The new app always closes every time I launch it and it's like a picture because I can't interact with any buttons


This is the app:

I also need to add these 2 things to the app:
• Show the total time passed since the last reset (automatic refresh every certain time)
• Show the time since the last reception.

Do not use a while loop to connect
Also see the instructions below about how to correctly ask for permissions before connecting

Taifun


(Canned Response ABG - Bluetooth non-BLE SCAN Permission Blocks)

The easiest solution, for immediate relief
(from @Barry_Meaker) ...

I had the same issue. The problem is your app does not have permission to see nearby devices. The solution is to give your app permission on your phone (no code changes in your app).

on your phone,

  • goto settings
  • search for your app
  • in App Info for your app select Permissions
  • change Nearby Devices from Not Allowed to Allowed
  • Done

By the way, the very first time you run the app, Android will ask if you want to grant the app this permission. If you say no, or ignore the pop-up, the permission will be set as Denied. Android will not ask again.

A more complex approach, for professional app development:

See Bluetooth liste of devices deosn't work anymore - #7 by Anke
Special note for Xiaomi devices:
I have an error with bluetooth on android 12, Xiaomi Poco X3 NFC - #20 by Patryk_F

Thanks you but it isn't the problem

Remove the while loop and ask for permission correctly
If it still does not work, provide a screenshot of your updated relevant blocks

Taifun

Thank you.
Now the app doesn't stop but still doesn't receive anything from the circuit. Can you find the cause?
This is the Arduino code:

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

const int switchPin = 12;
const int redPin = 10;
const int greenPin = 11;
const int pinMotor1 = 8;
const int pinMotor2 = 9;

const int pinS1 = A5;
const int pinS2 = A4;
const int pinS3 = A3;

int p1, p2, p3, pinLCD;
int switchState = LOW;
char on_off = '0', receivedChar;

LiquidCrystal lcd(A0, 13, 2, 3, 4, 5);

SoftwareSerial bluetooth(6, 7);

void color(unsigned char red, unsigned char green, unsigned char blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
}

void setup()
{
pinMode(switchPin, INPUT);
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
Serial.begin(9600);
bluetooth.begin(38400);
lcd.begin(16,2);
}

void loop()
{
int currentSwitchState = digitalRead(switchPin);

if (currentSwitchState != switchState)
{
switchState = currentSwitchState;

if (switchState == LOW)
{
  on_off = '1';
}
else
{
  on_off = '0';
}

Serial.print("Switch state: ");
Serial.println(switchState);
Serial.print("on_off value: ");
Serial.println(on_off);

}

if (bluetooth.available() > 0)
{
receivedChar = bluetooth.read();
Serial.print("Carattere ricevuto: ");
Serial.println(receivedChar);

if (receivedChar == '1')
{
  on_off = '1';
}
else if (receivedChar == '0')
{
  on_off = '0';
}

}

if (on_off == '1')
{
color(0, 255, 0);
digitalWrite(pinMotor1, HIGH);
digitalWrite(pinMotor2, HIGH);
int valoreS1 = analogRead(pinS1);
int valoreS2 = analogRead(pinS2);
int valoreS3 = analogRead(pinS3);

float tensione1 = valoreS1 * (5.0 / 1023.0);  
float distanza1 = 27.86 * pow(tensione1, -1.15);  

float tensione2 = valoreS2 * (5.0 / 1023.0);  
float distanza2 = 27.86 * pow(tensione2, -1.15); 

float tensione3 = valoreS3 * (5.0 / 1023.0); 
float distanza3 = 27.86 * pow(tensione3, -1.15)-8;

if (distanza1 < 12.00 && distanza2 < 12.00)
{
  if (distanza1 < 9.70 && distanza1 > 6.00 && distanza2 < 9.70 && distanza2 > 8.00 && distanza3 < 13 )
  {
    p1=p1+1;
    bluetooth.println('1');
    lcd.clear();
    pinLCD = map(A0, 0, 1023, 0, 180);
    lcd.setCursor(0, 0);
    lcd.print("Pezzo A");
  }
  if (distanza2 < 10)
  {
    p2=p2+1;
    bluetooth.println('2'); 
    lcd.clear();
    pinLCD = map(A0, 0, 1023, 0, 180);
    lcd.setCursor(0, 0);
    lcd.print("Pezzo B");
  }
  if (distanza1 < 8.00 || distanza3 < 8.00)
  {
    p3=p3+1;
    bluetooth.println('3');
    lcd.clear();
    pinLCD = map(A0, 0, 1023, 0, 180);
    lcd.setCursor(0, 0);
    lcd.print("Pezzo C");
  }
}
Serial.println();
Serial.print("Distanza1: ");
Serial.print(distanza1);
Serial.println(" cm");
Serial.print("Distanza2: ");
Serial.print(distanza2);
Serial.println(" cm");
Serial.print("Distanza3: ");
Serial.print(distanza3);
Serial.println(" cm");
Serial.print("Pezzo A : ");
Serial.println(p1);
Serial.print("Pezzo B : ");
Serial.println(p2);
Serial.print("Pezzo C : ");
Serial.println(p3);

}
else
{
color(255, 0, 0);
digitalWrite(pinMotor1, LOW);
digitalWrite(pinMotor2, LOW);
Serial.println("Spento");
}

delay(1000);
}
This is the app:

I notice several issues ...

  • You don't have any visual feedback in the app to tell you if you are connected or not.

  • You enable Clock2, but your Clock Timer is for Clock1.

  • You lack any debugging output to show the raw incoming data.

  • You lack anything to show when that incoming data arrived.

  • You have a Before Picking event, presumably for a List Picker, but no After Picking event for that List Picker. People who use List Pickers instead of List Views seem to run into trouble on this board more than those who use ListViews for their connections.

Now I have fixed the clock and I am sure that it is connected to the hc-05 because it flashes twice. My main problem is I can't get the app to receive anything. I am only sending you the app as I have not changed anything inherent in the Arduino code.

Work on that next.

Now the app receives the signal and increases the counter but image4 (which is the image of the last piece passed) becomes the correct one but immediately afterwards returns with the first one that was received.

Since image4 is only updated in response to incoming data, you need a log to see what came in and when.

Here is a simple log you can incorporate:

Thanks man but I solved in this way: