Find the time between two sensor measurements

I'm building an irrigation system that runs autonomously. When the moisture level drops below a lower threshold, a pump turns on. When it rises back above an upper threshold, the pump turns offs. That section of the code is as follows:

if(mAvgVal[3]<=150){
SWITCH_PUMP;
digitalWrite(PUMP, HIGH);
}
if (mAvgVal[3]>=400){
digitalWrite(PUMP, LOW);
}

I need to find time that the pump is "HIGH", i.e the time it takes moisture to rise from 150 to 400. The following is what have (it doesn't even verify)

unsigned long pumptstrt = 0;
unsigned long pumpt = 0;

if(mAvgVal[3]<=150){
pumptstrt = millis();
delay(200);
}

else if (mAvgVal[3]>=400){
pumpt = millis()-pumpstrt;
delay(200);
}
pumpt = pumpt/1000;

I feel like it's an obvious answer but I haven't gotten it. Any help would be great, Thanks!

Well you should really show all your code, but the idea you have there is correct.

You should make your additions and do some serial prints to prove your thought process.

.

Is this:

SWITCH_PUMP;

... a call to a function? It needs ():

SWITCH_PUMP();

(It wouldn't prevent the code compiling though: just won't work.)

Post your whole sketch.

I didnt want to post all the code because theres more going on in addition to the part that controls the pump but here is all of it. The part controlling the pump is towards the middle and end.

/*Arduino Automated Irrigation System (AAIS)

  • Code by Istvan Romhany
  • February 2016
    */
    #include <SPI.h> //Libraries provide pre-written functions
    #include <SD.h>

#define SENSORS 3;
#define PUMP_TIME 150000;

//Pin Declaration
const int LED = 7; // Red Led at pin 7
const int LEDG = 3; // Green Led at pin 4
const int mst0 = A0; // Moisture Sensor at Analog 0, A0
const int mst1 = A1; // Moisture Sensor at Analog 1, A1
const int mst2 = A2; // Moisture Sensor at Analog 2, A2

const int PUMP = 8; // Pump SWITCH signal pin

const int sdPin = 4; //sd card

// Variables initialized
int m0Val[5] = {0,0,0,0,0};
int m1Val[5] = {0,0,0,0,0};
int m2Val[5] = {0,0,0,0,0};
int mAvgVal[4] = {0,0,0,0};

unsigned long count = 0; //How many measurements are written on data file
int i = 0; // variable used to write values of sensor readings

int pumpCount = 0;
int SNSR_T = 1000; // ONE SECOND = 1000. TIME BETWEEN SENSOR MEASUREMENTS

unsigned long previousMillis = 0;

// the setup routine runs once when you press reset:
void setup() {
// Start communication with computer
Serial.begin(9600);
delay(1000);
// initialize the pins as an output or input.
pinMode(LED, OUTPUT); // set pin LED to current output mode
pinMode(LEDG, OUTPUT); // set pin LEDG to current output mode
pinMode(PUMP, OUTPUT); // set pin PUMP to current output mode

pinMode(mst0, INPUT); // Set mst sensor pins to input mode
pinMode(mst1, INPUT);
pinMode(mst2, INPUT);

//Condition: stop the program if SD card is not recording data
//RED LED will start blinking
while(!SD.begin(sdPin))
{
LED_Blink(LED, 2000, 0);
}
}
// the loop routine runs over and over again non-stop:
void loop() {

unsigned long currentMillis = millis();

if ( (unsigned long) currentMillis - previousMillis >= SNSR_T) //START OF SENSOR DATA TAKING
{
// Gather information from sensors and store in variables m0Val to m3Val //
m0Val = analogRead(mst0); // snsor 0 reading
_ m1Val = analogRead(mst1); // snsr 1 reading_
_ m2Val = analogRead(mst2); // snsr 2 reading
* i++;*_

// Take the average of the sensor values, store into variable mAvg
if (i==5){
* for (int n=0; n<=4; n++){*
* mAvgVal[0]= mAvgVal[0]+ m0Val[n];*
* }*
* mAvgVal[0]/=5;*

* for (int n=0; n<=4; n++){*
* mAvgVal[1]= mAvgVal[1]+ m1Val[n];*
* }*
* mAvgVal[1]/=5;*
* for (int n=0; n<=4; n++){*
* mAvgVal[2]= mAvgVal[2]+ m2Val[n];*
* }*
* mAvgVal[2]/=5; *

* mAvgVal[3]=mAvgVal[0]+mAvgVal[1]+mAvgVal[2];*

* mAvgVal[3]/=3;*

//Increase sensor reading frequency when pump is on
if(mAvgVal[3]<=150){
* SNSR_T = 1000;
_}
else if (mAvgVal[3]>=400){_

SNSR_T = 1000;
_}*_

* //turn pump on/off when moisture reaches a defined threshold*
* if(mAvgVal[3]<=150){*
* SWITCH_PUMP;
_ digitalWrite(PUMP, HIGH);
}
if (mAvgVal[3]>=400){
digitalWrite(PUMP, LOW);
}*_

* //find amount of time pump is on then convert to minutes*
* unsigned long pumptstrt = 0;*
* unsigned long pumpt = 0;*
* if(mAvgVal[3]<=150){*
* pumptstrt = millis();*
* delay(200);*
}

* else if (mAvgVal[3]>=400){*
* pumpt = millis()-pumpstrt;*
* delay(200);*
*} *
* pumpt = pumpt/1000;*

* //Open text file. After designated time (line 134) data will be saved in a new file *

* if (currentMillis >= 30000) {*
File dataFile = SD.open("TmAvg2.txt", FILE_WRITE);
* if (dataFile)*
* {*
* //if(count==0){ // if on the first run (count=0), print some information on the screen*
* dataFile.println("Count\tm0\tm1\tm2\tmAvg\tpumpt");*
* // }*
* dataFile.print(count);*

* for(int n=0; n <=3; n++)*
* {*
* dataFile.print("\t"); *
* dataFile.print(mAvgVal[n]);*
* if(n==3){*
* dataFile.print("\t"); *
* dataFile.print(pumpt);*
* dataFile.println();*
* }*
* }*
* dataFile.close();*
* LED_Blink(LEDG, 500, 0);*

* }*
* else{*
* LED_Blink(LED, 500, 2);
_ }
}
else {_
File dataFile = SD.open("TmAvg.txt", FILE_WRITE);
_ if (dataFile)
{
if(count==0){ // if on the first run (count=0), print some information on the screen*
* dataFile.println("Count\tm0\tm1\tm2\tmAvg\tpumpt");
}
dataFile.print(count);*_

* for(int n=0; n <=3; n++)*
* {*
* dataFile.print("\t"); *
* dataFile.print(mAvgVal[n]);*
* if(n==3){*
* dataFile.print("\t"); *
* dataFile.print(pumpt);*
* dataFile.println();*
* }*
* }*
* dataFile.close();*
* LED_Blink(LEDG, 500, 0);*

* }*
* else{*
* LED_Blink(LED, 500, 2);
_ }
}
// Printing valuable information: count, average (from array), sensors m0 to m3*
* Serial.print(count);Serial.print("\t\t");
Serial.print(mAvgVal[0]);Serial.print("\t\t");
Serial.print(mAvgVal[1]);Serial.print("\t\t");
Serial.print(mAvgVal[2]);Serial.print("\t\t");
Serial.print(mAvgVal[3]);Serial.print("\t\t");
Serial.print(pumpt);Serial.println("\t\t");*_

// Adding +1 to count (a run has finished and values have been written)
count++;
i=0;

for(int n=0; n<=3; n++){
* mAvgVal[n]=0;*
}
}//End data writting
* previousMillis = millis();*
}//End sensor data gathering
}//End Loop
void LED_Blink(int color, int dur, int blinks){
* for(int n=0; n<=blinks; n++){*
* digitalWrite(color, HIGH);*
* delay(dur);*
* digitalWrite(color, LOW);*
* if(n>0){*
* delay(dur/2);*
* }*
* }*
}
void SWITCH_PUMP(){
* if(digitalRead(PUMP)==HIGH){*
* digitalWrite(PUMP, LOW); *
* }*
* else{*
* digitalWrite(PUMP, HIGH);*
* }*
}

See my previous reply about the need for () in the call to SWITCH_PUMP.

Get rid of the ;
#define SENSORS 3;
#define PUMP_TIME 150000;

As mentioned this does what?
SWITCH_PUMP;

Use CTRL T to format the sketch. Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]

Get rid of (unsigned long)
if ( (unsigned long) currentMillis - previousMillis >= SNSR_T) //START OF SENSOR DATA TAKING
.

SWITCH_PUMP is a function to turn the pump on and off and I am able to call it perfectly fine. What I need help with is finding the amount of time that the pump is 'HIGH'. The code that I have for that doesn't even verify so I cant run it and check with serial.prints. I'm just looking for code that will help me find that desired time interval. Sorry for not formatting the code, here it is again.

#include <SPI.h> //Libraries provide pre-written functions
#include <SD.h>


//Pin Declaration
const int LED = 7; // Red Led at pin 7
const int LEDG = 3; // Green Led at pin 4
const int mst0 = A0; // Moisture Sensor at Analog 0, A0
const int mst1 = A1; // Moisture Sensor at Analog 1, A1
const int mst2 = A2; // Moisture Sensor at Analog 2, A2

const int PUMP = 8; // Pump SWITCH signal pin

const int sdPin = 4; //sd card

// Variables initialized
int m0Val[5] = {0,0,0,0,0};
int m1Val[5] = {0,0,0,0,0};
int m2Val[5] = {0,0,0,0,0};
int mAvgVal[4] = {0,0,0,0};


unsigned long count = 0; //How many measurements are written on data file
int i = 0; // variable used to write values of sensor readings

int pumpCount = 0;
int SNSR_T = 1000; // ONE SECOND = 1000. TIME BETWEEN SENSOR MEASUREMENTS

unsigned long previousMillis = 0;


// the setup routine runs once when you press reset:
void setup() {
// Start communication with computer
  Serial.begin(9600);
delay(1000);
// initialize the pins as an output or input.
  pinMode(LED, OUTPUT); // set pin LED to current output mode
  pinMode(LEDG, OUTPUT); // set pin LEDG to current output mode
  pinMode(PUMP, OUTPUT); // set pin PUMP to current output mode
  
  pinMode(mst0, INPUT); // Set mst sensor pins to input mode
  pinMode(mst1, INPUT);
  pinMode(mst2, INPUT);
  

//Condition: stop the program if SD card is not recording data
//RED LED will start blinking
  while(!SD.begin(sdPin))
  {
    LED_Blink(LED, 2000, 0);
  }
}
// the loop routine runs over and over again non-stop:
void loop() {

unsigned long currentMillis = millis();

if ( currentMillis - previousMillis >= SNSR_T) //START OF SENSOR DATA TAKING 
{
// Gather information from sensors and store in variables m0Val to m3Val //
  m0Val[i] = analogRead(mst0); // snsor 0 reading
  m1Val[i] = analogRead(mst1); // snsr 1 reading
  m2Val[i] = analogRead(mst2); // snsr 2 reading
  i++;
  
//  Take the average of the sensor values, store into variable mAvg

if (i==5){
      for (int n=0; n<=4; n++){
        mAvgVal[0]= mAvgVal[0]+ m0Val[n]; 
      }
      mAvgVal[0]/=5;
      
      for (int n=0; n<=4; n++){
       mAvgVal[1]= mAvgVal[1]+ m1Val[n]; 
      }
      mAvgVal[1]/=5;

      for (int n=0; n<=4; n++){
       mAvgVal[2]= mAvgVal[2]+ m2Val[n]; 
      }
      mAvgVal[2]/=5;  
   
   
    mAvgVal[3]=mAvgVal[0]+mAvgVal[1]+mAvgVal[2];
   
   mAvgVal[3]/=3;

   
 //Increase sensor reading frequency when pump is on
 if(mAvgVal[3]<=150){
   SNSR_T = 1000;
}
 else if (mAvgVal[3]>=400){
   SNSR_T = 1000;
 }

 
  //turn pump on/off when moisture reaches a defined threshold
   if(mAvgVal[3]<=150){
  SWITCH_PUMP;
  digitalWrite(PUMP, HIGH);
 }
 if (mAvgVal[3]>=400){
  digitalWrite(PUMP, LOW);
 }
  

  //find amount of time pump is on then convert to minutes
  unsigned long pumptstrt = 0;
  unsigned long pumpt = 0;

  if(mAvgVal[3]<=150){
     pumptstrt = millis();
     delay(200);
 }
   
  else if (mAvgVal[3]>=400){
     pumpt = millis()-pumpstrt;
     delay(200);
 }   
  pumpt = pumpt/1000;
 
 
  //Open text file. After designated time (line 134) data will be saved in a new file   
    
  if (currentMillis >= 30000) {
File dataFile = SD.open("TmAvg2.txt", FILE_WRITE);
    if (dataFile)
    {
       //if(count==0){ // if on the first run (count=0), print some information on the screen
         dataFile.println("Count\tm0\tm1\tm2\tmAvg\tpumpt");
    // }
    dataFile.print(count);
   
  for(int n=0; n <=3; n++)
    {
      dataFile.print("\t");   
      dataFile.print(mAvgVal[n]);
      if(n==3){
        dataFile.print("\t");   
        dataFile.print(pumpt);
        dataFile.println();
      }
    }
    dataFile.close();
    LED_Blink(LEDG, 500, 0);
    
  }
  else{
    LED_Blink(LED, 500, 2);
  }
  }
  else {
     File dataFile = SD.open("TmAvg.txt", FILE_WRITE);
  if (dataFile)
    {
       if(count==0){ // if on the first run (count=0), print some information on the screen
         dataFile.println("Count\tm0\tm1\tm2\tmAvg\tpumpt");
     }
    dataFile.print(count);
   
  for(int n=0; n <=3; n++)
    {
      dataFile.print("\t");   
      dataFile.print(mAvgVal[n]);
      if(n==3){
        dataFile.print("\t");   
        dataFile.print(pumpt);
        dataFile.println();
      }
    }
    dataFile.close();
    LED_Blink(LEDG, 500, 0);
    
  }
  else{
    LED_Blink(LED, 500, 2);
  }
  }

// Printing valuable information: count, average (from array), sensors m0 to m3
    Serial.print(count);Serial.print("\t\t");
    Serial.print(mAvgVal[0]);Serial.print("\t\t");
    Serial.print(mAvgVal[1]);Serial.print("\t\t");
    Serial.print(mAvgVal[2]);Serial.print("\t\t");
    Serial.print(mAvgVal[3]);Serial.print("\t\t");
    Serial.print(pumpt);Serial.println("\t\t");
 
 
 // Adding +1 to count (a run has finished and values have been written)
 count++; 
 i=0;
 
 
 for(int n=0; n<=3; n++){
  mAvgVal[n]=0;
 }


  pumpt = 0;
}//End data writting
  previousMillis = millis();
}//End sensor data gathering




}//End Loop

void LED_Blink(int color, int dur, int blinks){
   for(int n=0; n<=blinks; n++){
    digitalWrite(color, HIGH);
    delay(dur);
    digitalWrite(color, LOW);
    if(n>0){
      delay(dur/2);
    }
    } 
}

void SWITCH_PUMP(){
  if(digitalRead(PUMP)==HIGH){
    digitalWrite(PUMP, LOW);    
  }
  else{
    digitalWrite(PUMP, HIGH);
  }
}
  //turn pump on/off when moisture reaches a defined threshold
   if(mAvgVal[3]<=150){
  SWITCH_PUMP;
  digitalWrite(PUMP, HIGH);
 }

You call a function like this.
SWITCH_PUMP();

Not like this:
SWITCH_PUMP;

.

TheDesperateOne:
SWITCH_PUMP is a function to turn the pump on and off and I am able to call it perfectly fine.

LarryD:
You call a function like this.
SWITCH_PUMP();

Not like this:
SWITCH_PUMP;

If you don't believe that, uncomment one or other of the marked lines below and see which version gives you serial output.

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("in setup");
//uncomment on of the following
//SWITCH_PUMP; //this doesn't call the function
//SWITCH_PUMP(); //this does call the function
}

void loop() {
  // put your main code here, to run repeatedly:

}

void SWITCH_PUMP()
{
   Serial.println("in switch_pump");
}

But, assuming the pump is switching, this sketch shows a way of capturing the on and off times:

//how long is the pump (led on pin 13) on for....
//this example toggles the output on button press
//captures time it went on
//when it goes off it subtracts that time from current and reports

const int  inputPin = 7;
unsigned long pumpWentOnAt;
unsigned long pumpWentOffAt;
unsigned long pumpWasOnFor;
bool pumpState = LOW;


// Variables will change:
int inputState;         // current state of the input
int lastInputState;     // previous state of the input

void setup()
{
  // initialize the input pin as a input pullup for active low
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, pumpState);
  Serial.begin(9600);
  Serial.println("Press button to toggle the pump...");
}


void loop()
{
  // read the input pin:
  inputState = digitalRead(inputPin);

  // compare the inputState to its previous state (lastInputState)
  if (inputState != lastInputState) //so it changed one way or the other
  {
    if (inputState == LOW) //pressed
    {
      // if the current state is LOW then the input
      // went from not high to low:
      SWITCH_PUMP();
    }
    // save the current state as the last state,
    //for next time through the loop
    lastInputState = inputState;
  }
}//loop

void SWITCH_PUMP()
{
  if (pumpState == LOW)
  {
    pumpState = !pumpState;
    digitalWrite(LED_BUILTIN, pumpState);
    pumpWentOnAt = millis();
    Serial.print("Pump went on at ");
    Serial.print(pumpWentOnAt);
  }

  else if (pumpState == HIGH)
  {
    pumpState = !pumpState;
    digitalWrite(LED_BUILTIN, pumpState);
    pumpWentOffAt=millis();
    Serial.print(", off at ");
    Serial.print(pumpWentOffAt);
    pumpWasOnFor = pumpWentOffAt - pumpWentOnAt;
    Serial.print(" and was on for ");
    Serial.println(pumpWasOnFor);
  }
}//switch_pump