I'm building an irrigation system that monitors the moisture content in the plants soil and then logs it on an SD card. In the code I have written below, the sensors take readings every 24 seconds (SNSR_T=24000) and logs them every 2 minutes (data is logged after 5 readings). This works perfectly fine, however when I change SNSR_T to 36000 (so data is logged every 3 minutes) or anything bigger, it doesn't work. Does anybody know why this is? Does arduino have an issue with performing arithmetic functions that have larger solutions? Any advice will help, thanks!
//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 SNSR_T = 24000; // ONE SECOND = 1000. TIME BETWEEN SENSOR MEASUREMENTS
long pumpt = 0;
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;
//turn pump on when moisture reaches a defined threshold
if (mAvgVal[3] <= 150) {
digitalWrite(PUMP, HIGH);
SNSR_T = 1000; //Increase sensor reading frequency when pump is on
}
//determine how long pump is on
if (digitalRead(PUMP) == HIGH) {
pumpt = pumpt + 5;
}
//turn pump off when moisture reaches a defined threshold or pump exceeds max time on
if (mAvgVal[3] >= 350 || pumpt >= 900) {
digitalWrite(PUMP, LOW);
SNSR_T = 24000; //return sensor reading frequency to original
}
//Open text file. After designated time (line 119) data will be saved in a new file
if (currentMillis >= 216000000) {
File dataFile = SD.open("TmAvg2.txt", FILE_WRITE);
if (dataFile)
{
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;
//return pumpt to 0 when pump is off
if (mAvgVal[3] >= 350 || pumpt >= 900) {
pumpt = 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);
}
}
}