Hi! I have a pretty basic question about using the Serial.print with an if/else statement. I want to output a moisture value of 0 if the voltage reading is 5, but if the voltage is below 5 I want to use an equation to calculate moisture and print that value instead. I can’t seem to code this properly and either only get a moisture of 0 (for any voltage reading) or a calculated value (so a voltage reading of 5 does not give 0). The current iteration of my code is below, thank you!
void loop() {
//first sensor
int sensorVal = analogRead(sensorPin);
//prints time since program started
Serial.print("Time_1: “);
time = millis();
Serial.print(time);
// wait a second so as not to send massive amounts of data
Serial.print(”, Serial Value: “);
Serial.print(sensorVal);
//convert the ADC reading to voltage
float voltage = (sensorVal/1024.0)*5.0;
Serial.print(”, Volts: “);
Serial.print(voltage);
Serial.print(”, Moisture Content: ");
float moisture;
if(voltage == 5){
moisture = 0;
Serial.println(moisture);
} else {
moisture = ((voltage - 4.98)/-0.089);
Serial.println(moisture);
}