HELP PLEASE URGENT

hi guys im developing a program to control 2 pneumatic struts, i have been having problems because i need the program to run the code in order, for example

i press two buttons (for security reasons)
then it starts two relays, one handles a timer and the other one starts the pneumatic struts to go down
when the timer finishes it sends a signal that stops the pneumatic struts
then it starts a second timer that turns on a relay that moves the strut up
then the timer finishes, it sends a signal and stops everything

the problem is that im doing ths with if commands, and for some reason they are not following an order, meaning that if the timer signal comes it activates the struts no matter that the buttons were not pressed

is there a way to make arduino to do the if commands in order??

is there a way to make arduino to do the if commands in order??

They are ALWAYS executed in the proper order. It is your code that is not correct. We can't see it!

Ok, you'll need to specify this a bit more clearly.

Do you need the buttons pressed in sequence? Do the buttons need to be held down? What happens if the buttons are released while this sequence is happening?

You say that one of the relays "handles a timer". I don't know what this means. There's an external timer of some sort? It is operated with a relay?

You say that the timer sends a signal. Are you saying that the external timer sends a pulse to a pin, or sends an input HIGH?

Or was this all misprint, and what you actually meant that the arduino would do timing internally?

the problem is that im doing ths with if commands, and for some reason they are not following an order, meaning that if the timer signal comes it activates the struts no matter that the buttons were not pressed

Oh, I see. You get some sort of periodic timing signal from something, and you want the arduino to go through a sequence if both buttons are held down at the time a signal is received.

So, you have two timer inputs, two button inputs, and two relay outputs (arm up/arm down).

I still don't understand what you mean by "one [relay] handles a timer".

ok here it is goes with more details....

before i begin let me say this, im not a programmer nor have a diploma on computer sciences, im an electrical enginner that likes to play with arduino to solve some issues at work, i THINK my issue is coming from the use of IF COMMANDS, every comment will be appreciated as long as you do it with the intend of helping, they help me grow on something i dont know much about

here is an example of a timer... the input voltage is 110v so im using a relay to turn it on, it has an output which is gingto arduino, that signal is 5v or 0v so im handling it as HIGH or LOW

the machine has two ways to be operated,

FIRST ONE..MANUAL
, i press one button, the gas spring will go down (no matter of timer, cause it is not being activated)

i press another button and the gas spring will go up (again no matter of timer)

SECOND WAY TO HANDLE IT ´AUTOMATIC´ (THIS IS THE ONE GIVING ME ISSUES)

1- i press two buttons at the same time, the relay #1 (this relay sends the voltage to the timer) once the timer turns on, it will start counting down (depending on how many seconds i set it, i can set up to 12seconds)... while it is counting down it will turn on the relay #3 (this relay activates a solenoid, this solenoid valve moves the spring down).

2- when the timer stops, it sends a 5v signal to arduino, when arduino gets this signal, it will turn off the relay #3, thenit will turn on relay #2 (this relay activates the second timer), while the second timer is counting down, it will activate the relay #4 (this activates the solenoid vavle to move the gas spring up).

3- when the second timer stops, it will send a 5v signal to arduino,when it gets the signal it will turn off all the relays or at least make sure they are off.

4- it needs to wait until i press the two buttons again to repeat the cycle

MY SPECIFIC QUESTION IS THIS:
WHEN I PRESS THE 2 BUTTONS THE RELAYS ARE ACTIVATED CORRECTLY, HOWEVER IF I PRESS THE BUTTONS AGAIN, IT WILL TURN ON THE RELAYS AGAIN (OBVIOUSLY CAUSE THE IF VALUE IS BEING MET)

NOW, HOW CAN I AVOID THAT FROM HAPPENING? IS THERE A WAY THAT IF I PRESS THE BUTTONS IT WILL START DOING THE PROGRAM AND NOT BEING INTERRUPTEDBY THE BUTTONS AGAIN?

NOTE: I LEARNED SOMEWHERE THAT I CAN USE A NOINTERRUPTS COMMAND SO NOTHING WILL STOP IT (NOT SURE HOW IT WORKS) HOWEVER THAT DOES NOT HELP ME, CAUSE IN THE FUTURE I HAVE TO WRITE A STOP COMMAND, SO IF THERE IS AN EMERGENCY, I CAN PRESS AN EMERGENCY BUTTON AND THE MACHINE NEEDS TO STOP AT ANY POINT OF THE PROGRAM
here is my code so far:

const int botonI = 13; // THESE FIRSTS TWO BUTTONS NEED TO BE PRESSED ON AUTOMATIC SECTION
const int botonD = 12;
const int botonLI = 11; //moves spring up
const int botonLD = 10; // moves spring down
const int Relay4 = 1; // ACTIVATES SOLENOID TO MOVE SPRING UP
const int Relay3 = 2; // ACTIVATES SOLENOID TO MOVE SPRING DOWN
const int Relay2 = 3; // ACTIVATES SECOND TIMER
const int Relay1 = 4; // ACTIVATES FIRST TIMER
const int Timer = 9; //THIS IS THE 5V SIGNAL FROM THE FIRST TIMER
const int Timer2 = 7; //THIS IS THE 5V SIGNAL FROM THE SECOND TIMER
const int Proceso = A0; //HERE I SELECT MANUAL OR AUTOMATIC

//FIN VARIABLE FISICAS*******************

int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int timerState = 0;
int timerState2 = 0;
int relay1 = 0;
int relay2 = 0;
int relay3 = 0;
int relay4 = 0;
int proceso = 0;

//FIN VARIABLES INTERNAS******************

void setup() {

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(botonD, INPUT);
pinMode(botonI, INPUT);
pinMode(botonLD, INPUT);
pinMode(botonLI, INPUT);
pinMode(Barrera, INPUT);
pinMode(Timer, INPUT);
pinMode(Timer2, INPUT);
pinMode(Proceso, INPUT);
}

//***********FIN SETUP

void loop(){
proceso = digitalRead(Proceso);

if (proceso == HIGH) {
AUTO();
}
else {
MANUAL();
}
STOP();
}

//*******************************************************************************

void MANUAL () {
buttonState3 = digitalRead(botonLD);
buttonState4 = digitalRead(botonLI);
if (buttonState3 == LOW) {
digitalWrite(Relay3, HIGH);
}
else {
digitalWrite(Relay3, LOW);
}
if (buttonState4 == LOW) {
digitalWrite(Relay4, HIGH);
}
else {
digitalWrite(Relay4, LOW);
}

}

//*****************************************************************************

void AUTO() {
buttonState = digitalRead(botonD);
buttonState2 = digitalRead(botonI);
timerState = digitalRead(Timer);
timerState2 = digitalRead(Timer2);
relay1= digitalRead(Relay1);
relay2 = digitalRead(Relay2);
relay3 = digitalRead(Relay3);
relay4 = digitalRead(Relay4);

if (buttonState == LOW && buttonState2 == LOW) {
digitalWrite(Relay3, HIGH);
digitalWrite(Relay1, HIGH);
}

if (timerState == HIGH && relay1 == HIGH) {
digitalWrite(Relay3, LOW);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay4, HIGH);
}

if (timerState2 == HIGH && relay1 == HIGH && relay4 == HIGH) {
digitalWrite(Relay1, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(Relay4, LOW);
}
}

You can add a 'flag' to your if test:

if (flag == 0 && (buttons are in desired state)){
flag == 1;
// allows the action to only happen once, or not until the flag is cleared to 0 again
digitalWrite( ...

}

i forgot to mention, i have to pressed the two buttons within a period of time (let say 200ms) then if i release the buttons the program has to keep running in the order, the buttons are just to give the start order

You are looking to see if the switches are pressed. That is not going to work. You need to look at when the switch BECOMES pressed. Record when that happens. Look at the state change detection example.

When either switch BECOMES pressed, then look to see if the other IS pressed. When both are pressed, and the time between them becoming pressed is small enough, start the process.

can you please give me the URL for this state change detection example? i have been trying to find it,unfortunately i was not able to. i would appreciate it so i can study it

IF THERE IS AN EMERGENCY, I CAN PRESS AN EMERGENCY BUTTON AND THE MACHINE NEEDS TO STOP AT ANY POINT OF THE PROGRAM

Better make the emergency button in "hardware" only and let it switch of the (power of) Arduino too. That is the most safe way imho.

WARNING: Handling events like emergency in a inproper way can have legal consequences in many countries.

Your 'processo' pin is A0. Does this mean that you are using an analog pin for your processo switch? I'll assume that you are not - that you are just using digital pin 0. YOu are already using digitalRead() to get the state of this switch, and it seems to be working for you.

Also, I note that you are using INPUT rather than INPUT_PULLUP for everything. I'll assume that you are using switches wil internall pullup resistors. Again, I assume this because your setup is already partly working. (if your switches dont have internal pullup resistors, this may be why you are getting strange behaviour).

I use classes to isolate the logic. In particular, the "SolenoidPair" class guarrantees that both solenoids will never be on at the same time.

All of the classes have a setup() and a loop() method, even though your outputs don't really need a loop(). This keeps things consistent - everything gets a time slice whether it needs it or not.

Haven't tested this - that many buttons and inputs are a bit of a pain to set up.

/*
As this is partly in a different language, 'I' and 'D' mean 'UP' and 'DOWN'.
*/


enum Pin {
  botonI_pin = 13,   //  THESE FIRSTS TWO BUTTONS NEED TO BE PRESSED ON AUTOMATIC SECTION
  botonD_pin = 12,
  botonLI_pin = 11,   //moves spring up
  botonLD_pin = 10, // moves spring down
  RelayI_solenoid_pin = 1,  // ACTIVATES SOLENOID TO MOVE SPRING UP
  RelayD_solenoid_pin = 2,  // ACTIVATES SOLENOID TO MOVE SPRING DOWN
  RelayI_timer_pin = 3, // ACTIVATES SECOND TIMER
  RelayD_timer_pin = 4, // ACTIVATES FIRST TIMER
  TimerD_pin = 9,  //THIS IS THE 5V SIGNAL FROM THE FIRST  TIMER
  TimerI_pin = 7,  //THIS IS THE 5V SIGNAL FROM THE SECOND TIMER
  Proceso_pin = A0 //HERE I SELECT MANUAL OR AUTOMATIC
};

class AbstractInput {
    boolean stateAnte;
    boolean stateQuo;

  protected:
    virtual boolean read();

  public:
    void setup() {
      stateAnte = stateQuo = read();
    }

    void loop() {
      stateAnte = stateQuo;
      stateQuo = read();
    }

    boolean isRising() {
      return !stateAnte && stateQuo;
    }

    boolean isFalling() {
      return stateAnte && !stateQuo;
    }

    boolean isChanging() {
      return stateAnte != stateQuo;
    }

    boolean isHigh() {
      return stateQuo;
    }

    boolean isLow() {
      return !stateQuo;
    }
};

class DigitalInput : public AbstractInput {
    const Pin pin;

  protected:
    boolean read() {
      return digitalRead(pin) != LOW;
    }

  public:
    DigitalInput(Pin pin) : pin(pin) {}
};

class Timer : public DigitalInput {
    const Pin startPin;
  public:
    Timer(Pin startPin, Pin readPin):
      startPin(startPin), DigitalInput(readPin) {
    }

    void START() {
      digitalWrite(startPin, HIGH);
    }

    void STOP() {
      digitalWrite(startPin, LOW);
    }

};

class SolenoidPair {
    const Pin pinI;
    const Pin pinD;

    boolean goingUp = false;
    boolean goingDown = false;

  public:
    SolenoidPair(Pin pinI, Pin pinD) :
      pinI(pinI), pinD(pinD) {}

    void setup() {
      STOP();
    }

    void loop() {
    }

    void STOP() {
      digitalWrite(pinI, LOW);
      goingUp = false;

      digitalWrite(pinD, LOW);
      goingDown = false;

    }

    void UP() {
      digitalWrite(pinD, LOW);
      goingDown = false;

      digitalWrite(pinI, HIGH);
      goingUp = true;
    }

    void DOWN() {
      digitalWrite(pinI, LOW);
      goingUp = false;

      digitalWrite(pinD, HIGH);
      goingDown = true;
    }

    boolean isMoving() {
      return goingUp || goingDown;
    }

};

DigitalInput botonI(botonI_pin);  //  THESE FIRSTS TWO BUTTONS NEED TO BE PRESSED ON AUTOMATIC SECTION
DigitalInput botonD(botonD_pin);
DigitalInput botonLI(botonLI_pin);   //moves spring up
DigitalInput botonLD(botonLD_pin); // moves spring down
DigitalInput Proceso(Proceso_pin); //HERE I SELECT MANUAL OR AUTOMATIC

SolenoidPair solenoids(RelayI_solenoid_pin, RelayD_solenoid_pin);

Timer TimerI(RelayI_solenoid_pin, RelayI_timer_pin);
Timer TimerD(RelayD_solenoid_pin, RelayD_timer_pin);

enum Auto_State {
  auto_stopped = 0,
  auto_down = 1,
  auto_up = 2
} auto_state;

void setup() {
  pinMode(botonI_pin, INPUT);
  pinMode(botonD_pin, INPUT);
  pinMode(botonLI_pin, INPUT);
  pinMode(botonLD_pin, INPUT);
  pinMode(TimerI_pin, INPUT);
  pinMode(TimerD_pin, INPUT);
  pinMode(Proceso_pin, INPUT);

  pinMode(RelayI_solenoid_pin, OUTPUT);
  pinMode(RelayD_solenoid_pin, OUTPUT);
  pinMode(RelayI_timer_pin, OUTPUT);
  pinMode(RelayD_timer_pin, OUTPUT);

  botonI.setup();
  botonD.setup();
  botonLI.setup();
  botonLD.setup();
  TimerI.setup();
  TimerD.setup();
  Proceso.setup();

  solenoids.setup();

  auto_state = auto_stopped;

}

void loop() {
  // give a time-slice to all the other components

  botonI.loop();
  botonD.loop();
  botonLI.loop();
  botonLD.loop();
  TimerI.loop();
  TimerD.loop();
  Proceso.loop();

  solenoids.loop();

  if (Proceso.isChanging()) {
    solenoids.STOP();
    TimerI.STOP();
    TimerD.STOP();
    auto_state = auto_stopped;
  }

  if (Proceso.isHigh()) {
    AUTO();
  }
  else {
    MANUAL();
  }
}

void MANUAL() {
  if (solenoids.isMoving() && (botonLI.isHigh() == botonLD.isHigh())) {
    solenoids.STOP();
  }
  else if (botonLI.isFalling()) {
    solenoids.UP();
  }
  else if (botonLD.isFalling()) {
    solenoids.DOWN();
  }
}

void AUTO() {
  switch (auto_state) {
    case auto_stopped:
      if (botonD.isLow() and botonI.isLow()) {
        solenoids.DOWN();
        TimerD.START();
        auto_state = auto_down;
      }
      break;

    case auto_down:
      if (TimerD.isHigh()) {
        TimerD.STOP();
        solenoids.UP();
        TimerI.START();
        auto_state = auto_up;
      }
      break;

    case auto_up:
      if (TimerI.isHigh()) {
        TimerI.STOP();
        solenoids.STOP();
        auto_state = auto_stopped;
      }
      break;
  }
}
[code]