Hello everyone
I'm trying to alternate between two digital outputs. The first digital output is setup to switch on at a certain value, lets call it 60% and to switch off at 20%. The second digital output is setup to switch on at 65% and to switch off also at 20%.
The active range will then be between 20% and 60%. For instance, when a reading of 60% is reached, then digital output 2 switches on. It then stays on until the reading drops to 20% or below that value. The reading then automatically increases again. Once it reaches 60% again, it must switch on digital output 3 instead of 2.
Lets say this happens but the reading is still increasing and not decreasing. Say it then reaches 65% then it much switch on digital output 2 as well. This means that digital output 2 and 3 is on at this stage. Now the reading is decreasing again. Once it reaches 20% both digital outputs switch off. Once the reading reaches 65% again, digital 2 switches on again. so it is still alternating between the two outputs. The setup of the second digital output is a fail safe value, i.e. when digital output one is switched on and the reading doesn't decrease but still increases, once it reaches the fail safe value the second digital output also switches on.
The functionality is called alternating pump control. I've come up with the following code and as I look at it, it should be working but it only keeps switching digital output 2 and never switches digital output 3.
I'm using a GLCD and have setup a simple count up count down program to test my code. I'm displaying the counted value i.e. the measured value and also the alternating trigger which acts as to switch between digital output 2 and 3.
Thanks in advance
Dirk
void loop()
{
GLCD.SelectFont(System5x7);
GLCD.CursorToXY(30, 19);
GLCD.print(Measured_Value);
GLCD.CursorToXY(30, 29);
GLCD.print(Alternating_Trigger);
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();
}
void Alternating_Pump_Control_trigger_function()
{
if ( Alternating_Pump_Control_Selector == 1 ) // Only execute code if the alternating pump control is activated.
{
if ( Alternating_Trigger == 1 && ( Measured_Value >= Relay1_On_Val ) )
{
digitalWrite(Relay1, HIGH);
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);
}
}
//******************************************************************************************************************************************************************************
if ( Alternating_Trigger == 1 )
{
if ( Measured_Value <= Relay1_Off_Val ) // When the off setpoint is reached, switch off the relay to switch off the pump
{
Alternating_Trigger = 2; // Set Alternating Trigger to 2 so that the pumps will be alternated with the next cycle.
digitalWrite(Relay1, LOW);
}
if ( Measured_Value <= Relay2_Off_Val ) // When the off setpoint is reached, switch off the relay to switch off the pump
{
digitalWrite(Relay2, LOW);
}
}
//******************************************************************************************************************************************************************************
if ( Alternating_Trigger == 2 && ( Measured_Value >= Relay1_On_Val ) ) //Relay 1 on limit is your level trigger.
{
digitalWrite(Relay2, HIGH);
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);
}
}
//******************************************************************************************************************************************************************************
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);
}
}
//******************************************************************************************************************************************************************************
}
}