Arduino crashes (goes back to setup) after few loops

Hey guys,
i am building hydroponics nutrient dosing system with arduino, peristaltic pumps and DFRobot probes. My goal is to take measurement of pH and EC and according to values turn on pumps.

Since I discovered, that two probes when in same solution are disturbing each other, I need to first turn on pH probe, take measurement, then turn it off and repeat for EC probe. I am doing this by using relays (arduino relay module).

Also since the first measurement is not accurate, I am using two arrays to take 10 sample values, then sort it from small to large, then take middle 6 values and calculate average which is the final value that should decide if it is needed to dose nutrients or acid.

Now I think this is the problem. After testing out, arduino will repeat the loop for 4-6 times and then it crashes. Either it will stop communicating with serial monitor or it will go to setup again (print("Ready")), or it will start talking "machine language" at serial monitor :smiley: ...

I have tried to clear each array after measurement is done, but it has no effect (if I am doing it right). Does anyone know what could be the problem?

NOTE: The code is not complete - does not have function for controling pumps yet, but i need to sort this out first. Also English is my second language, so I am sorry if its a bit rusty. Also I am new to whole Arduino thing, so I am sorry if the code is nonsense :smiley:

#include "DFRobot_EC.h"
#define SensorPin 1          // the pH meter Analog output is connected with the Arduino’s Analog
#define EC_PIN A2
float voltage,ecValue,phValue,temperature = 25;
DFRobot_EC ec;
#define Offset 0.07
unsigned long int avgValue;
unsigned long int avgValueEC;   //Store the average value of the sensor feedback
float b;
int buf[10],temp;
int bufec[10],tempec;
int RelayControl1 = 4;    // Digital Arduino Pin used to control the relay
int RelayControl2 = 5;  
int RelayControl3 = 6;  
int RelayControl4 = 7;

void setup()
{ 
  Serial.begin(115200);  
  Serial.println("Ready");    //Test the serial monitor
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
  digitalWrite(RelayControl1,HIGH);
  digitalWrite(RelayControl2,HIGH);
  digitalWrite(RelayControl3,HIGH);
  digitalWrite(RelayControl4,HIGH);
  ec.begin();
}

void loop()
{
  digitalWrite(RelayControl4,LOW);
  delay(1000);
  get_ec();
  digitalWrite(RelayControl4,HIGH);
  delay(1000);
  digitalWrite(RelayControl3,LOW);
  delay(1000);
  get_ph();
  digitalWrite(RelayControl3,HIGH);
  delay(1000);
  
}
void get_ph()
{
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(2000);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue + Offset;                      //convert the millivolt into pH value
  Serial.print("    pH:");  
  Serial.print(phValue,2);
  Serial.println(" ");
  memset(buf, 0, sizeof(buf));
  memset(temp, 0, sizeof(temp));   
  delay(800);

}
void get_ec()
{
  for(int e=0;e<10;e++)       //Get 10 sample value from the sensor for smooth the value
  { 
    bufec[e]=analogRead(EC_PIN)/1024.0*5000;
    delay(1000);
  }
  for(int e=0;e<9;e++)        //sort the analog from small to large
  {
    for(int c=e+1;c<10;c++)
    {
      if(bufec[e]>bufec[c])
      {
        tempec=bufec[e];
        bufec[e]=bufec[c];
        bufec[c]=tempec;
      }
    }
  }
  avgValueEC=0;
  for(int e=2;e<8;e++)                      //take the average value of 6 center sample
    avgValueEC+=bufec[e];
  float ecVolt=(float)avgValueEC/6;
  ecValue =  ec.readEC(ecVolt,temperature);
  Serial.print(ecValue,2);
  memset(bufec, 0, sizeof(bufec));
  memset(tempec, 0, sizeof(tempec));  
  delay(800);
 }   

Post a few pics of your wiring and a schematic of your circuit.

It reads like a power issue or an array going out of bounds or running out of memory.

In loop() could you put a serial.println("tick"); to see if loop has actually stopped running?

Better yet

void loop()
{
Serial.println( "tick a" );
  digitalWrite(RelayControl4,LOW);
  delay(1000);
Serial.println( "tick b" );
  get_ec();
Serial.println( "tick c" );
  digitalWrite(RelayControl4,HIGH);
Serial.println( "tick d" );
  delay(1000);
  digitalWrite(RelayControl3,LOW);
Serial.println( "tick e" );
  delay(1000);
Serial.println( "tick f" );
  get_ph();
Serial.println( "tick g" );
  digitalWrite(RelayControl3,HIGH);
  delay(1000);
Serial.println( "tick aaaaaaaHHHH got to the end" );
  
}

Start hunting down the section that is causing the issue.

Agree with @Idahowalker . Try adding a Serial.print and return to the beginning of each of your functions other than loop, like:

void get_ph()
{
  Serial.println( "get_ph" ); // <-- add
  return; // <-- add

  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(2000);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
.
.
.

This removes the arrays and math from execution. If the problem persists the switching of the relays or disturbance on the power supply is the problem.

So I have tried to upload edited code... First it got to the end of the loop, but then it vent wrong again. No respond to serial monitor (again after 4-6 loops), prints "Ready" - setup and then the relays start to turn on and off randomly (much shorter delay than entered). So I guess the power is the problem?

Here are pictures of serial monitor:

Here is the short video: video 1639062038 - YouTube

I can draw circuit diagram if it is needed.
Thank you!

On executing get_gc the program stops working, correct?

Well what do you think you can do next?
If you guessed Serial.prints! you might be a giant.

void get_ec()
{
Serial.println( "a");
  for(int e=0;e<10;e++)       //Get 10 sample value from the sensor for smooth the value
  { 
    bufec[e]=analogRead(EC_PIN)/1024.0*5000;
    delay(1000);
  }
Serial.println( b");
  for(int e=0;e<9;e++)        //sort the analog from small to large
  {
    for(int c=e+1;c<10;c++)
    {
      if(bufec[e]>bufec[c])
      {
        tempec=bufec[e];
        bufec[e]=bufec[c];
        bufec[c]=tempec;
      }
    }
  }
Serial.println( "c");
  avgValueEC=0;
Serial.println( "d");
  for(int e=2;e<8;e++)                      //take the average value of 6 center sample
    avgValueEC+=bufec[e];
  float ecVolt=(float)avgValueEC/6;
  ecValue =  ec.readEC(ecVolt,temperature);
  Serial.print(ecValue,2);
  memset(bufec, 0, sizeof(bufec));
  memset(tempec, 0, sizeof(tempec));  
  delay(800);
 }   

You can figure out how to add serial prints to the rest of the code to hunt down the area of concern, right?

Yes, I will try it, but i have an update.
Since you guys were talking about possible power problem, I realized that whole circuit was running from usb alone (expect pumps, they have dedicated power source). So I connected 12V 1A supply and it seems it helped, the loop is now running without doing crazy stuff, but i still get "Ready" (setup) every once in the while.

Also you can see that pH values are jumping a bit, I dont know if it has something to do with it, but probe was calibrated yesterday.

ready printouts would be where your project might be resetting.

Try those serial prints.

Which Arduino are you using, and what does your voltmeter say?

Oi! Yours talks! Cool.

Do you have a diode connected across each relay coil?

See flyback-diodes-and-why-you-need-them for more information.