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);*
* }*
}