Ardino MEGA se reinicia, bolquea cuando realiza el programa.

Buenas..

Escribo para solicitar ayuda en este proyecto, en base al proyecto estoy aprendiendo a utilizar y programar con aurdino. Mis bases de programación son muy básicas por ello me cuesta mucho entender a la hora de programar y ordenar la escritura, así mismo cuesta entender cada tutorial de la página a falta de vocabulario técnico.

El proyecto consiste en realizar una cuenta en el tiempo que tarda un interruptor en abrir y o cerrar, al realizar las instrucciones de abrir y luego cerrar la pantalla muestra los datos obtenidos de la cuenta, luego de ello se debería volver a realizar la operación ya sea de ''abrir'' o ''cerrar''. Pero en este punto aurdino se cuelga, la pantalla muestra unos caracteres raros, se desfasa la escritura y ademas de ello el programa realiza la operación de ''abrir'' o ''cerrar'' pero este omite los datos y la escritura del tiempo que tardo la operación. Esto sucede hasta que reseteo el programa, luego nuevamente se vuelve a trabar después de una vuelta completa del programa.

Seguramente en algún punto estoy haciendo todo mal, utilizando mal las librerias y o dando muchas vueltas en el programa.

Espero que me puedan ayudar a solucionarlo.

El código esta adjunto

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
SoftwareSerial BT(10, 11); //10 rx y 11 tx
int OCO, OCO1, OCO2 = 0;
float TR1, TS1, TT1, TR2, TS2, TT2, TR3, TS3, TT3 = 0;
long inicioA = 0;
long actualA = 0;
int IC = 0;
int IA = 0;
const int botonPinA = 22;
const int botonPinC = 23;
const int botonPinAC = 24;
const int faseR = 5;
const int faseS = 6;
const int faseT = 3;
const int ordenPinC = 4;
const int ordenPinA = 2;
const int PC_NCpin = 12;
const int PAT_in = 7;
const int sirena = 41;
const int luzC = 30;
const int luzA = 31;
const int luzI = 32;
boolean flag = true;
long previousMillis = 0;
long On = 50;             // tiempo que dura encendido, en milisegundos
long Off = 100;          // tiempo que dura apagado, en milisegundos
const int sensorPinC = 15;
const int sensorPinA = 16;
char seleccion;
float tiempoR = 0;
float tiempoS = 0;
float tiempoT = 0;
float reloj = 0;
int startA = 0;
int startC = 0;
int start = 0;
int r = 0;
int s = 0;
int t = 0;
int rc = 0;
int sc = 0;
int tc = 0;
int reset = 0;
int resetT = 0;
int resetTT = 0;
int t_muerto = 0;
int Aluz = 0;
int Cluz = 0;
float medicion = 0;
int tiempoA = 0;
int tiempoC = 0;
char cadena[30]; //Creamos un array que almacenará los caracteres que escribiremos en la consola del PC. Le asignamos  un tope de caracteres, en este caso 30
byte posicion = 0; //Variable para cambiar la posición de los caracteres del array
int valor;
char cadena1[30];
byte posicion1 = 0;
int valor1;


void setup() {
  lcd.begin(20, 4);
  Serial.begin(9600);
  BT.begin(38400);
  pinMode(ordenPinA, OUTPUT);
  pinMode(ordenPinC, OUTPUT);
  pinMode(sensorPinA, INPUT);
  pinMode(sensorPinC, INPUT);
  pinMode(faseR, INPUT);
  pinMode(faseT, INPUT);
  pinMode(faseS, INPUT);
  pinMode(botonPinA, INPUT);
  pinMode(botonPinC, INPUT);
  pinMode(botonPinAC, INPUT);
  pinMode(PC_NCpin, INPUT);
  pinMode(sirena, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(PAT_in, INPUT);
  pinMode(sensorPinC, INPUT);
  pinMode(sensorPinA, INPUT);
  pinMode(luzC, OUTPUT); 
  pinMode(luzA, OUTPUT);
  digitalWrite(9, HIGH);
  digitalWrite(luzC, LOW);
  digitalWrite(luzA, LOW);
  lcd.setCursor(0, 0);
  lcd.print("********************");
  lcd.setCursor(0, 1);
  lcd.print("* INICIANDO CULIAU    *");
  lcd.setCursor(0, 2);
  lcd.print("*                  *");
  lcd.setCursor(0, 3);
  lcd.print("********************");
  delay(1500);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("********************");
  lcd.setCursor(0, 1);
  lcd.print("*      CRONOS      *");
  lcd.setCursor(0, 2);
  lcd.print("*      LISTA       *");
  lcd.setCursor(0, 3);
  lcd.print("********************");
  delay(1000);
  BT.println("LISTA");
}

cronos.txt (9.05 KB)

void loop() {
 seleccion = BT.read();



 if ((digitalRead(faseR) == HIGH) && (digitalRead(faseS) == HIGH) && (digitalRead(faseT) == HIGH)) {
Aluz++;
if (millis() < (previousMillis + On + Off)){
  flag = true; 
}
else {
 flag = false; 
 previousMillis = millis();
}
if (flag == true){
 if (millis() > (previousMillis + On)){
 digitalWrite(luzA, HIGH);
 }
 else{
   digitalWrite(luzA, LOW); 
 }
}
 
 else { 
lcd.clear();
lcd.setCursor(7, 1);
   lcd.print("PRESIONE");
   lcd.setCursor(6, 2);
   lcd.print("APERTURA");
   BT.println("PRESIONE APERTURA");
}
 }



if ((digitalRead(faseR) == LOW) && (digitalRead(faseS) == LOW) && (digitalRead(faseT) == LOW)) {
   Cluz++;
 if (millis() < (previousMillis + On + Off)){
  flag = true; 
}
else {
 flag = false; 
 previousMillis = millis();
}
if (flag == true){
 if (millis() > (previousMillis + On)){
 digitalWrite(luzC, HIGH);
 }
 else{
   digitalWrite(luzC, LOW); 
 }
}
else {lcd.clear();
lcd.setCursor(7, 1);
   lcd.print("PRESIONE");
   lcd.setCursor(6, 2);
   lcd.print("CIERRE");
   BT.println("PRESIONE CIERRE"); 
}
}
 
  
if ((Aluz = 1) && ((digitalRead(faseR) == HIGH) && (digitalRead(faseS) == HIGH) && (digitalRead(faseT) == HIGH))) {    

if ((seleccion == '1') || (digitalRead(botonPinA)) == HIGH) {
     Aluz = 0;
     digitalWrite(luzA, LOW);
     digitalWrite(sirena, HIGH);
     delay(200);
     digitalWrite(sirena, LOW);
     digitalWrite(ordenPinA, HIGH);
     delay(200);
     digitalWrite(ordenPinA, LOW);
     inicioA = micros();
     lcd.clear();
     lcd.setCursor(7, 1);
     lcd.print("ORDEN");
     lcd.setCursor(6, 2);
     lcd.print("APERTURA");
     BT.println("ORDEN DE APERTURA");
     startA++;
     
   }
}  


 if (startA == 1){
   if ((digitalRead(faseR) == LOW) && (r == 0)) {
     actualA = micros();
     tiempoR = (actualA - inicioA - 12);
     r++;
   }
   if ((digitalRead(faseS) == LOW) && (s == 0)) {
     actualA = micros();
     tiempoS = (actualA - inicioA - 25);
     s++;
   }
   if ((digitalRead(faseT) == LOW) && (t == 0)) {
     actualA = micros();
     tiempoT = (actualA - inicioA - 38);
     t++;
   }
 }
 if ((r == 1) && (s == 1) && (t == 1) && (resetT == 0)) {
   BT.println("tiempos de Apertura");
   BT.print("Fase R: ");
   BT.println(tiempoR / 1000);
   BT.print("Fase S: ");
   BT.println(tiempoS / 1000);
   BT.print("Fase T: ");
   BT.println(tiempoT / 1000);
   BT.print("Discordacia R/S: ");
   BT.println((tiempoR / 1000) - (tiempoS / 1000));
   BT.print("Discordacia S/T: ");
   BT.println((tiempoS / 1000) - (tiempoT / 1000));
   BT.print("Discordacia R/T: ");
   BT.println((tiempoR / 1000) - (tiempoT / 1000));
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("TIEMPOS DE APERTURA");
   lcd.setCursor(0, 1);
   lcd.print("    R     S     T");
   lcd.setCursor(0, 2);
   lcd.print(tiempoR / 1000);
   lcd.setCursor(7, 2);
   lcd.print(tiempoS / 1000);
   lcd.setCursor(14, 2);
   lcd.print(tiempoT / 1000);
   delay(5000);
    resetT++;
   lcd.clear();
   lcd.setCursor(7, 1);
   lcd.print("SELECCIONE");
   lcd.setCursor(6, 2);
   lcd.print("CIERRE");
   BT.println("SELECCIONE CIERRE");
   tiempoR = 0;
   tiempoS = 0;
   tiempoT = 0;
   actualA = 0;
   inicioA = 0;
 
 

 }
 seleccion = BT.read();


if ((Cluz = 1) && ((digitalRead(faseR) == LOW) && (digitalRead(faseS) == LOW) && (digitalRead(faseT) == LOW))) {
   if  ((seleccion == '3') || (digitalRead(botonPinC) == LOW)) {
     Cluz == 0;
     digitalWrite(luzC, LOW);
     digitalWrite(sirena, HIGH);
     delay(200);
     digitalWrite(sirena, LOW);
     digitalWrite(ordenPinC, HIGH);
     delay(200);
     digitalWrite(ordenPinC, LOW);
     startC++;
     lcd.clear();
     lcd.setCursor(7, 1);
     lcd.print("ORDEN");
     lcd.setCursor(6, 2);
     lcd.print("CIERRE");
     BT.println("ORDEN DE CIERRE");
     inicioA = micros();
    
   }
}
 if (startC == 1) {
   if ((digitalRead(faseR) == HIGH) && (rc == 0)) {
     actualA = micros();
     tiempoR = (actualA - inicioA - 12);
     rc++;
   }
   if ((digitalRead(faseS) == HIGH) && (sc == 0)) {
     actualA = micros();
     tiempoS = (actualA - inicioA - 25);
     sc++;
   }
   if ((digitalRead(faseT) == HIGH) && (tc == 0)) {
     actualA = micros();
     tiempoT = (actualA - inicioA - 38);
     tc++;
   }
 }
 if ((rc == 1) && (sc == 1) && (tc == 1) && (resetTT == 0)) {
   BT.println("tiempos de cierre");
   BT.print("Fase R: ");
   BT.println(tiempoR / 1000);
   BT.print("Fase S: ");
   BT.println(tiempoS / 1000);
   BT.print("Fase T: ");
   BT.println(tiempoT / 1000);
   BT.print("Discordacia R/S: ");
   BT.println((tiempoR / 1000) - (tiempoS / 1000));
   BT.print("Discordacia S/T: ");
   BT.println((tiempoS / 1000) - (tiempoT / 1000));
   BT.print("Discordacia R/T: ");
   BT.println((tiempoR / 1000) - (tiempoT / 1000));
   lcd.clear();
   lcd.setCursor(1, 0);
   lcd.print("TIEMPOS DE CIERRE");
   lcd.setCursor(0, 1);
   lcd.print("    R     S     T");
   lcd.setCursor(0, 2);
   lcd.print(tiempoR / 1000);
   lcd.setCursor(7, 2);
   lcd.print(tiempoS / 1000);
   lcd.setCursor(14, 2);
   lcd.print(tiempoT / 1000);
   delay(5000);
   resetTT++;
   tiempoR = 0;
   tiempoS = 0;
   tiempoT = 0;
   actualA = 0;
   inicioA = 0;

 }

 seleccion = BT.read();
 if ((seleccion == '4') || (digitalRead(botonPinAC) == HIGH)) {
   resetT = 0;
   resetTT = 0;
   r = 0;
   s = 0;
   t = 0;
   rc = 0;
   sc = 0;
   tc = 0;
   seleccion = BT.read();
   lcd.clear();
   lcd.setCursor(7, 1);
   lcd.print("RESET");
   delay (1000);
   lcd.clear();
   BT.println("RESET");
   t_muerto = 0;
   start = 0;
   tiempoA = 0;
   tiempoC = 0;
   lcd.setCursor(0, 0);
   lcd.print("********************");
   lcd.setCursor(0, 1);
   lcd.print("*      CRONOS      *");
   lcd.setCursor(0, 2);
   lcd.print("*      LISTA       *");
   lcd.setCursor(0, 3);
   lcd.print("********************");
   delay(1000);
   BT.println("LISTA");
 }
}

Ahora bien, lo que consultas creo porque no he podido probarlo que se debe al uso de la palabra start
Si ves, start esta en color naranja, que es el color que reconoce IDE para ciertas palabras reservadas.

Hay varios errores a corregir como definir como long a las variables que luego usas con millis() o micros(). Eso no esta bien.
Tampoco esta bien que uses millis() y luego un delay() y abuses de lcd.clear.
Ahora estas cosas nada tienen que ver con la falla.
Por ahora solo veo la palabra start como responsable.
El código no ocupa gran tamaño ni consume memoria excesiva.

Estas secuencias

seleccion = BT.read();

estan mal planteadas sin el debigo if (BT.available>0) {

Todo el código a mi gusto esta mal planteado.
Debe tener un orden para presentación (LCD) para consulta del BT, para lectura de sensores o variables analogicas (Tensiones, fases, corrientes o lo que hagas).