Arduino freezes up after about 15 to 20 mins

Hi,

I am new to the world of arduino. For my first project I was working on a automatic pump controller for an overhead tank.

My circuit is similar to this other than I have used a TIP122 to control the relay. There is no probe for sump and no buzzer


The core that I have written is :

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize LCD display pins
int L1=A0;
int L2=A1;
int L3=A2;
int L4=A3;
int motor=13;
int c=100;
int q;
int h;
int t;
int f;
int ms=0;

void setup()
  {
    delay(500);            // delay 0.5 sec for LCD to initialize
    lcd.begin(16,2);
    lcd.print("Initializing...");
    lcd.setCursor(0,1);
    lcd.print("   Please Wait!!!");
    pinMode(L1, INPUT);
    pinMode(L2, INPUT);
    pinMode(L3, INPUT);
    pinMode(L4, INPUT);
    pinMode(motor, OUTPUT);
    delay(5000);            // 5 sec delay incase of power faliure
    lcd.clear();
    ms=0;
    }
  
void loop()
  {
    q=analogRead(L1);
    h=analogRead(L2);
    t=analogRead(L3);
    f=analogRead(L4);
      if(ms==1)
       {
          digitalWrite(motor, HIGH);
          lcd.setCursor(0,1);
          lcd.print("PUMP: ON ");
       }
      else
        {
          if(ms==0)
            {
              digitalWrite(motor, LOW);
              lcd.setCursor(0,1);
              lcd.print("PUMP: OFF");
            }
        }
    if(q>c && h>c && t>c && f>c)
      {
        lcd.setCursor(0,0);
        lcd.print("Water Lvl E    F");
        lcd.setCursor(11,0);
        lcd.print(char(255));
        lcd.print(char(255));
        lcd.print(char(255));
        lcd.print(char(255));
        ms=0;
      }
     else
       {
         if(q>c && h>c && t>c && f<c)
           {
             lcd.setCursor(0,0);
             lcd.print("Water Lvl E    F");
             lcd.setCursor(11,0);
             lcd.print(char(255));
             lcd.print(char(255));
             lcd.print(char(255));
             lcd.print(" ");
           }
         else
           {
             if(q>c && h>c && t<c && f<c)
               {
                 lcd.setCursor(0,0);
                 lcd.print("Water Lvl E    F");
                 lcd.setCursor(11,0);
                 lcd.print(char(255));
                 lcd.print(char(255));
                 lcd.print(" ");
                 lcd.print(" ");
               }
             else
               {
                 if(q>c && h<c && t<c && f<c)
                   {
                     lcd.setCursor(0,0);
                     lcd.print("Water Lvl E    F");
                     lcd.setCursor(11,0);
                     lcd.print(char(255));
                     lcd.print(" ");
                     lcd.print(" ");
                     lcd.print(" ");
                   }
                  else
                    {
                      if(q<c && h<c && t<c && f<c)
                         {
                           lcd.setCursor(0,0);
                           lcd.print("Water Lvl E    F");
                           lcd.setCursor(11,0);
                           lcd.print(" ");
                           lcd.print(" ");
                           lcd.print(" ");
                           lcd.print(" ");
                           ms=1;
                         }
//                       else
//                         {
//                           lcd.clear();
//                           lcd.print("  ***ERROR***  PLEASE RESET");
//                           while(1);
//                         }
                    }
               }
           }
       } 
  delay(1000);
  }

Now everything was working fine until the transistor which i was using earlier i.e. BC337 shorted out. So I decided to swap it out with TIP122. It was working fine until it started causing problems. It was switching on even with base disconnected from pin 13. Found out the flux from soldering was the main culprit.

After that incident the arduino keeps on freezing every 15 to 20 mins and then resets itself. Sometimes it even freezes up immediately after resetting.

Tried uploading the code again but it keeps doing the same thing.

Can anybody help me out with this???

The first thing I would do is to simplify your code. Give the pins and variables meaningful names to make it more readable to start with.

Do you really need several complicated tests like this ?

if (q > c && h > c && t < c && f < c)

If a particular probe is in the water then write char(255) on the LCD and test the next one up and so on. Alternatively work from the top down. If the full probe is in the water write the graphics to the LCD and stop checking, else check the next one down and so on until you find the current level, write its graphic and stop checking.

This will not help your current problem, which may be hardware related, but will make finding out much easier.

Sankalp:
After that incident the arduino keeps on freezing every 15 to 20 mins and then resets itself. Sometimes it even freezes up immediately after resetting.

If you suspect the Arduino may have been damaged upload a simple sketch to blink LEDs using the same pins as in your main sketch and see will that run for a long time.

If the Arduino is not damaged the most likely reason for a problem after a long running period is data overflow. And the crash problem might possibly have been responsible for your transistor failure.

...R

Hi, when it is working, please check with a DMM the voltages that you have on each of the analog inputs.
When it is not working, please do the same, each time noting which probes are sunmerged.

The problem can be the fact that you are using a current to detect the probes' states, a DC current that may be, through, electrolysis changing the characteristics of the probes.

Tom...... :slight_smile:

Thanks guys for your input.. I will try these things and get back to you..

Made some progress since yesterday. Found out it wasen't an issue with the code. The relay with 240V AC mains was connected on the same PCB as the other components which was causing the arduino to go haywire. After separating the relay they circuit seems to be stable but in the past 17 hours the arduino has froze once. I was thinking about including a watchdog timer in the program but i am not familiar with the code.

Can anyone of u guys help me with the code??

Here's mine:

 #include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize LCD display pins
int L1=A3;
int L2=A2;
int L3=A1;
int L4=A0;
int motor=13;
int c=150;
int q;
int h;
int t;
int f;
int ms=0;

void setup()
  {
    delay(500);            // delay 0.5 sec for LCD to initialize
    lcd.begin(16,2);
    lcd.print("Initializing...");
    lcd.setCursor(0,1);
    lcd.print("   Please Wait!!!");
    pinMode(L1, INPUT);
    pinMode(L2, INPUT);
    pinMode(L3, INPUT);
    pinMode(L4, INPUT);
    pinMode(motor, OUTPUT);
    delay(5000);            // 5 sec delay incase of power faliure
    lcd.clear();
    ms=0;
    }
  
void loop()
  {
    q=analogRead(L1);
    h=analogRead(L2);
    t=analogRead(L3);
    f=analogRead(L4);
      if(ms==1)
       {
          digitalWrite(motor, HIGH);
          lcd.setCursor(0,1);
          lcd.print("PUMP: ON ");
       }
      else
        {
          if(ms==0)
            {
              digitalWrite(motor, LOW);
              lcd.setCursor(0,1);
              lcd.print("PUMP: OFF");
            }
        }
    if(q>c && h>c && t>c && f>c)
      {
        lcd.setCursor(0,0);
        lcd.print("Water Lvl E    F");
        lcd.setCursor(11,0);
        lcd.print(char(255));
        lcd.print(char(255));
        lcd.print(char(255));
        lcd.print(char(255));
        ms=0;
      }
     else
       {
         if(q>c && h>c && t>c && f<c)
           {
             lcd.setCursor(0,0);
             lcd.print("Water Lvl E    F");
             lcd.setCursor(11,0);
             lcd.print(char(255));
             lcd.print(char(255));
             lcd.print(char(255));
             lcd.print(" ");
           }
         else
           {
             if(q>c && h>c && t<c && f<c)
               {
                 lcd.setCursor(0,0);
                 lcd.print("Water Lvl E    F");
                 lcd.setCursor(11,0);
                 lcd.print(char(255));
                 lcd.print(char(255));
                 lcd.print(" ");
                 lcd.print(" ");
                 ms=1;
               }
             else
               {
                 if(q>c && h<c && t<c && f<c)
                   {
                     lcd.setCursor(0,0);
                     lcd.print("Water Lvl E    F");
                     lcd.setCursor(11,0);
                     lcd.print(char(255));
                     lcd.print(" ");
                     lcd.print(" ");
                     lcd.print(" ");
                     ms=1;
                   }
                  else
                    {
                      if(q<c && h<c && t<c && f<c)
                         {
                           lcd.setCursor(0,0);
                           lcd.print("Water Lvl E    F");
                           lcd.setCursor(11,0);
                           lcd.print(" ");
                           lcd.print(" ");
                           lcd.print(" ");
                           lcd.print(" ");
                           ms=1;
                         }
                       else
                         {
                           lcd.clear();
                           lcd.print("Sensor Error");
                           delay(30000);
                         }
                    }
               }
           }
       } 
  delay(1000);
  }