[RISOLTO] sonda umidità unico led blinking millis

Ciao,
Vorrei che il blink si verificasse solo per "value_A0<400".
Lo scopo di questa richiesta è: visualizzare tre stati di umidità con un singolo led.
Ho provato a modificare il programma inserendo il blinking led con millis ma non funziona, potete aiutarmi?

Grazie in anticipo

/*
 FILE:    hygrometer.05GIU17
 TAG:     LM393, FC28, HYGROMETER, SOIL, MOISTURE, LED, BLINK
 PURPOSE: Soil moisture test program
 PROBES:  LM393+FC28
 VERSION: 05GIU17
 DISPLAY: NC
 COMFORT: humid soil: 200 < value < 500
 LOCATION:Monteriggioni (200m slm)
 AUTHOR:  Antonio Cannavale
 
 Soil moisture sensor connected to Arduino as follow:
 GND -> GND
 VCC -> +5V
 D0  -> D12
 A0  -> A0
 */

// Initializing variables:
unsigned long previousMillis=0; // Store the last time read was updated:
unsigned long previousMillisBlink=0;
const int intervalBlink=500;
const int interval=10000; // Set a delay time (milliseconds):

const int greenLED_A0=12; // Indicator green led connect at Digital PIN D12:

int ledState=LOW;

int soil_moisture_A0=0; // Read from Analog PIN A0:
int value_A0=0; // Store the value coming from the sensor A0:
float volts_A0=0; // Store the volt coming from the sensor A0:

// 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:

  pinMode(soil_moisture_A0,INPUT); // Set analog pin as an INPUT:
  pinMode(greenLED_A0,OUTPUT); // Set digital pin as an OUTPUT:
  
  Serial.println(F("\nSoil moisture sensor"));
  Serial.println(F("--------------------"));

  digitalWrite(greenLED_A0, LOW); // Switch off Green LED Indicator:
  
  delay(500); // Delay to let system boot:

  // Record the time once the setup has completed:
  previousMillis = millis();
  previousMillisBlink = 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_A0=analogRead(soil_moisture_A0); // Read from analog pin:
    volts_A0=(value_A0/1023.0)*5.0; // Conversion to volts:

    // Check soil hymidity:
    if(value_A0>400 & value_A0<500) {
      digitalWrite(greenLED_A0,HIGH); // Swith on indicator led:
      Serial.print(F("humid soil, "));
    }
    if(value_A0<399) {
      Serial.print(F("wet soil, "));
      
      if (millis() - previousMillisBlink >= intervalBlink) {
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
        digitalWrite(greenLED_A0,ledState);
        previousMillisBlink = millis();
      }
    }
    
    if(value_A0>501) {
      digitalWrite(greenLED_A0,LOW); // Swith off indicator led:
      Serial.print(F("dry soil, "));
    }
      
    // Send soil Moisture values to serial communication:
    Serial.print(F("sensor A0: ")) & Serial.print(value_A0) & Serial.print(F(", ")) & Serial.print(volts_A0,2) & Serial.println(F("V"));
    
    previousMillis = millis(); // Store the current time:
    
  } // End if:
  
} // End loop:

ciao,

io un errore lo vedo qua:

if (value_A0 > 400 & value_A0 < 500)

poi se value_A0 è uguale a 400 od a 500, nel tuo sketch, non succede nulla...quindi scarti questi due valori

nella stessa riga vedo un errore anch'io...

    if(value_A0>400 & value_A0<500) {

se vuoi l'AND logico devi usare &&.

Posted by fratt

  • Today at 08:56 pm

Quote

nella stessa riga vedo un errore anch'io...
Code: [Select]

if(value_A0>400 & value_A0<500) {

se vuoi l'AND logico devi usare &&.

intendevo proprio quello ... :smiley:

il 400 e 500 "saltati" sono nel resto del programma...

azz... ho fatto il bacchettone per niente...

fratt:
nella stessa riga vedo un errore anch'io...

    if(value_A0>400 & value_A0<500) {

se vuoi l'AND logico devi usare &&.

Ciao,
io intendevo "tutti i valori compresi tra 400 e 500".
comunque anche inserendo questa modifica il led non lampeggia in quel intervallo.
Avete altri suggerimenti?

Grazie mille in anticipo.

antoniocnn:
io intendevo "tutti i valori compresi tra 400 e 500".

Con (value_A0<399) testi i valori da 0 a 398
Con (value_A0>400 & value_A0<500) testi i valori da 401 a 499
Con (value_A0>501) testi i valori da 502 al massimo.

I valori 399, 400, 500, 501 vengono ignorati (nessuna condizione risulta vera).
In questo specifico caso usare & o && non cambia.

comunque anche inserendo questa modifica il led non lampeggia in quel intervallo.

Quale intervallo? Il lampeggio è messo nella condizione da 0 a 398 ed è scritto giusto, anche se potrebbe essere molto più compatto. Impostando a mano value_A0 ad un valore inferiore a 399 il LED deve lampeggiare.

Claudio_FF:
Quale intervallo?

Mi sono sbagliato, scusate.

Claudio_FF:
Il lampeggio è messo nella condizione da 0 a 398 ed è scritto giusto, anche se potrebbe essere molto più compatto. Impostando a mano value_A0 ad un valore inferiore a 399 il LED deve lampeggiare.

Praticamente quando legge si accende e resta acceso e quando ri-legge si spegne e resta spento.

Ho suggerito di impostare a mano i valori di value_A0 per poter testare il regolare funzionamento di quegli if:
- dando un valore inferiore a 399 il LED deve lampeggiare
- dando un valore superiore a 501 deve stare spento
- dando un valore tra 401 e 499 deve stare acceso.
(questa è la logica che hai scritto)
Se questi comportamenti sono corretti, allora il problema si sposta da un'altra parte, e la domanda da "perché non lampeggia" diventerebbe: "perché il valore letto di value_A0 è sbagliato?"

Vedo adesso che gli if sono all'interno di un if principale che li blocca per 10 secondi. Per questo non lampeggia.

Il lampeggio va messo nel loop principale fuori dalla struttura:

 if (millis() - previousMillis >= interval) {

Quindi va usata un'altra variabile flag, impostata dentro la struttura, e letta all'esterno dal lampeggiatore:

if (blinkON) {
    if (millis() - previousMillisBlink >= intervalBlink) { 
        previousMillisBlink += intervalBlink;
        ledState = !ledState;
        digitalWrite(greenLED_A0, ledState);
    }
}
else
    previousMillisBlink = millis();

Premetto che sono autoididatta e tutto quello che conosco l'ho appreso da internet.

Ho creato una nuova variabile come suggerito

int blinkON;

Dopo di che ho inserito il codice come suggerito ma non ho capito come proseguire per richiamare il blink quando value_A0<400

// The loop function runs over and over again forever:
void loop() { // Start loop:

  if (blinkON) {
    if (millis() - previousMillisBlink >= intervalBlink) { 
        previousMillisBlink += intervalBlink;
        ledState = !ledState;
        digitalWrite(greenLED_A0, ledState);
    }
  }
  else {
    previousMillisBlink = millis();
  }

  // Check whether it has been long enough:
  if (millis() - previousMillis >= interval) {
    
    // Read Soil Moisture values:
    value_A0=analogRead(soil_moisture_A0); // Read from analog pin:
    volts_A0=(value_A0/1023.0)*5.0; // Conversion to volts:

    // Check soil hymidity:
    if(value_A0>=400 & value_A0<=500) {
      digitalWrite(greenLED_A0,HIGH); // Swith on indicator led:
      Serial.print(F("humid soil, "));
    }
    if(value_A0<400) {
      
      Serial.print(F("wet soil, "));
    }
    
    if(value_A0>501) {
      digitalWrite(greenLED_A0,LOW); // Swith off indicator led:
      Serial.print(F("dry soil, "));
    }
      
    // Send soil Moisture values to serial communication:
    Serial.print(F("sensor A0: ")) & Serial.print(value_A0) & Serial.print(F(", ")) & Serial.print(volts_A0,2) & Serial.println(F("V"));
    
    previousMillis = millis(); // Store the current time:
    
  } // End if:
  
} // End loop:

All'interno degli if che testano gli intervalli, va impostato il valore di blinkON in modo che la funzione all'esterno della struttura (che viene eseguita in continuazione) lampeggi solo quando serve.

Claudio_FF:
All'interno degli if che testano gli intervalli, va impostato il valore di blinkON in modo che la funzione all'esterno della struttura (che viene eseguita in continuazione) lampeggi solo quando serve.

Vediamo se ho capito.

  if(value_A0<400) {
    digitalWrite(blinkON,HIGH);
    Serial.print(F("wet soil, "));
  }

In questo modo?

Grazie in anticipo

antoniocnn:

  if(value_A0<400) {

digitalWrite(blinkON,HIGH);
    Serial.print(F("wet soil, "));
  }



In questo modo?

blinkON non è mica un piedino di uscita di Arduino...

È una variabile per comandare la parte di programma che svolge la funzione di lampeggiatore.

Quindi negli if va impostata a uno o a zero a seconda se vuoi che il LED lampeggi oppure no.

Questo led non ne vuole sapere di lampeggiare.
Ecco il codice che attualmente utilizzo.
Qualcuno può ancora aiutarmi?

Grazie in anticipo

/*
 FILE:    hygrometer.05GIU17
 TAG:     LM393, FC28, HYGROMETER, SOIL, MOISTURE, LED, BLINK, MILLIS
 PURPOSE: Soil moisture test program
 PROBES:  LM393+FC28
 VERSION: 05GIU17
 DISPLAY: NC
 COMFORT: humid soil: 200 < value < 500
 LOCATION:Monteriggioni (200m slm)
 AUTHOR:  Antonio Cannavale
 
 Soil moisture sensor connected to Arduino as follow:
 GND -> GND
 VCC -> +5V
 D0  -> D12
 A0  -> A0
 */

// Initializing variables:
unsigned long previousMillis=0; // Store the last time read was updated:
unsigned long previousMillisBlink=0; // Store last time LED was updated:

// Variables constant:
const int interval=10000; // Set a delay time (milliseconds):
const int greenLED_A0=12; // Indicator green led connect at Digital PIN D12:

// Variables will change:
int ledState=LOW; // ledState used to set the LED

int soil_moisture_A0=0; // Read from Analog PIN A0:
int value_A0=0; // Store the value coming from the sensor A0:

float volts_A0=0; // Store the volt coming from the sensor A0:

// 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:

  pinMode(soil_moisture_A0,INPUT); // Set analog pin as an INPUT:
  pinMode(greenLED_A0,OUTPUT); // Set digital pin as an OUTPUT:
  
  Serial.println(F("\nSoil moisture sensor"));
  Serial.println(F("--------------------"));

  digitalWrite(greenLED_A0, LOW); // Switch off Green LED Indicator:
  
  delay(500); // Delay to let system boot:

  // Record the time once the setup has completed:
  previousMillis = millis();
  previousMillisBlink = 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_A0=analogRead(soil_moisture_A0); // Read from analog pin:
    volts_A0=(value_A0/1023.0)*5.0; // Conversion to volts:

    // Check soil hymidity:
    if(value_A0>=400 & value_A0<=500) {
      digitalWrite(greenLED_A0,HIGH); // Swith on indicator led:
      Serial.print(F("humid soil, "));
    }
    if(value_A0<400) {
      blink(200); // Led will blink every 200ms
      Serial.print(F("wet soil, "));
    }
    
    if(value_A0>501) {
      digitalWrite(greenLED_A0,LOW); // Swith off indicator led:
      Serial.print(F("dry soil, "));
    }
      
    // Send soil Moisture values to serial communication:
    Serial.print(F("sensor A0: ")) & Serial.print(value_A0) & Serial.print(F(", ")) & Serial.print(volts_A0,2) & Serial.println(F("V"));
    // Store the current value: 
    previousMillis = millis();
    
  } // End if:
} // End loop:

// Function Blinking indicator led:
void blink(int intervalBlink) {
  if (millis() - previousMillisBlink >= intervalBlink) {
    // If the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    // Set the LED with the ledState of the variable:
    digitalWrite(greenLED_A0, ledState);
    // Store the current value:
    previousMillisBlink = millis();
  } // End if:
} // End blink:

ciao

se il problema è questo:

antoniocnn:
Praticamente quando legge si accende e resta acceso e quando ri-legge si spegne e resta spento.

... è quello che gli hai detto di fare. :wink:

Per come è scritto il loop si comporta come se ti usassi un bel delay(1000).

Pensa bene cosa deve essere fatto una volta al secondo e cosa deve essere fatto in continuazione.

ciao
pippo72

P.S. non posso (voglio) essere più preciso sennò Guglielmo (giustamente) mi "richiama" :stuck_out_tongue_closed_eyes: :stuck_out_tongue_closed_eyes:

Ciao,
purtroppo le mie capacità non mi permettono di riuscire nel mio scopo.

Vi ringrazio per la disponibilità.

A presto.

Ciao,
credo che neanche sta volta sono riuscito nel mio scopo.
Secondo voi ho fatto bene a smembrare il codice loop in singole funzioni?

Grazie

/*
 FILE:    hygrometer.05GIU17
 TAG:     LM393, FC28, HYGROMETER, SOIL, MOISTURE, LED, BLINK, MILLIS
 PURPOSE: Soil moisture test program
 PROBES:  LM393+FC28
 VERSION: 05GIU17
 DISPLAY: NC
 COMFORT: humid soil: 200 < value < 500
 LOCATION:Monteriggioni (200m slm)
 AUTHOR:  Antonio Cannavale
 
 Soil moisture sensor connected to Arduino as follow:
 GND -> GND
 VCC -> +5V
 D0  -> D12
 A0  -> A0
 */

// Initializing variables:
unsigned long previousMillis=0; // Store the last time read was updated:
unsigned long previousMillisBlink=0; // Store last time LED was updated:

// Variables constant:
const int greenLED_A0=12; // Indicator green led connect at Digital PIN D12:

// Variables will change:
int ledState=LOW; // ledState used to set the LED

int soil_moisture_A0=0; // Read from Analog PIN A0:
int value_A0=0; // Store the value coming from the sensor A0:

float volts_A0=0; // Store the volt coming from the sensor A0:

// 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:

  pinMode(soil_moisture_A0,INPUT); // Set analog pin as an INPUT:
  pinMode(greenLED_A0,OUTPUT); // Set digital pin as an OUTPUT:

  Serial.println(F("\nSoil moisture sensor"));
  Serial.println(F("--------------------"));

  digitalWrite(greenLED_A0, LOW); // Switch off Green LED Indicator:
  
  // Record the time once the setup has completed:
  previousMillis = millis();
  previousMillisBlink = millis();

} // End setup:

// The loop function runs over and over again forever:
void loop() { // Start loop:

  read (10000); // Read value every 10s:
  check (); // Check state soil humidity:
  blink (200); // Blink led every 200ms.
  
} // End loop:


// Function read soil hymidity:
void read(int interval) {
  
  // Check whether it has been long enough:
  if (millis() - previousMillis >= interval) {
    
    // Read Soil Moisture values:
    value_A0=analogRead(soil_moisture_A0); // Read from analog pin:
    volts_A0=(value_A0/1023.0)*5.0; // Conversion to volts:
    
    // Send soil Moisture values to serial communication:
    //Serial.print(F("Time : ")) & Serial.print(millis()) & Serial.print(F(", "));
    Serial.print(F("sensor A0: ")) & Serial.print(value_A0) & Serial.print(F(", ")) & Serial.print(volts_A0,2) & Serial.println(F("V"));
    
    previousMillis = millis(); // Store the current time:
  }
} // End read:

// Function check state soil himidity:
void check() {
  if(value_A0>=400 && value_A0<=500) {
  digitalWrite(greenLED_A0,HIGH); // Swith on indicator led:
  Serial.print(F("humid soil, "));
  }
  if(value_A0<400) {
    blink(200); // Led will blink every 200ms
    Serial.print(F("wet soil, "));
  }
  if(value_A0>501) {
    digitalWrite(greenLED_A0,LOW); // Swith off indicator led:
    Serial.print(F("dry soil, "));
  }
} // End check:

// Function blinking indicator led:
int blink(int intervalBlink) {
  if (millis() - previousMillisBlink >= intervalBlink) {
    // If the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    // Set the LED with the ledState of the variable:
    digitalWrite(greenLED_A0, ledState);
    // Store the current value:
    previousMillisBlink = millis();
  }
} // End blink:
[code]

ciao,

secondo me questo potrebbe essere un problema:

int soil_moisture_A0 = 0; // Read from Analog PIN A0:
pinMode(soil_moisture_A0, INPUT); // Set analog pin as an INPUT:

stai attribuendo a soil_moisture_A0 il valore 0 (non A0) e poi usi questa variabile per dichiarare il pin 0 come input...che è un digitale.
ricorda inoltre che per in pin analogici non serve dichiarare il pinmode() quando sono usati come input.

secondo me dovresti dichiarare soil_moisture_A0 in questo modo:

#define soil_moisture_A0 A0

non dichiarare il pinmode()...in questo modo ai tuoi analogRead(soil_moisture_A0) è come se scrivessi analogRead(A0).

questo è quello che ho visto...prova e facci sapere.

Ho modificato lo sketch come indicato da ORSO2001.
Come potete vedere ho inserito // accanto alle linee di codice per tracciare i cambiamenti.
Il led lampeggia continuamente.

/*
 FILE:    hygrometer.05GIU17
 TAG:     LM393, FC28, HYGROMETER, SOIL, MOISTURE, LED, BLINK, MILLIS
 PURPOSE: Soil moisture test program
 PROBES:  LM393+FC28
 VERSION: 05GIU17
 DISPLAY: NC
 COMFORT: humid soil: 200 < value < 500
 LOCATION:Monteriggioni (200m slm)
 AUTHOR:  Antonio Cannavale
 
 Soil moisture sensor connected to Arduino as follow:
 GND -> GND
 VCC -> +5V
 D0  -> D12
 A0  -> A0
 */

// Initializing variables:
unsigned long previousMillis=0; // Store the last time read was updated:
unsigned long previousMillisBlink=0; // Store last time LED was updated:

// Variables constant:
const int greenLED_A0=12; // Indicator green led connect at Digital PIN D12:

// Variables will change:
int ledState=LOW; // ledState used to set the LED

//int soil_moisture_A0=0;
#define soil_moisture_A0 A0 // Read from Analog PIN A0:

int value_A0=0; // Store the value coming from the sensor A0:
float volts_A0=0; // Store the volt coming from the sensor A0:

// 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:

  //pinMode(soil_moisture_A0,INPUT); // Set analog pin as an INPUT:
  pinMode(greenLED_A0,OUTPUT); // Set digital pin as an OUTPUT:

  Serial.println(F("\nSoil moisture sensor"));
  Serial.println(F("--------------------"));

  digitalWrite(greenLED_A0, LOW); // Switch off Green LED Indicator:
  
  // Record the time once the setup has completed:
  previousMillis = millis();
  previousMillisBlink = millis();

} // End setup:

// The loop function runs over and over again forever:
void loop() { // Start loop:

  read (10000); // Read value every 10s:
  check (); // Check state soil humidity:
  blink (200); // Blink led every 200ms.
  
} // End loop:


// Function read soil hymidity:
void read(int interval) {
  
  // Check whether it has been long enough:
  if (millis() - previousMillis >= interval) {
    
    // Read Soil Moisture values:
    value_A0=analogRead(soil_moisture_A0); // Read from analog pin:
    volts_A0=(value_A0/1023.0)*5.0; // Conversion to volts:
    
    // Send soil Moisture values to serial communication:
    //Serial.print(F("Time : ")) & Serial.print(millis()) & Serial.print(F(", "));
    Serial.print(F("sensor A0: ")) & Serial.print(value_A0) & Serial.print(F(", ")) & Serial.print(volts_A0,2) & Serial.println(F("V"));
    
    previousMillis = millis(); // Store the current time:
  }
} // End read:

// Function check state soil himidity:
void check() {
  if(value_A0>=400 && value_A0<=500) {
  digitalWrite(greenLED_A0,HIGH); // Swith on indicator led:
  Serial.print(F("humid soil, "));
  }
  if(value_A0<400) {
    blink(200); // Led will blink every 200ms
    Serial.print(F("wet soil, "));
  }
  if(value_A0>501) {
    digitalWrite(greenLED_A0,LOW); // Swith off indicator led:
    Serial.print(F("dry soil, "));
  }
} // End check:

// Function blinking indicator led:
int blink(int intervalBlink) {
  if (millis() - previousMillisBlink >= intervalBlink) {
    // If the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    // Set the LED with the ledState of the variable:
    digitalWrite(greenLED_A0, ledState);
    // Store the current value:
    previousMillisBlink = millis();
  }
} // End blink:

ciao...scusa mi sono un attimo perso...il fatto che lampeggi continuamente è quello che vuoi?

nel loop() hai inserito la funzione blink(200); dove 200 è l'intervallo per il confronto.

la funzione blink() dice:

// Function blinking indicator led:
int blink(int intervalBlink) {
  if (millis() - previousMillisBlink >= intervalBlink) {
    // If the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    // Set the LED with the ledState of the variable:
    digitalWrite(greenLED_A0, ledState);
    // Store the current value:
    previousMillisBlink = millis();
  }
} // End blink:

tralasciando che la funzione la potevi dichiarare come void in quanto non torna alcunché... in pratica ogni 200 ms il LED cambia di stato indipendentemente da altro...altro consiglio è quello di chiamare le tue funzioni con "nome" diverso da quello dello namespace standard...come vedi blink e read hanno colore diverso (arancio) da check (nero)...questo perchè sia blink che read sono presenti nella lista Keywords in quanto presenti in librerie in uso...