Setting digital output to be high as a default

Hello,

I am using Arduino UNO for my project. In this project I use a single-pole-double-throw relay. I need to use both states of a relay (i.e. I control a pump when there is no connection established and control a solenoid valve when there is). My code is as follows:

void regular_fill (){
  
  
  unsigned long time, old_time;
  int check_period = 1000;//check period = 1 sec --> pause between pressure switch checks

ll_reading = check_ll(); //initial check --> double check for an error in "loop" function
ul_reading = check_ul(); //initial check
old_time = millis (); //reference time, needed for periodical check of pressure switch

while (ll_reading == 1 && ul_reading !=1){
  
  //code block for pressure switch check
  time = millis();
  if ((time-old_time)>=1000){
    ps_reading = check_ps();
    old_time = time;
  }
  if (ps_reading == 1)
    return;
  
  digitalWrite (lower_relay, LOW);
  delay (100);
  
  ul_reading = check_ul(); //continuos check to meet WHILE loop condition
  
}
 
//want to return lower_relay to HIGH here
  
  /* how do I return lower_relay to HIGH so that it stays there forever or 
      until I change its state? 
     digitalWrite (lower_relay, HIGH);//turn OFF 24V airgap pump and airgap solenoid valve --> quite sure will not work
  */
  return;
  
}

In the code above, for this subroutine, I need the state of upper_relay to be LOW until I hit certain water level and after I need to drop the relay to another state. But to do this, I need to set lower_relay to HIGH and it needs to stay at HIGH for other parts of code to be functional.

Please suggest how to set a digital pin to HIGH so that it will be in that state globally until I call it up again (i.e. want to have HIGH as default).
I know I could change relay connections, but it will interfere with voltage-sensitive components.... thus I really cannot change my circuit..

pinMode(outputPin, OUTPUT);
digitalWrite(outputPin, HIGH);

The output pin will stay HIGH until you change it.

Thank you for your reply

I inserted that code in void setup () function. Or you are saying that I can assign something to be HIGH and as OUTPUT not only in void setup () function, but also anywhere in the code?

That is correct. You can change the mode from INPUT to OUTPUT in void setup(), in void loop(), in a function.
Once it is an output, you can digitalWrite it HIGH or LOW as well.

Usually the hardware connected requires the pin to be INPUT or OUTPUT all the time, so it using pinMode in void setup() is all that is needed.

OO Thanks!

That opens a new world of opportunities! :smiley: