Door programming problem in automatic operation

I took care to translate it for you

/motor + relay + LCD + cell + sensors 

// LCD Keypad Shield

#include <LiquidCrystal.h>

// Create the lcd object (with the different digital ports it uses) 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//variables
int lcd_key     = 0;
int adc_key_in  = 0;
int TensionBAS  = 0; //VoltageDOWN
int TensionHAUT = 0; //VoltageUP

//constantes
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

#define RelayPin1 2 //digital port D2 is connected to relay 1 
#define RelayPin2 3 //digital port D3 is connected to relay 2 
#define CapteurBAS 11 // the digital port D11 is connected to the DOWN sensor 
#define CapteurHAUT 12 // the digital port D12 is connected to the UP sensor 

#define debugTxtVar(myParameterText, variableName) \
        Serial.print(#myParameterText " " #variableName"="); \
        Serial.println(variableName); 

int Etat_fonctionnement_manuel = 0; //manual operation state
int Etat_fermeture_auto = 0; //auto closing state
int Etat_ouverture_auto = 0; //auto opening state

unsigned long temps_mesures = 0; //measures  time
unsigned long temps_fonctionnement_manuel = 0; //manual operation time
unsigned long temps_fermeture_auto = 0; //auto closing time
unsigned long temps_ouverture_auto = 0; //auto opening time
unsigned long temps_actuel; //current time

boolean JourVrai;   // ItIsDAY
boolean Test_manuel_demarrage = false; //Starting manual test

const byte manuel_Machine_inactive0 = 0; //manual_stateMachineIdle0
const byte manuel_debut_fermeture_porte1 = 1; //manual_startClosingDoor1
const byte manuel_Attendre_CapteurBAS_ferme2 = 2; //manual_waitForSensorDownClosed2
const byte manuel_Ouvrir_porte_apresTempo3 = 3; //manual_openDoorAfterWaiting3
const byte manuel_Attendre_CapteurHAUT_ferme4 = 4; //manual_waitForSensorDoorUp4
const byte manuel_arret_machine_apresTempo5 = 5; //manual_waitAndStopStateMachine5

const byte autoFermeture_attendre_tempoSoir1 = 1;//autoclose_wait_EveningDelay1
const byte autoFermeture_debut_fermeture2 = 2; //autoclose_start_closing2
const byte autoFermeture_CapteurBAS_ferme3 = 3; //autoclose_DOWNsensor_closed3
const byte autoFermeture_arret_machine4 = 4; //autoclose_stop_machine4
const byte autoFermeture_machine_inactive0 = 0; //autoclose_MachineIdle0

const byte autoOuverture_attendre_tempoMatin1 = 1;//autoOpen_wait_MorningDelay1
const byte autoOuverture_debut_ouverture2 = 2; //autoOpen_start_opening2
const byte autoOuverture_CapteurHAUT_ferme3 = 3; //autoOpen_UPsensor_closed3
const byte autoOuverture_arret_machine4 = 4; //autoOpen_stop_machine4
const byte autoOuverture_machine_inactive0 = 0; //autoOpen_MachineIdle0


void setup() {
  // We put the pins of each relay at the output 
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);

  pinMode(CapteurBAS, INPUT);
  pinMode(CapteurHAUT, INPUT);

  Serial.begin(9600);

  lcd.begin(16, 2);            // Screen startup 
  lcd.setCursor(0, 0);         // Positioning the cursor at the start 
  lcd.print("Mesure tension"); // Message
}


void loop() {
  task_mesure_tensions();
  AffichageLCD();

  lcd_key = read_LCD_buttons();  // Buttons reading 
  // Outsourcing the various tasks 
  switch (lcd_key)               // Action when button is pressed
  {
    case btnRIGHT: //by pressing the RIGHT button 
      {
        if (!Test_manuel_demarrage) { // if the manual test has not already started 
          Test_manuel_demarrage = true;
          Etat_fonctionnement_manuel = manuel_debut_fermeture_porte1;
        }
        break;
      }
  }

  if (Test_manuel_demarrage) {
    task_fonctionnement_manuel();
  }

  // if the solar cell detects the day and the sensor does not detect the open door 
  if (JourVrai && digitalRead(CapteurHAUT) == HIGH ) {
    task_ouverture_auto(); // open door
  }

  // if the solar cell detects at night and the sensor does not detect the closed door 
  if (!JourVrai && digitalRead(CapteurBAS) == HIGH ) {
    task_fermeture_auto(); // close door
  }
} // end of loop

void AffichageLCD() { //LCD_Display

    temps_actuel = millis();
 
    // Display the voltage measurement on the LCD screen and wait for 1000 ms 
    lcd.setCursor(0, 1);           // Positioning of the start of line cursor 

    if( (temps_actuel - temps_mesures) >= 1000ul )
    {
        temps_mesures = temps_actuel;

        // lcd.print("L");

        if (JourVrai) 
        {
          lcd.print("jour"); //displays the value of the state of the solar cell 
        }
        else if (!JourVrai) 
        {
          lcd.print("nuit"); //displays the value of the state of the solar cell 
        }
      
        lcd.print(" F");
        lcd.print(Etat_fermeture_auto); //displays the value of the automatic closing status 
      
        lcd.print(" O");
        lcd.print(Etat_ouverture_auto); //displays the value of the automatic opening state 
      
        lcd.print(" M");
        lcd.print(Etat_fonctionnement_manuel); //displays the value of the manual mode of the door 

         debugTxtVar("F=  ", Etat_fermeture_auto);
         //Serial.println(Etat_fermeture_auto);
      
         debugTxtVar("O= ", Etat_ouverture_auto);
         //Serial.println(Etat_ouverture_auto);
      
         //debugTxtVar("M= ", Etat_fonctionnement_manuel);
         //Serial.println(Etat_fonctionnement_manuel);
    }
} // AffichageLCD

void task_mesure_tensions() { //task_Voltage_measures
  //allows you to take voltage measurements and display data on the LCD screen 
  // Transforms the measurement (integer) into voltage via a cross product 
  int CelluleSolaireCAN = analogRead(A1); //solarCellADC
  // Measures the voltage across the solar cell 
  int MoteurCAN     = analogRead(A2);  //motorADC 
  // Measures the voltage consumed by the motor 

  float TensionCelluleSolaire = (float)CelluleSolaireCAN * (5.0 / 1023.0);
  // float solarCellVoltage = (float)solarCellADC * (5.0 / 1023.0);
  float TensionMoteur    = (float)MoteurCAN     * (5.0 / 1023.0);
  // float motorVoltage     = (float)motorADC     * (5.0 / 1023.0);


  if (TensionCelluleSolaire < 1.0)  //if (solarCellVoltage < 1.0) 
  { // low voltage means it is starting to get dark 
    if (JourVrai) { //if it is based on a day-night change of state 
      JourVrai = false; //the variable changes to false when it is dark 
      // start the state machine to automatically close the door 
      Etat_fermeture_auto = autoFermeture_attendre_tempoSoir1;
      Etat_ouverture_auto = autoOuverture_machine_inactive0;
    }
  }
  
  else if (TensionCelluleSolaire >= 1.0) //else if (solarCellVoltage >= 1.0)
  { // high voltage means the sun is rising 
      if (!JourVrai) { // if it is based on a night-day change of state 
        JourVrai = true;  //the variable turns to true when it is daylight 
        // start the state machine to automatically open the door 
        Etat_ouverture_auto = autoOuverture_attendre_tempoMatin1;
        Etat_fermeture_auto = autoFermeture_machine_inactive0;
      }
  }  
}

void FermerMoteur() { //RunMotorClosing
  digitalWrite(RelayPin1, LOW); //relay 1 is active 
  digitalWrite(RelayPin2, HIGH); //relay 2 is inactive 
}

void ArretMoteur() {//MotorStop
  digitalWrite(RelayPin1, HIGH); //relay 1 is inactive 
  digitalWrite(RelayPin2, HIGH); //relay 2 is inactive 
}

void OuvrirMoteur() {//RunMotorOpening
  digitalWrite(RelayPin1, HIGH); //relay 1 is inactive 
  digitalWrite(RelayPin2, LOW); //relay 2 is active 
}


void task_fonctionnement_manuel() { //allows you to activate the manual function test of the door 
  // if values of a variable shall be re-used the variable must be assigned
  // global or with attribute static to keep the value even after leaving the function
  temps_actuel = millis();
  debugTxtVar("task_fonctionnement_manuel ", Etat_fonctionnement_manuel);
  //delay(1000); // slow down serial output

  switch ( Etat_fonctionnement_manuel )
  {
    case manuel_debut_fermeture_porte1 :
      // After pressing the button, the motor turns in one direction and the door begins to close ... 
      FermerMoteur();
      temps_fonctionnement_manuel = temps_actuel;//we reset the time spent to 0 ms 
      Etat_fonctionnement_manuel = manuel_Attendre_CapteurBAS_ferme2; // we increment the variable by 1 
      break;

    case manuel_Attendre_CapteurBAS_ferme2 :
      if ( digitalRead(CapteurBAS) == LOW  ) //when the DOWN sensor contact is closed ... 
      { //... the motor pauses and the door stops for 3 seconds ... 
        ArretMoteur();
        temps_fonctionnement_manuel = temps_actuel;//we reset the time spent to 0 ms 
        Etat_fonctionnement_manuel = manuel_Ouvrir_porte_apresTempo3; // we increment the variable by 1 
      }
      break;

    case manuel_Ouvrir_porte_apresTempo3 :
      if ( (temps_actuel - temps_fonctionnement_manuel) >= 3000ul )
      { //once the break is over, the motor turns in the other direction and the door begins to open ... 
        OuvrirMoteur();
        temps_fonctionnement_manuel = temps_actuel;//we reset the time spent to 0 ms
        Etat_fonctionnement_manuel = manuel_Attendre_CapteurHAUT_ferme4; // we increment the variable by 1 
      }
      break;

    case manuel_Attendre_CapteurHAUT_ferme4 :
      if ( digitalRead(CapteurHAUT) == LOW  ) //when the UP sensor contact is closed ... 
      { // ...the motor pauses and the door stops for 3 seconds ... 
        ArretMoteur();
        temps_fonctionnement_manuel = temps_actuel;//we reset the time spent to 0 ms
        Etat_fonctionnement_manuel = manuel_arret_machine_apresTempo5; // we increment the variable by 1 
      }
      break;

    case manuel_arret_machine_apresTempo5 :
      if ( (temps_actuel - temps_fonctionnement_manuel) >= 3000ul )
      { //return to the initial state after a 3 second pause 
        Etat_fonctionnement_manuel = manuel_Machine_inactive0; // the variable goes back to 0 
      }
      break;

    case manuel_Machine_inactive0 :
      Test_manuel_demarrage = false; // manual loop finished 
      break;
  }//switch

}//task_fonctionnement_manuel



void task_fermeture_auto () { //task_autoClosing
  //allows you to activate the automatic closing of the door with the brightness threshold 
  // if values of a variable shall be re-used the variable must be assigned
  // global or with attribute static to keep the value even after leaving the function
  temps_actuel = millis();

  switch ( Etat_fermeture_auto )
  {
    case autoFermeture_attendre_tempoSoir1 :
      if ( (temps_actuel - temps_fermeture_auto) >= 10000ul)
      { //until the 10 seconds have elapsed ... 
        Etat_fermeture_auto = autoFermeture_debut_fermeture2; //we increment the variable by 1 
      }
      break;

    case autoFermeture_debut_fermeture2 :
      FermerMoteur();
      // the door starts to close 
      temps_fermeture_auto = temps_actuel; //we reset the time spent to 0 ms
      Etat_fermeture_auto = autoFermeture_CapteurBAS_ferme3; //we increment the variable by 1 
      break;

     case autoFermeture_CapteurBAS_ferme3 :
      if ( digitalRead(CapteurBAS) == LOW  ) //when the DOWN sensor contact is closed ... 
      { // the motor pauses and the door stops. 
        ArretMoteur();
        temps_fermeture_auto = temps_actuel; //we reset the time spent to 0 ms
        Etat_fermeture_auto = autoFermeture_arret_machine4; //we increment the variable by 1 
      }
      break;  

    case autoFermeture_arret_machine4 :
      if ( (temps_actuel - temps_fermeture_auto) >= 3000ul )
      { //leaves the door closed and resets the variable to its initial state. 
        Etat_fermeture_auto = autoFermeture_machine_inactive0; // the variable goes back to 0 
      }
      break;

    case autoFermeture_machine_inactive0:
      // nothing is happening
      break;
  } //switch

} //task_fermeture_auto



void task_ouverture_auto () { //task_autoOpening
  //allows you to activate the automatic opening of the door with the brightness threshold 
  // if values of a variable shall be re-used the variable must be assigned
  // global or with attribute static to keep the value even after leaving the function
  temps_actuel = millis();

  switch ( Etat_ouverture_auto )
  {
    case autoOuverture_attendre_tempoMatin1 :
      if ( (temps_actuel - temps_fermeture_auto) >= 10000ul)
      { //until the 10 seconds have elapsed ... 
        Etat_ouverture_auto = autoOuverture_debut_ouverture2; //we increment the variable by 1 
      }
      break;

    case autoOuverture_debut_ouverture2 :
      OuvrirMoteur();
      // the door starts to open 
      temps_ouverture_auto = temps_actuel; //we reset the time spent to 0 ms
      Etat_ouverture_auto = autoOuverture_CapteurHAUT_ferme3; //we increment the variable by 1 
      
      break;

    case autoOuverture_CapteurHAUT_ferme3 :
      if ( digitalRead(CapteurHAUT) == LOW  ) //when the UP sensor contact is closed ... 
      { // the motor pauses and the door stops. 
        ArretMoteur();
        temps_ouverture_auto = temps_actuel; //we reset the time spent to 0 ms
        Etat_ouverture_auto = autoOuverture_arret_machine4; //we increment the variable by 1 
      }
      break;  

    case autoOuverture_arret_machine4 :
      if ( (temps_actuel - temps_ouverture_auto) >= 3000ul )
      { //leaves the door open and resets the variable to its initial state. 
        Etat_ouverture_auto = autoOuverture_machine_inactive0; //the variable goes back to 0 
      }
      break;

    case autoOuverture_machine_inactive0 :
      // nothing is happening 
      break;
  } //switch

} //task_ouverture_auto





// Button reading function 
int read_LCD_buttons() { //allows you to control the buttons on the LCD display 

  adc_key_in = analogRead(0);   // Reading the analog port 

  // The following values must be adapted to the shield
  if (adc_key_in > 1000) return btnNONE;   // In principle 1023 when no key is pressed 
  if (adc_key_in < 50)   return btnRIGHT;     // 0
  if (adc_key_in < 195)  return btnUP;        // 99
  if (adc_key_in < 380)  return btnDOWN;      // 255
  if (adc_key_in < 555)  return btnLEFT;      // 409
  if (adc_key_in < 790)  return btnSELECT;    // 640

  return btnNONE;
}

However, I tried to add debugoutput at the top of each state-machine like in the example, but it doesn't work. Unless I coded incorrectly ?

I also moved all the current_time variables linked to the millis () function with the global variables. I declared the variable with "unsigned long" instead of "static unsigned long".

But when I upload, I don't really see the difference.

Finally, I hope you will have no trouble reading this commented code in English.

Your posting is way too short!!
Huh! why that? From this short posting I'm very unsecure if you have understood what the meaning of the serial monitor is and how to use it.

  • You wrote nothing about what the serial monitor shows
  • you wrote nothing about your inerpretation what the output in the serial monitor might mean

This leads me to the conclusion that you have NOT looked at the output of the serial monitor at all.

You seem to have an attitude of "Stefan will do it all for me"
NO I will NOT!

You might think hey why is this guy trying to give me orders in such a strong way?

You are free to decide to react on my posts in any way.
But indeed if you want my support I rise a condition:
Post in DETAIL what you have observed in the serial monitor and what your guessing is why this is shown in the serial monitor.

I have great doubts that you even opened the serial monitor.
Post a screenshot of the serial monitor and then write YOUR interpretation of what the output means.

WRITE a SUGGESTION what you have to change in your code to make it react in the way you want it.

What you are doing until now is just following my instructions
add a line of code here
move this line of code there

and then do a new test just looking at "does it behave like I want?" reporting "No it doesn't" give new instructions.

YOU have to ANALYSE your code YOURSELF and report your GUESSINGS.
Just guessings and suggestions on how to modify it.
This process will make you LEARN instead of just executing a "command" not thinking about what it does and forget it it as soon as the modification is done.

If I remember right you run out of due next week.
It is up to you if you speed up the developing-process of your code through detailed answers
or
to slow it down in a ping-pong-game of short answers and repeated asking back for more details.

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.