Millis problem, i cannot use it, i would have some help guys.

Hi guys,
i am trying to make a little and simple program for a greenhouse to plant my orchid.
I tryied many times with millis but i am just going crazy! :frowning:
i already setted up separetly all the relays and sensors, everything is working fine but if i can not put all toghether with millis sequence everything would be unuseful :frowning:
Please i need your help, maybe or for sure foor you is simple as drinking water but for me is like a mountain. :confused: this is my complete scheme without millis,
can someone help me to put it together and working?

//COMANDI RELATIVI ALL'ACCENDIMENTO E SPEGNIMENTO RELE SERPENTINA (ACCENDI SERPENTINA SE TEMP INFERIORE A 30 GRADI, SPEGNI SE SUPERA I 35
const int VENTOLA =9; //the "s" of relay module attach to    CONTROLLO ACCENDI SPEGNI VENTOLA
const int LUCE =7; //the "s" of relay module attach to       CONTROLLA ACCENDI SPEGNI LUCE
const int POMPA =8; //the "s" of relay module attach to      CONTROLLA ACCENDI SPEGNI POMPA
const int LM35 = 0;       // SENSORE LM35 ATTACCATO SU PIN A0 PER CONTROLLARE LA TEMPERATURA
float celsius = 0;      // temperature variables
float millivolts;             //dichiarazione di variabile tensione (float è per i numeri con la virgola)
int sensor;
const int SERPENTINA = 6;      //RELE 6 CONTROLLA LA SERPENTINA
const int LEDSERPENTINA = 10; //LED CONTROLLO ACCENSIONE SERPENTINA PIN 10 
void setup()
{
Serial.begin(9600);                   // inizializza la comunicazione seriale
// COMUNICAZIONE CON RELE' E ISTRUZIONI ACCENDI SPEGNI VENTOLE LUCI E ACQUA
pinMode(SERPENTINA, OUTPUT); //RELE CONTROLLO SERPENTINA ACCENDI E SPEGNI SECONDO TEMPERATURE LM35
pinMode(LEDSERPENTINA, OUTPUT); // LED CONTROLLO ACCENSIONE SERPENTINA
pinMode(LUCE, OUTPUT); // CONTROLLO ACCENDI SPEGNI LUCE
pinMode(POMPA, OUTPUT); //CONTROLLO ACCENDI SPEGNI POMPA ACQUA
pinMode(VENTOLA, OUTPUT); //CONTROLLO ACCENDI SPEGNI VENTOLA

}
void loop()
{
  digitalWrite(VENTOLA, HIGH); //ACCENDI LA VENTOLA
  delay(60000); //TIENILA ACCESA PER 60000 EQUIVALENTE A UN MINUTO
  digitalWrite(VENTOLA, LOW); //SPEGNI LA VENTOLA
  delay(3540000); //SPEGNI LA VENTOLA PER 3540000 EQUIVALENTE A 59 MINUTI
  digitalWrite(LUCE, HIGH); //ACCENDI LA LUCE 
  delay(64800000); //TIENILA ACCESA PER 64800000 EQUIVALENTE A 18 ORE
  digitalWrite(LUCE, LOW); //SPEGNI LA LUCE
  delay(21600000); //SPEGNI LA LUCE PER 21600000 EQUIVALENTE A 6 ORE
  digitalWrite(POMPA, HIGH); //ACCENDI LA POMPA ACQUA
  delay(600000); //TIENILA ACCESA PER 600000 EQUIVALENTE A 10 MINUTI
  digitalWrite(POMPA, LOW); //SPEGNI LA POMPA ACQUA
  delay(1200000); //SPEGNI LA VENTOLA PER 1200000 EQUIVALENTE A 20 MINUTI
sensor = analogRead(LM35);                     //lettura valore del sensore LM35 messo sull'ingresso analogico A0
millivolts = ( sensor/1023.0)*5000;                           //formula per ottenere la tensione di uscita dell'LM35 in millivolts
celsius =millivolts/10;                             // valore espresso in gradi Celsius (l'out del sensore è 10mv per grado)
Serial.print(celsius);Serial.println(" GRADI");                // stampa su serial monitor del valore di temperatura in gradi Celsius
// accendi il RELE
if (analogRead(LM35)<62) {            // 41 corrisponde a 20°C - 62 SONO CIRCA 30 GRADI -       QUINDI SE LA TEMPERATURA SCENDE SOTTO I 30 ° ACCENDI IL RELE' SERPENTINA
digitalWrite(SERPENTINA , HIGH);
analogWrite(LEDSERPENTINA, 255); }
if (analogRead(LM35)>72) {           // 41 corrisponde a 20°C - 72 SONO CIRCA 35 GRADI         QUINDI SE LA TEMPERATURA OLTREPASSA   I 35 ° SPEGNI IL RELE' SERPENTINA
digitalWrite(SERPENTINA , LOW);
analogWrite(LEDSERPENTINA, 0);}                                         // SE LA TEMPERATURA OLTREPASSA   I 35 SPEGNI IL LED POSIZIONATO IN PIN 10
delay(1000);
}

thanks for your help!
Diego(Milan)

Show us some of your sketch attempts using millis() used to get rid of delay().
.

It looks like you would be better off using an RTC rather than millis() or delay() in view of the long wait periods involved.

However, if you want to use millis() I suggest that you break the program down into a set of states and use switch/case to execute the code for the current state. Most of the time in each state will be spent waiting until millis() - stateStartTime >= state period

Before you enter each state set stateStartTime equal to millis() then use switch/case like this

switch (state)
{
  case 0:
    if (millis() - stateStartTime >= 60000UL)
    {
       digitalWrite(VENTOLA, LOW); //SPEGNI LA VENTOLA
       state = 1;
       stateStartTime = millis();
    }
    break;

  case 1:
    if (millis() - stateStartTime >= 3540000UL)
    {
       digitalWrite(LUCE, HIGH); //ACCENDI LA LUCE
       state = 2;
       stateStartTime = millis();
    }
    break;

etc etc etc


}

thank you so much for your help!
i will try it now, hoping... :slight_smile:
wish youa all a fantastic Christmas full of love!

i feel like a bottle in the ocean :frowning:
i want to ask sorry before posting my code but i am really confused now, don't laugh please :frowning:
i even cannot understand where i got to put strings now.
i tryied this:

long previousMills = 0;  //Memorizza l'ultima volta che il ciclo e' stato aggiornato
const int VENTOLA =9; //the "s" of relay module attach to    CONTROLLO ACCENDI SPEGNI VENTOLA
const int LUCE =7; //the "s" of relay module attach to       CONTROLLA ACCENDI SPEGNI LUCE
const int POMPA =8; //the "s" of relay module attach to      CONTROLLA ACCENDI SPEGNI POMPA
const int SERPENTINA = 6;      //RELE 6 CONTROLLA LA SERPENTINA
const int LEDSERPENTINA = 10; //LED CONTROLLO ACCENSIONE SERPENTINA PIN 10 
const int LM35 = 0;       // SENSORE LM35 ATTACCATO SU PIN A0 PER CONTROLLARE LA TEMPERATURA
float celsius = 0;      // temperature variables
float millivolts;             //dichiarazione di variabile tensione (float è per i numeri con la virgola)
int sensor;


void setup()
{
     Serial.begin(9600);                   // inizializza la comunicazione seriale
// COMUNICAZIONE CON RELE' E ISTRUZIONI ACCENDI SPEGNI VENTOLE LUCI E ACQUA
pinMode(SERPENTINA, OUTPUT); //RELE CONTROLLO SERPENTINA ACCENDI E SPEGNI SECONDO TEMPERATURE LM35
pinMode(LEDSERPENTINA, OUTPUT); // LED CONTROLLO ACCENSIONE SERPENTINA
pinMode(LUCE, OUTPUT); // CONTROLLO ACCENDI SPEGNI LUCE
pinMode(POMPA, OUTPUT); //CONTROLLO ACCENDI SPEGNI POMPA ACQUA
pinMode(VENTOLA, OUTPUT); //CONTROLLO ACCENDI SPEGNI VENTOLA

}

void loop()
{
switch (state)

  case 0:
    if (millis() - stateStartTime >= 60000)
    {
       digitalWrite(VENTOLA, LOW); //SPEGNI LA VENTOLA
       state = 1;
       stateStartTime = millis();
    }
    break;

  case 1:
    if (millis() - stateStartTime >= 3540000)
    {
       digitalWrite(LUCE, HIGH); //ACCENDI LA LUCE
       state = 2;
       stateStartTime = millis();
    }
    break;

    case 2:
    if (millis() - stateStartTime >= 600000)
    {
       digitalWrite(POMPA, HIGH); //ACCENDI LA POMPA
       state = 1;
       stateStartTime = millis();
    }
    break;

}

i'm feeling so stupid
sorry again

Have you looked at the demo Several Things at a Time which illustrates the use of millis() to manage timing ?

...R

not yet, as you can understand i am trying all the ways i will read it too!
thanks for your kind help too

The code in reply #4 needs some changes

At the very least you need a new global variable named state and initialised with a value of zero
You need to do
  digitalWrite(VENTOLA, HIGH); //ACCENDI LA VENTOLAin setup()

Add more cases, one each for the pump control stages that you were timing using delay()

You need to decide whether reading and reacting to the temperature is another state or whether it should happen each time through loop(). If it is another state then just add it in to the switch/case. If it should happen each time through loop() then add it after the switch/case.