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:
#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(" ");
}*/