Arduino Stops after some time no String in the code

Hi guys,
I am using and Arudino Nano a With chc430 chip I strongly guess.
It's for an aquaponic system,
The system is suppose to manage pumps, get temperature, humidity, (and later PH, and water temperature, etc).
No String in my code btw.

Every second I increment a counter,
every counter == 0, I open the pump,
every counter == 30, I close the pump,
every counter == 300; I reset the counter and add 1 cycle!
Sometime when the counter == 267 the system stop, it doesn't seems to be connected to cycles since the value is different every time witch is weird :confused: :confused:

Can someone help to debug please?

Here is the code:

#include <Wire.h>


// oustside temp
#include "DHT.h"
#define DHTPIN 4 
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//screen
#include "rgb_lcd.h"
rgb_lcd lcd;



// master code
const int             pinPump=6;
bool                  pumpIsOpen=false;
unsigned int          counter=0;
unsigned int          cycleNumber=0;

void loopScreen(unsigned int timeLeft, unsigned int cycleNumber, float humidity, float temperature){
      char            discplayString[16];
      char            discplayString2[16];
      lcd.clear();

      lcd.setCursor(0, 0);
      sprintf(discplayString, "H: %d ,T: %d*C", (int)humidity, (int)temperature);

      lcd.print(discplayString);
      lcd.setCursor(0, 1);
      sprintf(discplayString2, "T:%d s,C: %d c", timeLeft, cycleNumber);
      lcd.print(discplayString2);
}

void check()
{
  const unsigned int    timeLeftOpen=32;
  const unsigned int    cycleTime=300;
    if(counter == 0)
    {
      if(!pumpIsOpen){
        digitalWrite(pinPump, LOW); 
        Serial.println("pump open");
        pumpIsOpen = true;
      }
    }
     
    if(counter >timeLeftOpen)
    {
      if(pumpIsOpen){
        digitalWrite(pinPump, HIGH);
        Serial.println("pump stoped");
        pumpIsOpen = false;
        }
    }
      float temp_hum_val[2] = {0};
      if (!dht.readTempAndHumidity(temp_hum_val)) 
      {
        // Humidity
        Serial.println(temp_hum_val[0]);
        // Temperature
        Serial.println(temp_hum_val[1]);
      } else {
        Serial.println("Failed to get temprature and humidity value.");
      }
    
    counter ++;
    if(counter>cycleTime)
    {
     cycleNumber ++; 
     counter=0;
     Serial.println("cycle finished");
    }

    unsigned int timeLeft = cycleTime - counter;
    loopScreen(timeLeft, cycleNumber, temp_hum_val[0], temp_hum_val[1]);
    Serial.print("ctn: ");
    Serial.print(counter);
    Serial.print(",pump: ");
    Serial.print(pumpIsOpen);
    Serial.print(",humi: ");
    Serial.print(temp_hum_val[0]);
    Serial.print(",temp: ");
    Serial.print(temp_hum_val[1]);
    Serial.print(",cyclnb: ");
    Serial.print(cycleNumber);
    Serial.print("\n");
    delay(1000);
}

void setupPump(){
  pinMode(pinPump, OUTPUT); 
}

void setupScreen(){
    const int colorR = 255;
    const int colorG = 0;
    const int colorB = 0;

    lcd.begin(16, 2);
    lcd.setRGB(colorR, colorG, colorB);

    // Print a message to the LCD.
    
}
void setupTemp(){
      dht.begin();
  }
void setup() {
  Wire.begin();
  Serial.begin(115200);
  setupPump();
  setupTemp();
  setupScreen();
}

void loop()
{
  check();
}

Thanks guys for your help

I'd be suspicious of the pump - how is it wired up?

as a quick guess: you have defined several variables inside of functions which means that these variables are local to the function and get destroyed everytime the function is left.

So you should add the control-word "static" to those variables which makes the value survive the leaving and re-entering the function.

or you define the variables all global outside of al functions right on top of the file

For narrowing down where the program stops to work add serial-debug-output in a manner that each serial output has its own number so can clearly distinguish from which line of code the serial output comes from

best regards Stefan

Sometimes bad wiring(the pump in your case) from external world can mess up the arduino. Please post your schematic.
Use serial print of variables at every entry and exit of functions to figure out if the hang is consistent, in this way you can find out a code bug.
Check your power supply to the board whether it is rigid and strong in terms of connection because dupont cables are not good quality nowadays, and also check if the voltage is clean from the supply. Some chinese clones of the arduino do not have the decoupling capacitor at the vcc pin of the microcontroller which causes instability.

i tried emulating your code and running with a delay of 100 instead of 1000 and saw it run continuously for several minutes on an Uno.

by emulate, i mean i created a stub classes (struct) for DHT and rgb_lcd allowing me to execute the code logic w/o modification.

i'd suggest you try doing something similar: running with a shorter delay, toggling an LED instead of controlling a pump, stubbing out libraries in order to isolate the problem.

you could also try putting numerous prints in your code to help isolate the section where it appears to stop.

"With chc430 chip I strongly guess"

No need to guess, just look at the bottom of the board and read what is on the chip that is near the USB connector.
Atmrga16U2? CH430? Something else?

wildbill:
I'd be suspicious of the pump - how is it wired up?

I am using a relai with 4 channel like this => http://arduinolearning.com/wp-content/uploads/2017/01/4-channel-relay-image.jpg.

StefanL38:
as a quick guess: you have defined several variables inside of functions which means that these variables are local to the function and get destroyed everytime the function is left.

So you should add the control-word "static" to those variables which makes the value survive the leaving and re-entering the function.

or you define the variables all global outside of al functions right on top of the file

For narrowing down where the program stops to work add serial-debug-output in a manner that each serial output has its own number so can clearly distinguish from which line of code the serial output comes from

best regards Stefan

I read somewhere that I should avoid using global variable, I tried this, but this is not the issue I am going to put them back in global.
I am going to use rapsi to record every serial output! for debugging.

rvxfahim:
Sometimes bad wiring(the pump in your case) from external world can mess up the arduino. Please post your schematic.
Use serial print of variables at every entry and exit of functions to figure out if the hang is consistent, in this way you can find out a code bug.
Check your power supply to the board whether it is rigid and strong in terms of connection because dupont cables are not good quality nowadays, and also check if the voltage is clean from the supply. Some chinese clones of the arduino do not have the decoupling capacitor at the vcc pin of the microcontroller which causes instability.

What do you mean by bad wiring?
The arduino is connected to a Raspi for supply with is connected to a random cable and random supply I am sure this is the issue! I will record eveything an the raspi! no choice!


gcjr:
i tried emulating your code and running with a delay of 100 instead of 1000 and saw it run continuously for several minutes on an Uno.

by emulate, i mean i created a stub classes (struct) for DHT and rgb_lcd allowing me to execute the code logic w/o modification.

i'd suggest you try doing something similar: running with a shorter delay, toggling an LED instead of controlling a pump, stubbing out libraries in order to isolate the problem.

you could also try putting numerous prints in your code to help isolate the section where it appears to stop.

thanks for trying. I am going to get another arduino for my test since this one feed my plants and clean the water of my fishes,
And yes print will help me to find out for sure!
:heart: :heart: :heart: Thanks you all for your quick reply! :heart: :heart: :heart:
here is a part of the project

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.