The app does not connect to bluetooth

Dear @minalbero,
as far as the LCD is concerned please take a look to the following link:

It's a description on how to connect a 1602A LCD to an UNO board. In any case, should you need to change the pins assignment, you can follow the instructions reported therein.
The line of code where you configure the LCD pins seems to me wrong (no I2C should be referenced and the order of parameters seems messed).
In addition to that, in your Setup function you shall initalize the LCD with a line of code like: lcd.begin(cols,rows);

The following part of the code can be simplified as follows:
if ((switchState = digitalRead(switchPin)) == HIGH) on_off = '1'; else on_off = '0';

One "side comment" is: typically, to avoid spurious bouncing of the switches, it's better to select the input configuration as INPUT_PULLUP, this will enable the internal pullup resistor of the relevant input port. Obviously this leads to an "active_low" signal, but this is just a matter of software logic, while the benefit is to have a higher noise rejection.

A line which is not clear is also the following::
"receivedChar = on_off;"
why you overwrite the value received by the BT with the value previously set by the input switch ?

So, before going further in your logic, please take a moment to check that area of your code.
Cheers.

NNow I should have it fixed and I shouldn't have any more problems. Thanks very much to everyone :cowboy_hat_face:

I trolled, I don't know how to get information from arduino/hc-05 to the app. It must receive three values ​​that correspond to three figures and when it sends the signal it must increment its counter. Furthermore I have to put 2 timers, the first from when it is started and the other must be from when the last signal of the figure arrived. The blocks I have placed so far are these:

edit: The Arduino program is
(the program is incomplete as it is LCD and transmission of the 3 values ​​and therefore the 3 shapes):

#include <SoftwareSerial.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;

const int rs = A0;

const int en = 13;

const int d4 = 2;

const int d5 = 3;

const int d6 = 4;

const int d7 = 5;

int switchState = LOW;

char on_off = '0', receivedChar;

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);

}

void loop()

{

int currentSwitchState = digitalRead(switchPin);

if (currentSwitchState != switchState)

{

switchState = currentSwitchState;



if (switchState == HIGH)

{

  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);

Serial.println("Acceso");

int valoreS1 = analogRead(pinS1);

int valoreS2 = analogRead(pinS2);

int valoreS3 = analogRead(pinS3);

// Calcola la distanza in base al valore letto

float tensione1 = valoreS1 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)

float distanza1 = 27.86 * pow(tensione1, -1.15); // Formula empirica per convertire tensione in distanza

// Calcola la distanza in base al valore letto

float tensione2 = valoreS2 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)

float distanza2 = 27.86 * pow(tensione2, -1.15); // Formula empirica per convertire tensione in distanza

// Calcola la distanza in base al valore letto

float tensione3 = valoreS3 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)

float distanza3 = 27.86 * pow(tensione3, -1.15)-5; // Formula empirica per convertire tensione in distanza

if (distanza1 < 10 && distanza2 < 10 && distanza3 < 10)

{

if (distanza1 > 1 && distanza2 > 1 && distanza3 > 1)

  {

      int p1=p1+1;

      bluetooth.write(1);

  }

}

// Stampa la distanza sulla console seriale

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");

}

else

{

color(255, 0, 0);

digitalWrite(pinMotor1, LOW);

digitalWrite(pinMotor2, LOW);

Serial.println("Spento");

}

delay(1000);

}

/*int countObjects() {

// Simula la lettura da un sensore infrarosso

// Puoi personalizzare questa logica in base al tuo sensore reale

return random(1, maxObjects + 1); // Aggiunto 1 per ottenere un numero tra 1 e maxObjects inclusi

}

bool shouldProducePackage() {

return (objectCount[0] + objectCount[1] + objectCount[2]) >= 10;

}

void displayTotalPackages() {

lcd.clear();

lcd.print("Confezioni totali:");

lcd.setCursor(0, 1);

for (int i = 0; i < maxObjects; i++) {

lcd.print("Oggetto ");

lcd.print(i + 1);

lcd.print(": ");

lcd.print(totalPackages[i]);

lcd.print(" ");

}*/

Dear @minalbero,
there are still some issues in your approach.
If you want to ha a full-duplex coom's between the app and Arduino, you shall enable your app to receive BT messages when they arrive. To di so, you shall use a clock (a timer) that polls the BT input buffer often enough to be capable to catch characters as they arrive. Typically I use a 10 ms clock which verifies if any character has been received. If not, it exits immediately, if yes, it stays thare until the last character has been fetched.
The following image shows only the above. rationale; I don't have reported the asking for permission blocks that you shall maintain in yours.

Please pay attention to the "-1"number of characters: this means thet until the ending transmission character Linefeed (i.e. 0x0A or 10 decimal) is received, the ReceiveText block remains there waiting for any new character coming from the BT.
To accomplish this method, on Arduino side, the transmission to the BT shall be ended with a Serial.println(); instruction. So, if you transmit only one character it shall be done as Serial.println(character_to_be_sent);

A good example is (as always :star_struck:) on @Juan_Antonio's web site KIO4.com.
For instance this link:
Bluetooth. App Inventor. Comandos AT. Monitor Serie.
leads you to a couple of codes (both AI2 and Arduino) in which you can see how to perform a complete bidirectional communication. One step beyond.....
image

Is that okay?
Arduino, based on the piece that arrives, transmits 1, 2 or 3, and the app updates the counter of the detected piece.

I see trouble in the NorthEast.

You have a global Init from a value that should not exist yet.

You are missing an After Picking event.

Also, you are trying to receive text '1', '2', or '3' followed by \n but your transmission code is


[quote="minalbero, post:23, topic:101978"]
  bluetooth.write(1);
[/quote]

which looks more like it would send hex 0x01 rather than character '1\n'.

What should I do?

Study
https://www.google.com/search?q=Arduino+data+types

It's an emergency and I'm a beginner and it's due tomorrow and I have to solve this thing. Could you give me a more specific explanation?

bluetooth.println('1');

Same for '2' and '3'

Also check that your AI2 BT Delimiter is 10.

So it's correct?

#include <SoftwareSerial.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;

const int rs = A0;
const int en = 13;
const int d4 = 2;
const int d5 = 3;
const int d6 = 4;
const int d7 = 5;

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

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

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

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

if (switchState == HIGH)
{
  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);
//Serial.println("Acceso");
int valoreS1 = analogRead(pinS1);
int valoreS2 = analogRead(pinS2);
int valoreS3 = analogRead(pinS3);
// Calcola la distanza in base al valore letto
float tensione1 = valoreS1 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)
float distanza1 = 27.86 * pow(tensione1, -1.15); // Formula empirica per convertire tensione in distanza

// Calcola la distanza in base al valore letto

float tensione2 = valoreS2 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)
float distanza2 = 27.86 * pow(tensione2, -1.15); // Formula empirica per convertire tensione in distanza

// Calcola la distanza in base al valore letto

float tensione3 = valoreS3 * (5.0 / 1023.0); // Converte il valore analogico in tensione (0-5V)
float distanza3 = 27.86 * pow(tensione3, -1.15)-8; // Formula empirica per convertire tensione in distanza

if (distanza1 < 11.00 && distanza2 < 11.00)
{
if (distanza1 < 11.00 && distanza2 < 11.00 && distanza2 > 8.00 )
{
p1=p1+1;
bluetooth.write(1);

  }
  if (distanza2 < 8.00)
  {
      p2=p2+1;
      bluetooth.write(1);
      bluetooth.println('1');
  }
  if (distanza1 < 8.00)
  {
      p3=p3+1;
      bluetooth.write(1);
      bluetooth.println('1');
  }
  if (distanza3 < 11.00)
  {
      p3=p3+1;
      bluetooth.write(1);
      bluetooth.println('1');
  }

}
// Stampa la distanza sulla console seriale
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.println(p1);
Serial.println(p2);
Serial.println(p3);
}
else
{
color(255, 0, 0);
digitalWrite(pinMotor1, LOW);
digitalWrite(pinMotor2, LOW);
Serial.println("Spento");
}

delay(1000);
}

No.
You must remove every bluetooth.write(1) and replace it with the println.

You must read your code.
I should not have to read your code for you.

Sorry man :frowning:

But if I put this can I remove bluetooh.println('1')?

Tell me the difference between Serial.println() and bluetooth.println()

Which one goes out to AI2?

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