Hola. soy nuevo usando arduino, estoy haciendo un programa a grandes rasgos de la activacion de un motor con un paro de emergencia, necesito interrumpir la operacion del motor en cualquier momento del programa enviando un caracter desde la app, he intentado de todo pero no consigo realizar la interrupcion correctamente, por favor
#include "BluetoothSerial.h"
#include <Arduino.h>
#include <HCSR04.h>
//inicializacion BT
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
//settear motor a pasos
#define EN_PIN 22 //enable (CFG6)
#define DIR_PIN 23 //direction
#define STEP_PIN 21 //step
//variables a usar
int contadorbotellas = 0, estadodeposito, niveldeposito, pasosavanzados, vuelta = 0, i;
char recibido = '0';
float distanciainicial = 0;
void setup()
{
Serial.begin(9600);
SerialBT.begin("esp32");
//attachInterrupt(digitalPinToInterrupt(loop), paroemergencia, CHANGE);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); //deactivate driver (LOW active)
pinMode(DIR_PIN, OUTPUT);
digitalWrite(DIR_PIN, LOW); //LOW or HIGH
pinMode(STEP_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(EN_PIN, LOW); //activate driver
//LED BUILT_IN is GPIO 33
pinMode(33, OUTPUT); // Set the pin as output
//distanciainicial = hc.dist() ;
}
void loop() {
// put your main code here, to run repeatedly:
recibir();
}
void recibir()
{
if (SerialBT.available())
{
recibido = SerialBT.read();
}
switch (recibido)
{
case '1':
avanzar();
break;
case '2':
paro();
break;
default:
break;
}
}
void avanzar()
{
Serial.println("en movimiento");
for (i = 1; i <= 6; i++)
{
//encerder bomba
for (int j = 0; j <= 100; j++)
{
Serial.println(j);
motoractivado();
}
vuelta++;
contadorbotellas++;
Serial.print("vuelta: ");
Serial.println(vuelta);
Serial.print("Botellas llenas: ");
Serial.println(contadorbotellas);
if (i == 6)
{
recibido = '0';
vuelta = 0;
contadorbotellas = 0;
Serial.println("proceso finalizado");
}
SerialBT.print(vuelta);
SerialBT.print(";");
SerialBT.print(contadorbotellas);
SerialBT.print(";");
}
}
void motoractivado()
{
recibido = '1';
// Serial.println("High");
// digitalWrite(STEP_PIN, HIGH);
// delay(2);
// Serial.println("Low");
// digitalWrite(STEP_PIN, LOW);
// recibir();
// delay(2);
}
void paro()
{
recibido = '0';
digitalWrite(STEP_PIN, LOW);
Serial.println("detenido");
recibir();
}