// 4095 is the number when sensor is dry // around 1664 submerged, technically got down to 1403 but that was an outlier int plantPin = 32; int sensorValue; int numReadings = 10; int water = 1664; int dry = 4095; int high = 0; int low = 101; char userInput; float averages[144];//Arrey of average values taken every 10 minutes int highs[144]; int lows[144]; int dataPoints[602];//extra two spots unsigned long startTime; int d; int averagedValue; int a = 0; float average; void setup(){ pinMode(plantPin, INPUT); startTime = millis(); Serial.begin(115200); d=0; //clears arrey for (int i=0; i<144; i++){ averages[i]=0; highs[i]=0; lows[i]=0; } } //Prints out Results void showData(float averages[144], int highs[144], int lows[144]){ Serial.println("Data shown below is in the format: Time in mins, 10 min average, high, low"); for (int i=0; i<144; i++){ int times; times = 10*i +10; Serial.print(times); Serial.print(", "); Serial.print(averages[i]); Serial.print(", "); Serial.print(highs[i]); Serial.print(", "); Serial.println(lows[i]); average = average+averages[i]; } average = average/144.0; Serial.print("Overall average:"); Serial.println(average); } void showAverages(float averages[144]){ Serial.println("10 min averages"); for (int i=0; i<144; i++){ Serial.println(averages[i]); } } void showHighsOrLows(int highs[144]){ for (int i=0; i<144; i++){ Serial.println(highs[i]); } } //Makes each individual data point more accurate, reduces outliers int smooth(){ int sum = 0; // Read the sensor multiple times and add them together for (int i = 0; i < numReadings; i++) { sensorValue = analogRead(plantPin); sum += sensorValue; delay(100); // Delay between consecutive readings } // Calculate the average value averagedValue = sum / numReadings; averagedValue = map(averagedValue,dry,water,0,100); return averagedValue; } void loop() { dataPoints[d] = smooth(); d++; // Print the averaged value Serial.print("Time since program start in seconds:"); Serial.print(millis()/1000); Serial.print(" DATA POINT:"); Serial.println(averagedValue); Serial.print("# of data points collected for current intervul:"); Serial.println(d);//for testing //high if (averagedValue >= high){ high = averagedValue; } //low if (averagedValue <= low){ low = averagedValue; } unsigned long mins = (millis()-startTime)/60000; if (mins >= 10){//back to 10___________________________________________________________________________________________________________________________ Serial.println("logging an average++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");//for testing //Calculate Average using dataPoints arrey int total = 0; for (int x=0; x<600; x++){//600 form 60_______________________________________________________________________________________________________ total = total + dataPoints[x]; } //re-start time etc. startTime = millis(); if (a<144){ averages[a] = total/600.0;//back to 600____________________________________________________________________________________________________ lows[a] = low; highs[a] = high; a++; } else { Serial.println("Time is up!"); } high=0; low=101; d=0; } userInput = Serial.read();//Checks for input by user if (userInput == 'r'){ //make function for data report showData(averages, highs, lows); } if (userInput == 'a'){ showAverages(averages); } if (userInput == 'h'){ Serial.println("Highs"); showHighsOrLows(highs); } if (userInput == 'l'){ Serial.println("Lows"); showHighsOrLows(lows); } }