Ciao,
ho riscritto il codice per il sensore umidità terreno inserendo la funzione millis() al posto del delay ma vorrei sapere dalla comunità la correttezza delle operazioni.
Mi sono accorto di alcune anomalie nel monitor seriale. Nello specifico dovrei attendere il valore ogni 5000ms mentre vengono eseguite letture continue.
/*
FILE: hygrometer.17MAG17
TAG: LM393, FC28
PURPOSE: Soil moisture test program
PROBES: LM393+FC28
VERSION: 17MAG17
DISPLAY:
COMFORT: 400 mV soil, DRY soil: 1023, WET soil: 0
LOCATION:Monteriggioni (200m slm)
AUTHOR: Antonio Cannavale
Soil moisture sensor connected to Arduino as follow:
GND -> GND
VCC -> +5V
D0 -> NC
A0 -> A0
*/
// Initializing variables:
unsigned long previousMillis = 0; // Store the last time we changed something:
const long interval = 5000; // Set a delay time (milliseconds):
int soil_moisture = 0; // Read from analog pin A0:
int value = 0; // Store the value coming from the sensor:
float volts = 0; // Store the volt coming from the sensor
// Set to true to print status messages to the serial:
boolean DEBUG = true;
// The setup function runs once when you press reset or power the board:
void setup() { // Start setup:
// We start the serial library to output our messages:
if (DEBUG) {
Serial.begin(9600);
} // End if:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only:
} // End while:
Serial.println(F("\nSoil moisture sensor"));
Serial.println(F("--------------------"));
delay(5000); // Delay to let system boot:
// Record the time once the setup has completed:
unsigned long currentMillis = millis();
} // End setup:
// The loop function runs over and over again forever:
void loop() { // Start loop:
if (millis() - previousMillis >= interval) {
// Read Soil Moisture values:
value = analogRead(soil_moisture); // Read from analog pin:
volts = (value/1023.0)*5.0; // Conversion to volts:
// Send soil Moisture values to serial communication:
Serial.print(value) & Serial.print(F("\t")) & Serial.print(volts) & Serial.println(F("mV"));
} // End if:
} // End loop:
Hai dimenticato di aggiornare "previousMillis" alla fine del relativo IF, per cui, una volta superato il valore di "interval" il suddetto IF è sempre vero.
Guglielmo
_P.S.: Poi mi spieghi quel "if (DEBUG)" nel setup() ... _ ... quando si fa copia/incolla bisognerebbe almeno cercare di farlo bene e capire cosa si fa ... :
gpb01:
Hai dimenticato di aggiornare "previousMillis" alla fine del relativo IF, per cui, una volta superato il valore di "interval" il suddetto IF è sempre vero.
Fatto, ora tutto funziona.
Allego il codice riscritto.
/*
FILE: hygrometer.17MAG17
TAG: LM393, FC28
PURPOSE: Soil moisture test program
PROBES: LM393+FC28
VERSION: 17MAG17
DISPLAY:
COMFORT: 400 mV soil, DRY soil: 1023, WET soil: 0
LOCATION:Monteriggioni (200m slm)
AUTHOR: Antonio Cannavale
Soil moisture sensor connected to Arduino as follow:
GND -> GND
VCC -> +5V
D0 -> NC
A0 -> A0
*/
// Initializing variables:
long previousMillis = 0; // Store the last time we changed something:
int interval = 5000; // Set a delay time (milliseconds):
int soil_moisture = 0; // Read from analog pin A0:
int value = 0; // Store the value coming from the sensor:
float volts = 0; // Store the volt coming from the sensor
// Set to true to print status messages to the serial:
boolean DEBUG = true;
// The setup function runs once when you press reset or power the board:
void setup() { // Start setup:
// We start the serial library to output our messages:
if (DEBUG) {
Serial.begin(9600);
} // End if:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only:
} // End while:
Serial.println(F("\nSoil moisture sensor"));
Serial.println(F("--------------------"));
delay(1000); // Delay to let system boot:
// Record the time once the setup has completed:
previousMillis = millis();
} // End setup:
// The loop function runs over and over again forever:
void loop() { // Start loop:
// Check whether it has been long enough.
if (millis() - previousMillis > interval) {
// Read Soil Moisture values:
value = analogRead(soil_moisture); // Read from analog pin:
volts = (value/1023.0)*5.0; // Conversion to volts:
// Send soil Moisture values to serial communication:
Serial.print(value) & Serial.print(F("\t")) & Serial.print(volts,4) & Serial.println(F("mV"));
// Store the current time:
previousMillis = millis();
} // End if:
} // End loop: