Arduino Uno with water level control and flow meter, program resets itself

I am a beginner with Arduino. I do have a good understanding of how it works. As well I 'm not much of a code writer but I can read and understand a good portion of it.
My project is for a vertical hydroponics garden. My design does not allow for a large reservoir so I need to activly control water level. As well I need to be able to drip fertilizer when the pump is running so I need to incorporate a flow meter. I'm using an ultra sonic sensor for water level control and some modified code I found online. The flow meter is a Hall effect sensor.
My issue is that when the pump is activated by way of a relay the flow meter will measure the amount of water and I will be able to adjust the drip. I can verify the code and upload it to the Arduino Uno. However, when the flow meter is activated it reset the program. I have been able to run both programs separately but when I try to combine them I have my issue. I don't really know how to combine the two programs to have them work independently. So I think this is my issue. Any help or direction to solve my problem would be greatly appreciated .

// include the library code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <NewPing.h> // Include  library used for measuring the distance using HC-SR 06 sonar sensor
#define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN   7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
#define MOTORPIN 8 // Arduino pin tied to Relaypin of the Relay Circuit

byte statusLed    = 13;

int sensorInterrupt = 0;  // 0 = digital pin 2
int sensorPin       = 2;

float calibrationFactor = 7.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
float val;

#include <EEPROM.h>
int addr = 0;
int addr2 = 1;
int flag;
byte readval;

#define buttonPin 10
int buttonState = 0;      // variable for reading the pushbutton status
float TankHeight, WaterLevelMAX, GapbwSonar, SonarReading, ActualReading,  Temp;
int percentage;

void setup() {

 Serial.begin(9600);
 lcd.init();                      // initialize the lcd 
 // Print a message to the LCD.
 lcd.backlight();
 lcd.begin(20, 4); // set up the LCD's number of columns and rows:

 pinMode(MOTORPIN,OUTPUT);// Relay pin as output pin
// pinMode(EXTRELAYPIN,OUTPUT);// External Relay pin as output pin
 digitalWrite(MOTORPIN,HIGH); //Turn off the relay

 lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Starting ARDUINO, ");
  lcd.setCursor(0,1);
  lcd.print("Press SCAN Button to");
  lcd.setCursor(0,2);
  lcd.print("Change Tank Height  ");
  lcd.setCursor(0,3);
  lcd.print("If Required  ");
  delay(2000);
 
// delay(1000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Confirm Tank Must be");
 lcd.setCursor(0,1);
 lcd.print("Empty to Set Tank   ");
 lcd.setCursor(0,2);
 lcd.print("Height using Sonar  ");
 delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Now Scan to Change  ");
lcd.setCursor(0,1);
lcd.print("Tank Height to New  ");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Long Press Button...");
 for (int i=0; i<=5; i++)
{
 lcd.setCursor(0,1);
 lcd.print("  Hold :  ");
 lcd.print(5-i);
 lcd.print(" Seconds ");
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH)
 {
 TankHeight =sonar.ping_cm();
 EEPROM.write(addr, TankHeight);
 }
delay(1000);
}

 {TankHeight= EEPROM.read(addr);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Tank New Height Set@");
 lcd.setCursor(0,1);
 lcd.print(" ");
 lcd.print(TankHeight);
 lcd.print("cm ");

 delay(2000);
 lcd.clear();
 lcd.setCursor(0,2);
 lcd.print("TANK HEIGHT: ");
 //lcd.print("cm ");
 lcd.setCursor(12,2);
 lcd.print(TankHeight);
 lcd.print("cm ");
 
 WaterLevelMAX=0.9*TankHeight;  // 10CM GAP BETWEEN MAX WATER LEVEL TO SONAR FIX
// WaterLevelMAX= 5-TankHeight;
 GapbwSonar=TankHeight-WaterLevelMAX;}

{

// Set up the status LED line as an output
 pinMode(statusLed, OUTPUT);
 digitalWrite(statusLed, HIGH); 
 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);

 pulseCount        = 0;
 flowRate          = 0.0;
 flowMilliLitres   = 0;
 totalMilliLitres  = 0;
 oldTime           = 0;

 // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
 // Configured to trigger on a FALLING state change (transition from HIGH
 // state to LOW state)
}
}
void loop() {

 //  main code to run repeatedly:

 delay(1000); // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 SonarReading=sonar.ping_cm();

 //Serial.println(DRYsensorValue);

 Temp= SonarReading-GapbwSonar;
 ActualReading= WaterLevelMAX-Temp;
 percentage=(ActualReading/WaterLevelMAX*100);
 Serial.println(percentage);
 //Serial.println(TankHeight);
 lcd.setCursor(0,0);
 lcd.print("WATER LEVEL:");
 lcd.print(percentage);
 lcd.print("%  ");
 {

 if(percentage<=40)
 {
  
 lcd.setCursor(0,1);
 lcd.print("PUMP STATUS: RUNNING");
 digitalWrite(MOTORPIN,LOW);
flag=1;
 EEPROM.write(addr2, flag);
 flag= EEPROM.read(addr2);

//  ZeroPercentage();
 }

{
  ((millis() - oldTime) > 100);    // Only process counters once per second
 { 
    detachInterrupt(sensorInterrupt);
       
   // Because this loop may not complete in exactly 1 second intervals we calculate
   // the number of milliseconds that have passed since the last execution and use
   // that to scale the output. We also apply the calibrationFactor to scale the output
   // based on the number of pulses per second per units of measure (litres/minute in
   // this case) coming from the sensor.
   flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
   
   // Note the time this processing pass was executed. Note that because we've
   // disabled interrupts the millis() function won't actually be incrementing right
   // at this point, but it will still return the value it was set to just before
   // interrupts went away.
   oldTime = millis();
   
   // Divide the flow rate in litres/minute by 60 to determine how many litres have
   // passed through the sensor in this 1 second interval, then multiply by 1000 to
   // convert to millilitres.
   flowMilliLitres = (flowRate / 60) * 1000;
   
   // Add the millilitres passed in this second to the cumulative total
   totalMilliLitres += flowMilliLitres;
     
   unsigned int frac;
   
    // Reset the pulse counter so we can start incrementing again
   pulseCount = 0;
   
   // Enable the interrupt again now that we've finished sending output
   attachInterrupt(sensorInterrupt, pulseCount, FALLING);
 }
 if(percentage>60 && percentage<=100)
 {
 delay(15000);

Temp= SonarReading-GapbwSonar;
 ActualReading= WaterLevelMAX-Temp;
 percentage=(ActualReading/WaterLevelMAX*100);
 
 lcd.setCursor(0,1);
 lcd.print("PUMP STATUS: OFF    ");
 lcd.setCursor(0,0);
 lcd.print("Water Level:");
                                  // This line is wrong . Should print actual fix it
 lcd.print("%  ");
  digitalWrite(MOTORPIN,HIGH);
// digitalWrite(EXTRELAYPIN,HIGH);

 flag=0;

 EEPROM.write(addr2, flag);
 flag= EEPROM.read(addr2);

//  HundredPercentage();
 }
 }

{

 flag= EEPROM.read(addr2);
 if(flag==1)
 { 
 //  lcd.clear();
 lcd.setCursor(0,3);
 lcd.print(" Flow Rate");
 lcd.setCursor(1, 3);
 lcd.print(flowRate);
 lcd.setCursor(6, 3);
 lcd.print("L/Min");

 digitalWrite(MOTORPIN, LOW);
// digitalWrite(EXTRELAYPIN, HIGH);
 delay(300);
 delay(300);
 }

 else if(flag==0)
 {
 lcd.setCursor(0,3);
 lcd.print("SUMP: NOWATER/CHECK");
 }
}
}
}

(Code tags and extra blank lines deleted by Moderator. Next post, use the </> button on the menu and paste your code in the box that opens. Be sure to use CTRL-T in the IDE before pasting it in.)

Could you post your Schematic showing all power and ground connections for all power supplies etc. This could be an electronic problem so I want to check the electronics before I consider going through the code.

I don't have a schematic, but I could easy make one. What software do you recommend for doing this?

pnorsworthy:
What software do you recommend for doing this?

Hand drawn is fine if clear and legible. If you want software assistance there's Express SCH among others. (Not a recommendation).

Express SCH is great for projects like this. Easy to make symbols for your components if you do not see a part you need.

I use windows XP Paint. To be honest i glanced at your code, but i do not have any overview of how it is actually structured. Basically all is either in setup() or in loop() (which is huge) Please format your code using ctrl-T and i think you should really put some sections in functions. It will create more overview and will be more explanatory than a few lines of comments.

I'm still working on the schematic, but in the meantime I have got my code to run and work properly. Simple mistakes of a beginner. No on to the next step of my project. I will try to finish the schematic and get it upload incase anyone has any advice for me.

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