So I have a vermicomposter in my basement that we put all of our food scraps in . It was drying out so I made an automatic misting system for it to keep it moist. The first code I had worked ok, although I know it was not optimal. I threw it together quickly and let it go along nicely for two years. Here is that code.
int sensor0Pin = A3;
int sensor1Pin = A5;
int solenoidWaterPin = 8;
int soil0 = 0;
int soil1 = 0;
const int highThreshold = 825;
const int dryestReading = 625;
const int overSaturation = 400;
const long sprayInterval = 20000;
const long waitInterval = 120000;
const long serialWriteInterval = 3000;
unsigned long previousMillis = 0;
unsigned long lastUpdate = 0;
boolean thresholdSpray = false;
void setup()
{Serial.begin(9600);
pinMode(solenoidWaterPin,OUTPUT);
digitalWrite(solenoidWaterPin, HIGH);}
void loop()
{int sensor0Value = analogRead(sensor0Pin); // Read the value of the front moisture sensor.
int sensor1Value = analogRead(sensor1Pin); // Read the value of the rear moisture sensor.
sensor0Value = constrain(sensor0Value, 300, 1023);
sensor1Value = constrain(sensor1Value, 300, 1023);
unsigned long currentMillis = millis();
//Map the value to a percentage
soil0 = map(sensor0Value, 300, 1023, 100, 0);
soil1 = map(sensor1Value, 300, 1023, 100, 0);
if (currentMillis - lastUpdate >= serialWriteInterval) {
lastUpdate = currentMillis;
Serial.print("Soil saturation at front is ");
Serial.print(soil0);
Serial.println("%");
Serial.print("Soil saturation at rear is ");
Serial.print(soil1);
Serial.println("%");
Serial.println("");
Serial.print("Front Sensor = ");
Serial.println(sensor0Value);
Serial.print("Rear Sensor = ");
Serial.println(sensor1Value);
Serial.println("");
Serial.println(thresholdSpray);
}
// If the analog value is high enough, turn on the solenoid relay. If we have sprayed, wait for water to saturate before allowing another spray.
if (sensor0Value < overSaturation || sensor1Value < overSaturation || sensor0Value > highThreshold || sensor1Value > highThreshold)
{digitalWrite(solenoidWaterPin,HIGH);
}
else if (thresholdSpray == false && (sensor0Value > dryestReading || sensor1Value > dryestReading) && (currentMillis - previousMillis >= waitInterval))
{ digitalWrite(solenoidWaterPin, LOW);
previousMillis = currentMillis;
thresholdSpray = true;
}
else if (thresholdSpray == true && (currentMillis - previousMillis >= sprayInterval))
{digitalWrite(solenoidWaterPin,HIGH);
previousMillis = currentMillis;
thresholdSpray = false;
}
}
// (sensor0Value > highThreshold or sensor1Value > highThreshold)
So in the serial monitor I would get values in the 600- 700 range when the soil was sort of drying out. Below that when sufficiently saturated and above that when dry. I’ve changed the code in order to update a google spreadsheet so I can keep an eye on things remotely. I’ve tried to put everything in its own function, but it’s not working correctly. So to the crux of my problem. With the following code the analog value always returns 300. I can reload my old sketch and the values read in the 650 range. I have tried moving the declaration into different sections including global scope. I’ve been trying to learn about references, pointers and functions, but I am not finding a whole lot of info. Especially for Ardiuino. And tips would be appreciated. I ran out of room so I will post my new code in the post after this. Thanks