Trying to setup an automatic alternating function to alternate outputs

Hello PaulS and Robin2

I've created a separate sketch which I've posted in my previous post. I kept the sketch separate from my main body of code so that I can simulate test and make sure that the code is functioning correctly before I'm going to implement it into my main sketch.

From the code that I've posted:

Here I'm just printing the simulated measured value and the alternating trigger. The alternating trigger variable is responsible for selecting either relay1 (ditial output 2) or relay2 ( digital output 3) and to switch it on or off. This part of the code can obviously be changed to serial print but since I have everything hooked up already I was using the GLCD for testing the functionality of the code.

  GLCD.SelectFont(System5x7);
  GLCD.CursorToXY(30, 19);
  GLCD.print(Measured_Value);
  GLCD.CursorToXY(30, 29);
  GLCD.print(Alternating_Trigger);

This part of the code that is in my main loop is used to simulate a rising and falling measured value. Once the setpoint is reached then the value must start to decrease again as it would should a pump start pumping the level down:

 if ( Switch_trigger == 1 && ( Measured_Value <= Relay1_On_Val ) )
  {
    Measured_Value +=5;
    delay(1000);
    
    if ( Measured_Value > Relay1_On_Val )
    {
      Switch_trigger = 2;
    }      
  } 

  if ( Switch_trigger == 2 && ( Measured_Value >= Relay1_Off_Val ) )
  {
    Measured_Value -= 5;
    delay(1000);    
    
    if ( Measured_Value < Relay1_Off_Val )
    {
      Switch_trigger = 1;
    }  
  }
  
  Alternating_Pump_Control_trigger_function();

At the end of my void loop() you'll see that I'm calling my alternating pump control function.

The function starts of with a if which validates that Alternating_Pump_Control_Selector == 1. If not the whole function is bypassed. This is to check whether the user is implementing alternating pump control or just normal limits in the unit.

If the user is implementing alternating pump control then I check if the measured value is above relay 1 on setpoint, declared as Relay1_On_Val. If it is I switch Relay 1 on.

    if ( Alternating_Trigger == 1 && ( Measured_Value >= Relay1_On_Val ) )
    {
      digitalWrite(Relay1, HIGH);

You'll notice that there is a second if within this if. This is used to test if the level is still increasing and to switch on the second relay should the level increase above the fail safe setpoint.

      if ( Measured_Value >= Relay2_On_Val )        // Trigger Relay 2 on as well if the value is still rising. This indicates that the first pump is not able to pull the level down i.e.
      {                                             // pump requires maintenance or pump is broken.
        digitalWrite(Relay2, HIGH); 
      }

I then go and test whether the alternating trigger is set to 1 in a seperate if statement. This if statement is then used to test whether the lower setpoint have been reached as yet or not. If the measured value is below the lower setpoint, Relay1_Off_Val, switch the relay off by making digital output 2 low. Before this is done I set the Alternating_Trigger to value 2 so that the inverse of the above code can be executed.

After the this if statement I have another one to test whether the measured value is below Relay2_Off_Val as well to switch of this relay as well.

So now the measured value should increase again and the alternating trigger is set to a value of 2. Once the level reaches Relay1_On_Val again, then we should enter the inverse setup if statement which is the following code:

if ( Alternating_Trigger == 2 && ( Measured_Value >= Relay1_On_Val ) )      //Relay 1 on limit is your level trigger.
    {
      digitalWrite(Relay2, HIGH);

and here we test the fail safe again but you'll notice that I'm switching the relays in reverse order or alternate order:

      if ( Measured_Value >= Relay2_On_Val )        // Trigger Relay 2 on as well if the value is still rising. This indicates that the first pump is not able to pull the level down i.e.
      {                                             // pump requires maintenance or pump is broken.
        digitalWrite(Relay1, HIGH); 
      }

Lastly I test in a new if statement again if the alternating trigger is set to a value of 2 and if so I test to see when the level drops below the lower setpoints to switch off the relays and to switch the alternating trigger back to a value of 1 so that the top part of the code can be executed:

    if ( Alternating_Trigger == 2 )
    {  
      if ( Measured_Value <= Relay1_Off_Val )       // When the off setpoint is reached, switch off the relay to switch off the pump
      {
        Alternating_Trigger = 1;                      // Set Alternating Trigger to 2 so that the pumps will be alternated with the next cycle. Only do this once the relay off limit have been reached otherwise it the following alternating sequence starts anyways.        
        digitalWrite(Relay2, LOW); 
      }      
        
      if ( Measured_Value <= Relay2_Off_Val )       // When the off setpoint is reached, switch off the relay to switch off the pump
      {
        digitalWrite(Relay1, LOW); 
      }
    }

My problem is that Alternating trigger never changes from a value of 1 to a value of 2 so that the alternate part of the code can be executed. It stays stuck on a value of 1 and I can't seem to wrap my head around why it is doing that. I know that this is quite a mouth full so thank you for taking the time to go through it. If something is still unclear please let me know.

Dirk