Do until button is pressed

Hi, I need some help. I am working on a volunteer project for my community. I thought I could do all of the programming myself but I can't figure out a part of it. I will try to explain what needs to happen. There are two switches, one is a safety the other is the one the person would press to complete an action. When the button is pressed I want pin 4 to go high, but only if the safety is low. I want pin 4 to stay high until the safety button is pressed.

I hope this is enough for you to help me, I know it is probably something really easy.
Thanks

How about posting the code you have so far? Between code tags.

This is the part of the code I am struggling with. I am not sure what the correct way to do this is. I might be totally in the wrong path with this code. What I want is when the leftstate button is pressed I want it to first check to make sure outstate is not pressed and then then make relayPin4 and 5 to go HIGH until oustate is pressed. This code is not doing what I want. In my project the outstate is a safety switch that stops a swinging arm going to the left. Outstate is just a small micro switch that the arm hits.

if (leftstate == HIGH) {
  while (outstate == LOW) {
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  }
}

If outstate is LOW, how will this loop ever exit?

while (outstate == LOW) {
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  }

Unless you have an interrupt changing it, I suspect you will need a digitalRead in that loop too.

I am not sure what the correct way to do this is. I might be totally in the wrong path with this code.

Suppose that leftState IS high, and that outState IS low. Why do you need to turn the pins off over and over?

You don't, so, let's revise the code:

if (leftstate == HIGH)
{ // Down here where it belongs
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  while (outstate == LOW)
  {  // Down here where it belongs
  }
}

Now, what is going to make the while loop end? Hell will freeze over first.

I do not understand what has to happen with the while loop now. RelayPin4 stays HIGH the entire time.

Where are you reading the pin that is stored as 'outstate'?

Outstate is on pin 9, if that is what you mean. I will show you my entire code the part that is at the very is the part I can't figure out, everything else is working.

// A1 = stop
// A2 = left
// A3 = right
// A4 = down
// A5 = up
// 8 = in safey
// 9 = out safety
// left = out
// right = in
const int Up = A5;
const int Down = A4;
const int Right = A3;
const int Left = A2;
const int Stop = A1;

const int inSafe = 8;
const int outSafe = 9;

const int relayPin1 = 0;              
const int relayPin2 = 1;              
const int relayPin3 = 2;              
const int relayPin4 = 3;               
const int relayPin5 = 4;                
const int relayPin6 = 5;               
const int relayPin7 = 6;                
const int relayPin8 = 7; 

int upstate = 0;
int downstate = 0;
int rightstate = 0;
int leftstate = 0;
int stopstate = 0;

int instate = 0;
int outstate = 0;


void setup()
{
  pinMode(Up, INPUT);
  pinMode(Down, INPUT);
  pinMode(Right, INPUT);
  pinMode(Left, INPUT);
  pinMode(Stop, INPUT);
  
  pinMode(inSafe, INPUT);
  pinMode(outSafe, INPUT);
  
  pinMode(relayPin1, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin2, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin3, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin4, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin5, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin6, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin7, OUTPUT);      // sets the digital pin as output
  pinMode(relayPin8, OUTPUT);      // sets the digital pin as output
 
  digitalWrite(relayPin1, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin2, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin3, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin4, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin5, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin6, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin7, HIGH);        // Prevents relays from starting up engaged
  digitalWrite(relayPin8, HIGH);        // Prevents relays from starting up engaged
}

void loop(){
  
  upstate = digitalRead(Up);
  downstate = digitalRead(Down);
  rightstate = digitalRead(Right);
  leftstate = digitalRead(Left);
  stopstate = digitalRead(Stop);
  
  instate = digitalRead(inSafe);
  outstate = digitalRead(outSafe);
  
if (downstate == HIGH) {
  digitalWrite(relayPin2, LOW);   
  delay(300);
  digitalWrite(relayPin2, HIGH);
  delay(500);
  digitalWrite(relayPin1, LOW);
  delay(300);
  digitalWrite(relayPin1, HIGH);
  }
  
if (stopstate == HIGH) {                                                //Stops everything
  digitalWrite(relayPin2, LOW);                                         //Needs to include screen stop and arm stop
  delay(300);
  digitalWrite(relayPin2, HIGH);
  }
  
if (upstate == HIGH) {                                                  //Puts screen up
  digitalWrite(relayPin2, LOW);   
  delay(300);
  digitalWrite(relayPin2, HIGH);
  delay(500);
  digitalWrite(relayPin3, LOW);
  delay(300);
  digitalWrite(relayPin3, HIGH);
  }
//-----------------------------------------------------------------------------------------------------------------------
if (leftstate == HIGH) 
{
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  while (outstate == HIGH)
  {
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin5, HIGH); 
  }
}
}

Would this be the problem ?

pinMode(outSafe, INPUT);

...R

I don't think that is the problem, it is the same for all the switches. I think the problem is in this part of the code.

if (leftstate == HIGH) 
{
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  while (outstate == HIGH)
  {
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin5, HIGH); 
  }
}

I don't know what is wrong with it. I think it is part of the while statement. I want relayPin4 and 5 to go HIGH when outstate is HIGH.

Try:

if (leftstate == HIGH) 
{
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
  while (outstate=digitalRead(outSafe) == HIGH)
  {
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin5, HIGH); 
  }
}

luisilva:
Try:

if (leftstate == HIGH) 

{
 digitalWrite(relayPin4, LOW);
 digitalWrite(relayPin5, LOW);
 while (outstate=digitalRead(outSafe) == HIGH)
 {
 digitalWrite(relayPin4, HIGH);
 digitalWrite(relayPin5, HIGH);
 }
}

== has a higher precedence than = so you need parentheses to change the order of operations, like this: (outstate=digitalRead(outSafe)) == HIGH

Try:

if (leftstate == HIGH && outstate == LOW) 
{
  digitalWrite(relayPin4, LOW);
  digitalWrite(relayPin5, LOW);
 }
 else{
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPin5, HIGH); 
 }

TRLEMUR:
I don't think that is the problem, it is the same for all the switches.

Did you read the line I posted VERY CAREFULLY?

Did you check the spelling?

...R