two buttons and three relays

Hello
I try to do an automation, but I have difficulties and I turn to you for help. I have two buttons for UP and DOWN, when you press and hold the button UP, the relay for operation is activated and works while the button is held. When you press and hold the DOWN button, the UP relay must be activated for one second and stopped, then the COMPR relay must be activated for one second and stopped, and then the DOWN relay must be activated and remain so until release the button.

I use a translator and I don't know how correct the translation is, for which I apologize.

this is my code

const int BUTTON_UP = 8;
const int BUTTON_DOWN = 9;
const int RELAY_UP = 2;
const int RELAY_DOWN = 3;
const int RELAY_COMPR = 4;
int BUTTONstate_UP = 0;
int BUTTONstate_DOWN = 0;

void setup() {
  pinMode(BUTTON_UP, INPUT);
  pinMode(BUTTON_DOWN, INPUT);
  pinMode(RELAY_UP, OUTPUT);
  pinMode(RELAY_DOWN, OUTPUT);
  pinMode(RELAY_COMPR, OUTPUT);

}

void loop() {
  BUTTONstate_UP = digitalRead(BUTTON_UP);
  if (BUTTONstate_UP == HIGH)
  {
    digitalWrite(RELAY_UP, HIGH);
  } 
  else{
    digitalWrite(RELAY_UP, LOW);
  }
  
  BUTTONstate_DOWN = digitalRead(BUTTON_DOWN);
  if (BUTTONstate_DOWN == HIGH)
  {
    digitalWrite(RELAY_UP, HIGH);
    delay(1000);
    digitalWrite(RELAY_UP, LOW);
    
  }
  else{
    digitalWrite(RELAY_UP, LOW);
    
  }
  
  BUTTONstate_DOWN = digitalRead(BUTTON_DOWN);
  if (BUTTONstate_DOWN == HIGH)
  {
    digitalWrite(RELAY_COMPR, HIGH);
    delay(1000);
    digitalWrite(RELAY_COMPR, LOW);
  } 
  else{
    
    digitalWrite(RELAY_COMPR, LOW);
  }
  
  BUTTONstate_DOWN = digitalRead(BUTTON_DOWN);
  if (BUTTONstate_DOWN == HIGH)
  {
    digitalWrite(RELAY_DOWN, HIGH);
  }
  else{
    digitalWrite(RELAY_DOWN, LOW);
  }
  
}

What is the the problem?

The problem is the relay goes UP in cyclic mode for one second and does not turn off. It should work for one second and stop.

Remember that your code loops. So I suspect you will have competing statements between your buttonup and buttondown if statements. You are also using delay which will hang up your code for that time. Look at the tutorials on millis, buttons, state machines, doing several things at once. Annotate your code with // to change to plain language what it is doing so that you can more easily understand. Read your code like the machine where you go through and then back through it thinking what happens with each input remembering that the controller loops many times a second (without delays)

How are your buttons wired. I normally use the internal pullups and use low as the activated state of a button when it connects pin to ground

Follow your code for a pressed up button:

  BUTTONstate_UP = digitalRead(BUTTON_UP);
  if (BUTTONstate_UP == HIGH)  WHEN YOU PRESS UP BUTTON
  {
    digitalWrite(RELAY_UP, HIGH); /TURN ON RELAY UP
  }
  else{
    digitalWrite(RELAY_UP, LOW);
  }
 
  BUTTONstate_DOWN = digitalRead(BUTTON_DOWN);
  if (BUTTONstate_DOWN == HIGH)
  {
    digitalWrite(RELAY_UP, HIGH);
    delay(1000);                               
    digitalWrite(RELAY_UP, LOW);
   
  }
  else{
    digitalWrite(RELAY_UP, LOW); //TURN IT LOW AGAIN
   
  }

For a pressed up button only I read your code as:

turn on relay UP
turn off relay up (because buttondown is low)
turn off relay comp
turn off relay down

This should all complete in a few microseconds and loop

For as long as the button down is pressed
turn off relay up
turn on relay up
wait 1s
turn off relay up
turn on relay comp
wait 1s
turn off relay comp
turn on relay down
then loop but your finger needs to be pressed the whole time and there is no button debouncing

Maybe I can't explain what needs to happen. I will try to describe it step by step.

  1. Pressing and holding the button UP and while pressing the relay UP works until it is released.
    Pressing and holding ButtonUP --> Relay UP ON while the button is pressed
    Relay COMPR OFF
    Relay DOWN OFF
    release the button UP --> all relays OFF
    END

2.Pressing and holding the button DOWN --> Relay UP ON one second and OFF
Relay COMPR ON
Delay 1 second
Relay DOWN ON
release the button DOWN --> all relays OFF
END

Now there is a cycle in which the relay UP constantly goes from on to off for a second. And I do not know where I'm wrong. He is a beginner, so I turn to you for help.

Hi, I understand what you are trying to achieve. I am telling you what it appears your code is currently doing. If you understand that then it is not too much to start getting it to do what you want.

This is how to re-write your sketch as a "State Machine". The sketch can be in several different states:

IDLE: Waiting for one of the two buttons to be pressed.

GOING_UP: The UP button was pressed and the UP relay was turned on. Waiting for the UP button to be released.

GOING_DOWN_PART_1: The DOWN button was pressed. A timer was started and the UP relay was turned on. Waiting for the timer to reach 1000 or the DOWN button to be released.

GOING_DOWN_PART_2: The first timer reached 1000. The UP relay was turned off, the COMPR relay was turned on, and the timer was re-started. Waiting for the timer to reach 1000 again or for the DOWN button to be released.

GOING_DOWN_PART_3: The second timer reached 1000. The COMPR relay was turned off and the DOWN relay was turned on. Waiting the DOWN button to be released.

In each state, only look for inputs/timeouts that would move the sketch to a different state.

const int BUTTON_UP = 8;
const int BUTTON_DOWN = 9;
const int RELAY_UP = 2;
const int RELAY_DOWN = 3;
const int RELAY_COMPR = 4;
int BUTTONstate_UP = 0;
int BUTTONstate_DOWN = 0;


enum States {IDLE, GOING_UP, GOING_DOWN_PART_1, GOING_DOWN_PART_2, GOING_DOWN_PART_3} State = IDLE;


void setup()
{
  pinMode(BUTTON_UP, INPUT);
  pinMode(BUTTON_DOWN, INPUT);
  pinMode(RELAY_UP, OUTPUT);
  pinMode(RELAY_DOWN, OUTPUT);
  pinMode(RELAY_COMPR, OUTPUT);
}


void loop()
{
  unsigned long currentTime = millis();
  static unsigned long stateStartTime = 0;


  BUTTONstate_UP = digitalRead(BUTTON_UP);
  BUTTONstate_DOWN = digitalRead(BUTTON_DOWN);


  switch (State)
  {
    case IDLE:
      if (BUTTONstate_UP == HIGH)
      {
        digitalWrite(RELAY_UP, HIGH);
        State = GOING_UP;
      }
      else if (BUTTONstate_DOWN == HIGH)
      {
        stateStartTime = currentTime;
        digitalWrite(RELAY_UP, HIGH);
        State = GOING_DOWN_PART_1;
      }
      break;


    case GOING_UP:
      if (BUTTONstate_UP == LOW)
      {
        digitalWrite(RELAY_UP, LOW);  // Button was released
        State = IDLE;
      }
      break;


    case GOING_DOWN_PART_1:
      if (currentTime - stateStartTime >= 1000)
      {
        digitalWrite(RELAY_UP, LOW);  // Stop going up
        stateStartTime = currentTime;
        digitalWrite(RELAY_COMPR, HIGH);
        State = GOING_DOWN_PART_2;
      }
      else if (BUTTONstate_DOWN == LOW)
      {
        digitalWrite(RELAY_UP, LOW);  // Stop going up
        State = IDLE;
      }
      break;


    case GOING_DOWN_PART_2:
      if (currentTime - stateStartTime >= 1000)
      {
        digitalWrite(RELAY_COMPR, LOW);  // Turn off COMPR
        stateStartTime = currentTime;
        digitalWrite(RELAY_DOWN, HIGH);
        State = GOING_DOWN_PART_3;
      }
      else if (BUTTONstate_DOWN == LOW)
      {
        digitalWrite(RELAY_COMPR, LOW);  // Turn off COMPR
        State = IDLE;
      }
      break;


    case GOING_DOWN_PART_3:
      if (BUTTONstate_DOWN == LOW)
      {
        digitalWrite(RELAY_DOWN, LOW);  // Turn off COMPR
        State = IDLE;
      }
      break;
  }
}