The app does not connect to bluetooth

Now I have two other problems:

  1. now that I have the connection there is a conflict between what I transmit via bluetooth and the physical one
  2. the LCD screen doesn't work.
    To better understand this is the delivery:
    Design a device that allows you to count objects of different shapes that slide on a conveyor belt, and sort them.
    Objects with three different shapes slide on the belt.
    The shapes, seen from above and from the side, slide on the surface resting on the base, as in the lower view, but with a random angle of
    orientation.
    Device operating specifications:
    The device must recognize and count objects of each shape, and for each recognized shape a digital signal must be activated
    which will allow the object to be sorted by diverting it onto other belt paths intended for packaging, this last phase involves the collection
    of objects in packs of 10 pieces, for each type of object.
    Objects on the conveyor belt will be sensed using infrared devices.
    The quantity of objects counted and the number of packages made, for each type, must be displayed on an LCD display and also
    sent, via Bluetooth or WiFi, to a terminal (PC, smartphone, other) for viewing.
    The control device must also be equipped with a button that will allow, at any time, to start the conveyor belt or
    arrest him. The same button function must also be possible by sending a command from the remote terminal. When the tape stops they will be
    object detection sensors are also inhibited. The movement of the tape (active tape) must be indicated by the lighting of a
    Green LED. A red LED will indicate the tape stopped status. Conveyor belt motor will be driven (belt moving)
    via a low level digital signal, which will assume the high state when the motor is stopped (belt stopped).
    Technical specifications:
    The objects have homogeneous dimensions: square base with side 9cm and height 3cm; the elevated part of object B is a cube, in position
    central, with a side of 3cm, while the elevated part of object C is a parallelepiped measuring 3cm x 9cm x 3cm high placed on one side.
    The sorting signals must be of the impulsive type, active at a low level, with a duration of 20ms.
    The conveyor belt has a width of 20 cm.
    The time between the passage of two consecutive objects in front of the sensors is at least 200 ms, during which they must be
    all the actions described above are guaranteed.
    This is the Arduino program:

Blockquote#include
<SoftwareSerial.h>
#include "LCD03.h"
const int switchPin = 12; // the switch connects to pin 12
const int redPin = 10;
const int greenPin = 11;
const int pinMotor1 = 8;
const int pinMotor2 = 9;
// Configurazione dei pin per il display LCD
const int rs = A0; // Pin RS collegato ad A0
const int en = 13; // Pin E collegato al pin 13
const int d4 = 2; // Pin D4 collegato al pin 2
const int d5 = 3; // Pin D5 collegato al pin 3
const int d6 = 4; // Pin D6 collegato al pin 4
const int d7 = 5; // Pin D7 collegato al pin 5
LiquidCrystal lcd(I2C_ADDR, en, rs, d4, d5, d6, d7);
//
int switchState = 0; // variable for reading the pushbutton status
char on_off = '0'; // Change to character '0'
SoftwareSerial bluetooth(6, 7); // RX, TX - change these pins as needed
void color(unsigned char red, unsigned char green, unsigned char blue) // the color generating
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
}
void setup()
{
pinMode(switchPin, INPUT); // initialize the buttonPin as input
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
Serial.begin(38400);
bluetooth.begin(38400); // initialize SoftwareSerial for Bluetoot
}
/
/
void loop()
{
// read the state of the switch value
switchState = digitalRead(switchPin);
if (switchState == HIGH)
{
on_off = '1';
}
else if (switchState == LOW)
{
on_off = '0';
}
if (bluetooth.available() > 0)
{
char receivedChar = bluetooth.read();
Serial.print("Received char: ");
Serial.println(receivedChar);
receivedChar = on_off;
if (receivedChar == '1')
{
on_off = '1'; // Change to character '1'
//switchState = HIGH;
Serial.println("Acceso");//turn on
}
else if (receivedChar == '0')
{
on_off = '0'; // Change to character '0'
//switchState = LOW;
Serial.println("Spento");//Turn off
}
}
//switchState = digitalRead(switchPin);
Serial.print("Switch state: ");
Serial.println(switchState);
Serial.print("on_off value: ");
Serial.println(on_off);
if (switchState == HIGH && on_off == '1') // if it is, the state is HIGH
{
color(0, 255, 0); // turn the led on
digitalWrite(pinMotor1, HIGH);
digitalWrite(pinMotor2, HIGH);
lcd.clear();
lcd.print("Oggetto ");
lcd.print(pieceType);
lcd.print(": ");
lcd.print(objectCount[pieceType - 1]);
if (shouldProducePackage()) {
for (int i = 0; i < maxObjects; i++) {
totalPackages[i] += objectCount[i];
objectCount[i] = 0;
}
displayTotalPackages();
}
else if (switchState == LOW || on_off == '0')
{
color(255, 0, 0); // turn the led off
digitalWrite(pinMotor1, LOW);
digitalWrite(pinMotor2, LOW);
}
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,
I don't understand these settings:

// Configurazione dei pin per il display LCD
const int rs = A0; // Pin RS collegato ad A0
const int en = 13; // Pin E collegato al pin 13
const int d4 = 2; // Pin D4 collegato al pin 2
const int d5 = 3; // Pin D5 collegato al pin 3
const int d6 = 4; // Pin D6 collegato al pin 4
const int d7 = 5; // Pin D7 collegato al pin 5
LiquidCrystal lcd(I2C_ADDR, en, rs, d4, d5, d6, d7);

If you use an I2C LCD, the pins to be used are only 2 (SDA and SCL) plus +5Vdc and GND. From where have you got these code lines ? What type of LCD are you using ?

On the web I've found something similar but no I2C_ADDR is used:
image
Where is lcd.begin(cols,rows) in your code ?

And what this means ?

  1. now that I have the connection there is a conflict between what I transmit via bluetooth and the physical one
    (me lo ridici in Italiano ? :grin:)

Ciao.

LCD che sto usando è 1602A.
Con:

intendo che per controllare i motori dovrebbe "prendere" l'ultimo dato che arriva, indipendentemente che sia switchState o on_off, solo che per l'accensione è cosi mentre per spegnerli ci devono essere entrambe le condizioni (switchState == LOW e on_off == '0').Non so come gestire questo a livello logico.
Spero di essere stato chiaro.

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?