Arduino board "crashes" executing a subroutine

Hi, I need some help with the next problem: I’m doing a project to control a water pump, but each time that I execute a subroutine to set a parameter, the board “crashes”, sometimes just restarts, and sometimes it go nuts, enabling and disabling random outputs. I’ve created a new file with just the subroutine to test it, and now the board doen’t crashes, but keeps randomly enabling and disabling the pump output pin even without a digitalWrite function on the code.

I’ve attached the file of the full code because it’s too long to post it here, but here is the subroutine code:

#include <LiquidCrystal.h>
#include <EEPROM.h>
float distancia;
float tiempo;
int trigger=4; //Trigger pin
int eco=5; //Echo pin

int nivMin;
int nivMax;
float area;
float porcentaje;

int bomba = A5; //output attached to the pump relay
int outBomba;
int setPoint; //% to start the pump
int sensorLleno = 3;
int sensorBomba = 2;
int x=0;
int tiempoProteccion=5; //Protection time in seconds

int izquierda=A4;
int derecha=A3;
int menu=A2;
int salir=A1;
int menus=1;
int counter=0;
int proteccion; //enables or disables the protection (0 or 1)

LiquidCrystal lcd(13,12,10,9,8,7); //( RS, EN, d4, d5, d6, d7)

void setup() {

 pinMode (6,OUTPUT);  //Screen light
 analogWrite (6,255); //PWM brightness control
 lcd.begin(16, 2); //LCD 16x2
// lcd.setCursor(3, 0); 
//lcd.print("Bienvenido");
//delay(2000); 
  

 pinMode(4,OUTPUT); //trigger
 pinMode(5,INPUT); //echo
 pinMode(bomba,OUTPUT); //pump
 pinMode(sensorLleno,INPUT);
 pinMode(sensorBomba,INPUT);

 pinMode(izquierda,OUTPUT);
 pinMode(derecha,OUTPUT);
 pinMode(menu,OUTPUT);
 pinMode(salir,OUTPUT);
 
 Serial.begin(9600);

//EEPROM.put(4,1); Not using it now

 EEPROM.get(0,nivMin);
 EEPROM.get(2,nivMax);
 EEPROM.get(4,proteccion);
 EEPROM.get(6,setPoint);
 EEPROM.get(8,tiempoProteccion);

 Serial.println(nivMin);
 Serial.println(nivMax);
 Serial.println(proteccion);
 Serial.println(setPoint);
 Serial.println(tiempoProteccion);
 
 lcd.clear();
}

void loop() {
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Ajuste el tiempo");

 if (tiempoProteccion>0){
  lcd.setCursor(0, 1);
  lcd.print("<<");
  }
  
 if (tiempoProteccion<=200){
  lcd.setCursor(14, 1);
  lcd.print(">>");
  }
  
 if (tiempoProteccion==0){
  lcd.setCursor(5, 1);
  lcd.print("Apagada");
}
 else{
  lcd.setCursor(2, 1);
  lcd.print(tiempoProteccion);
  lcd.setCursor(6, 1);
  lcd.print("Segundos");
 }

if (digitalRead(derecha)==HIGH){
  while (digitalRead(derecha)==HIGH){
    delay(100);
  }
  if (tiempoProteccion<250){
  tiempoProteccion++;
}}

else if (digitalRead(izquierda)==HIGH){
  while (digitalRead(izquierda)==HIGH){
    delay(100);
  }
  if (tiempoProteccion>0){
  tiempoProteccion--;
}}
if (digitalRead(salir)==LOW){
  delay(100);
  loop();
}

while (digitalRead(salir)==HIGH){
 delay(100);
}
// calibracion(); returns to the selection menu subroutine
}

This code has all the variables from the main code, and sorry for not translating the variables to english, but I don’t want to ruin something (also I’m pretty tired…). Thanks for your attention. :wink:

Proyecto_bomba.ino (6.92 KB)

Hi, I think you have Electromotive Interference problems, not code...

See this suggestions:

http://arduino-info.wikispaces.com/Arduino-Project-Planning-Electrical

terryking228:
Hi, I think you have Electromotive Interference problems, not code...

See this suggestions:

http://arduino-info.wikispaces.com/Arduino-Project-Planning-Electrical

The problem is that I still haven't connected any relay, and the problem only happens with that subroutine specifically. Now I'm pretty confused.

Ok, for some reason this piece of code is doing the problem, but I don't know why...

if (digitalRead(salir)==HIGH){
while (digitalRead(salir)==HIGH){
 delay(100);
}
calibracion();
}

One thing might be recursion. Under certain conditions calibracion() calls calibration() again.
Another one might be a divide by zero error; in math(), the area variable can theoretically become 0 and dividing by zero is never a good idea :wink:

sterretje:
One thing might be recursion. Under certain conditions calibracion() calls calibration() again.
Another one might be a divide by zero error; in math(), the area variable can theoretically become 0 and dividing by zero is never a good idea :wink:

I made a code to avoid "area" becoming 0 or a negative number. Anyway I've fixed it doing that part of the code again, so now it works but I don't know what might have happened. Thanks anyway.

if (digitalRead(salir)==LOW){
  delay(100);
  loop();
}

How many times can loop() call itself before top-of-stack runs into bottom of data? :slight_smile: