Buonasera a tutto il forum.
Dopo aver risolto un enorme problema di sequenzialita' di.apertura e chiusura elettrovalvole azionate a solenoidi, si presenta immediatamente un nuovo problema....nooooooooo.....
La necessità è quella di inserire il nuovo codice per l'apertura sequenziale delle valvole all'interno di un.switch case, in quanto questa è solo una possibile selezione in quanto le valvole possono essere in automatico, problema risolto, o in manuale accese o spente.
Il codice con millis dopo.tanto tempo e prove adesso funziona perfettamente, ma quando lo inserisco in un switch case, le aperture non rispettano più la precedente sequenza che in uno sketch per i fatti suoi invece funziona perfettamente.
Quale potrebbe essere il problema?
Vi ringrazio per qualsiasi aiuto o suggerimento.
ciao Danpis,
inserisci lo sketch...ed indica la parte che non funziona
Grazie ORSO per la risposta, ti allego di seguito lo sketch funzionante
// Hydroponic Valve timing management
const int ELT1Pin = 7; // Pin connect to use valve 1
const int ELT2Pin = 9; // Pin connect to use valve 2
const int ELT3Pin = 8; // Pin connect to use valve 3
const int ELT4Pin = 13; // Pin connect to use valve 4
int ELT1ledState = LOW; // Variable for valve 1 used to check previous valve state
int ELT2ledState = LOW; // Variable for valve 2 used to check previous valve state
int ELT3ledState = LOW; // Variable for valve 3 used to check previous valve state
int ELT4ledState = LOW; // Variable for valve 4 used to check previous valve state
unsigned long ELT1previousMillis = 0; // will store last time ELT1 was updated
unsigned long ELT2previousMillis = 0; // will store last time ELT2 was updated
unsigned long ELT3previousMillis = 0; // will store last time ELT3 was updated
unsigned long ELT4previousMillis = 0; // will store last time ELT4 was updated
long ELTOnTime = 1000; // Time Valve on
long ELTOffTime = 3 * ELTOnTime; // Don't modify, this value is chained with on time.
void setup()
{
Serial.begin(9600);
// set the digital pin as output:
pinMode(ELT1Pin, OUTPUT);
pinMode(ELT2Pin, OUTPUT);
pinMode(ELT3Pin, OUTPUT);
pinMode(ELT4Pin, OUTPUT);
}
void loop(){
automatic();
} // End of the Loop
void automatic() {
// check to see if it's time to change the state of the VALVE
unsigned long ELT1currentMillis = millis() + (ELTOnTime * 3); // Value to modify to add or subtract valves (example : 5 valves *4, *3, *2)
unsigned long ELT2currentMillis = millis() + (ELTOnTime * 2); // (example : 3 valves *2, *1)
unsigned long ELT3currentMillis = millis() + (ELTOnTime * 1); // (example : 2 valves *1)
unsigned long ELT4currentMillis = millis() ; // The last valve value MUST BE EMPTY
// Valve 1 Management----------------------------------------------------------------------
if((ELT1ledState == HIGH) && (ELT1currentMillis - ELT1previousMillis >= ELTOnTime))
{
ELT1ledState = LOW; // Turn it off
ELT1previousMillis = ELT1currentMillis; // Remember the time
digitalWrite(ELT1Pin, ELT1ledState); // Update the actual LED
}
else if ((ELT1ledState == LOW) && (ELT1currentMillis - ELT1previousMillis >= ELTOffTime))
{
ELT1ledState = HIGH; // turn it on
ELT1previousMillis = ELT1currentMillis; // Remember the time
digitalWrite(ELT1Pin, ELT1ledState); // Update the actual LED
} // End of valve 1 managment
// Valve 2 Management----------------------------------------------------------------------
if((ELT2ledState == HIGH) && (ELT2currentMillis - ELT2previousMillis >= ELTOnTime))
{
ELT2ledState = LOW; // Turn it off
ELT2previousMillis = ELT2currentMillis; // Remember the time
digitalWrite(ELT2Pin, ELT2ledState); // Update the actual Valve
}
else if ((ELT2ledState == LOW) && (ELT2currentMillis - ELT2previousMillis >= ELTOffTime))
{
ELT2ledState = HIGH; // turn it on
ELT2previousMillis = ELT2currentMillis; // Remember the time
digitalWrite(ELT2Pin, ELT2ledState); // Update the actual Valve
}
// End of valve 2 managment
// Valve 3 Management----------------------------------------------------------------------
if((ELT3ledState == HIGH) && (ELT3currentMillis - ELT3previousMillis >= ELTOnTime))
{
ELT3ledState = LOW; // Turn it off
ELT3previousMillis = ELT3currentMillis; // Remember the time
digitalWrite(ELT3Pin, ELT3ledState); // Update the actual Valve
}
else if ((ELT3ledState == LOW) && (ELT3currentMillis - ELT3previousMillis >= ELTOffTime))
{
ELT3ledState = HIGH; // turn it on
ELT3previousMillis = ELT3currentMillis; // Remember the time
digitalWrite(ELT3Pin, ELT3ledState); // Update the actual Valve
}
// End of valve 3 managment
// Valve 4 Management----------------------------------------------------------------------
if((ELT4ledState == HIGH) && (ELT4currentMillis - ELT4previousMillis >= ELTOnTime))
{
ELT4ledState = LOW; // Turn it off
ELT4previousMillis = ELT4currentMillis; // Remember the time
digitalWrite(ELT4Pin, ELT4ledState); // Update the actual LED
}
else if ((ELT4ledState == LOW) && (ELT4currentMillis - ELT4previousMillis >= ELTOffTime))
{
ELT4ledState = HIGH; // turn it on
ELT4previousMillis = ELT4currentMillis; // Remember the time
digitalWrite(ELT4Pin, ELT4ledState); // Update the actual LED
}
// End of valve 4 managment
// To add or subract valves, copy the previous Valve Managment statement and replace ELT value with the correct Valve Number,
// after adding and declaring all the right values to any previous line, declaration, setup and loop.
}
// end of automatic()
a questo sketch se aggiungo la parte seguente nel loop, si modificano completamente tutti i tempi di HIGH e LOW che altrimenti sarebbero corretti e funzionanti:
if(Serial.available())
{
sel=Serial.read();
}
switch(sel) {
case '1' : [
automatic();
break;
}
ovviamente la variabile sel è stata dichiarata precedentemente.
Spero che tu riesca ad aiutarmi,
ti ringrazio per il tempo che mi stai dedicando
Ringrazio tutti per l'aiuto,
ho comunque risolto, il problema era dato dal non aver dichiarato le variabili current millis a zero nelle dichiarazione iniziali.
Senza la dichiarazione a zero, la variabile veniva creata al momento in cui la parte di codice veniva eseguita e questo creava i millisecondi di ritardo che modificavano i valori dei ritardi, in modo completamente casuale, questo mi ha fatto pensare a differenze di calcolo che potevano essere date dal problema che ho risolto su indicato.
Grazie a tutti
Se potesse essere utile a qualcuno, di seguito lo sketch riguardante l'apertura e chiusura sequenziale di elettrovalvole o qualsiasi carico da gestire con relè e ritardo con millis.
Nelllo sketch è integrata la prima parte di uno switch case, a me utile per la gestione di alcuni parametri da remoto, ovviamente questa parte è da terminare.
// Hydroponic Valve timing management
const int ELT1Pin = 7; // Pin connect to use valve 1
const int ELT2Pin = 9; // Pin connect to use valve 2
const int ELT3Pin = 8; // Pin connect to use valve 3
const int ELT4Pin = 13; // Pin connect to use valve 4
int ELT1ledState = LOW; // Variable for valve 1 used to check previous valve state
int ELT2ledState = LOW; // Variable for valve 2 used to check previous valve state
int ELT3ledState = LOW; // Variable for valve 3 used to check previous valve state
int ELT4ledState = LOW; // Variable for valve 4 used to check previous valve state
unsigned long ELT1previousMillis = 0; // will store last time ELT1 was updated
unsigned long ELT2previousMillis = 0; // will store last time ELT2 was updated
unsigned long ELT3previousMillis = 0; // will store last time ELT3 was updated
unsigned long ELT4previousMillis = 0; // will store last time ELT4 was updated
unsigned long ELT1currentMillis = 0; // Variable used to store current time for valve 1
unsigned long ELT2currentMillis = 0; // Variable used to store current time for valve 2
unsigned long ELT3currentMillis = 0; // Variable used to store current time for valve 2
unsigned long ELT4currentMillis = 0; // Variable used to store current time for valve 2
long ELTOnTime = 2000; // Time Valve on
long ELTOffTime = 3 * ELTOnTime; // Don't modify, this value is chained with on time.
char sel = '0'; // variabile utilizzata per conservare lo stato del menu' seriale
void setup()
{
Serial.begin(9600);
// set the digital pin as output:
pinMode(ELT1Pin, OUTPUT);
pinMode(ELT2Pin, OUTPUT);
pinMode(ELT3Pin, OUTPUT);
pinMode(ELT4Pin, OUTPUT);
}
void loop(){
if(Serial.available())
{
sel=Serial.read();
}
switch(sel) {
case '1' : {
automatic();
break;
}
}
} // End of the Loop
void automatic() {
// check to see if it's time to change the state of the VALVE
unsigned long ELT1currentMillis = millis() + ELTOnTime * 3 ; // Value to modify to add or subtract valves (example : 5 valves *4, *3, *2)
unsigned long ELT2currentMillis = millis() + ELTOnTime * 2 ; // (example : 3 valves *2, *1)
unsigned long ELT3currentMillis = millis() + ELTOnTime;// (example : 2 valves *1)
unsigned long ELT4currentMillis = millis() ; // The last valve value MUST BE EMPTY
// Valve 1 Management----------------------------------------------------------------------
if((ELT1ledState == HIGH) && (ELT1currentMillis - ELT1previousMillis >= ELTOnTime))
{
ELT1ledState = LOW; // Turn it off
ELT1previousMillis = ELT1currentMillis; // Remember the time
digitalWrite(ELT1Pin, ELT1ledState); // Update the actual LED
}
else if ((ELT1ledState == LOW) && (ELT1currentMillis - ELT1previousMillis >= ELTOffTime))
{
ELT1ledState = HIGH; // turn it on
ELT1previousMillis = ELT1currentMillis; // Remember the time
digitalWrite(ELT1Pin, ELT1ledState); // Update the actual LED
} // End of valve 1 managment
// Valve 2 Management----------------------------------------------------------------------
if((ELT2ledState == HIGH) && (ELT2currentMillis - ELT2previousMillis >= ELTOnTime))
{
ELT2ledState = LOW; // Turn it off
ELT2previousMillis = ELT2currentMillis; // Remember the time
digitalWrite(ELT2Pin, ELT2ledState); // Update the actual Valve
}
else if ((ELT2ledState == LOW) && (ELT2currentMillis - ELT2previousMillis >= ELTOffTime))
{
ELT2ledState = HIGH; // turn it on
ELT2previousMillis = ELT2currentMillis; // Remember the time
digitalWrite(ELT2Pin, ELT2ledState); // Update the actual Valve
}
// End of valve 2 managment
// Valve 3 Management----------------------------------------------------------------------
if((ELT3ledState == HIGH) && (ELT3currentMillis - ELT3previousMillis >= ELTOnTime))
{
ELT3ledState = LOW; // Turn it off
ELT3previousMillis = ELT3currentMillis; // Remember the time
digitalWrite(ELT3Pin, ELT3ledState); // Update the actual Valve
}
else if ((ELT3ledState == LOW) && (ELT3currentMillis - ELT3previousMillis >= ELTOffTime))
{
ELT3ledState = HIGH; // turn it on
ELT3previousMillis = ELT3currentMillis; // Remember the time
digitalWrite(ELT3Pin, ELT3ledState); // Update the actual Valve
}
// End of valve 3 managment
// Valve 4 Management----------------------------------------------------------------------
if((ELT4ledState == HIGH) && (ELT4currentMillis - ELT4previousMillis >= ELTOnTime))
{
ELT4ledState = LOW; // Turn it off
ELT4previousMillis = ELT4currentMillis; // Remember the time
digitalWrite(ELT4Pin, ELT4ledState); // Update the actual LED
}
else if ((ELT4ledState == LOW) && (ELT4currentMillis - ELT4previousMillis >= ELTOffTime))
{
ELT4ledState = HIGH; // turn it on
ELT4previousMillis = ELT4currentMillis; // Remember the time
digitalWrite(ELT4Pin, ELT4ledState); // Update the actual LED
}
// End of valve 4 managment
// To add or subract valves, copy the previous Valve Managment statement and replace ELT value with the correct Valve Number,
// after adding and declaring all the right values to any previous line, declaration, setup and loop.
}
// end of automatic()