Two servos one sweep other button activated

I am truing to control two servos one running sweep constantly (To move a small arrow sign) the other opens a door when the button is pressed. My button only works when the sweep is at 0 and then the door will open but the sweep will stop. If the sweep is any other number the button will not open the door. See my loop code below. I am using the pwm servo library so I can add neopixals to my project latter. Any ideas I can try or am I trying to much at once? I hope I posted this in the correct place if not please let me know where to move it.

void loop(){
  
  buttonState = digitalRead(buttonPin);    // read the state of the pushbutton value:
  if (buttonState == HIGH)                 // check if the pushbutton is pressed.
  {       
    digitalWrite(ledPin, HIGH);            // turn LED on:
    myservoDoor.write(3);                 //Door close
  } 
  else 
  {
    digitalWrite(ledPin, LOW);             // turn LED off:
    myservoDoor.write(91);                //Door open
    delay(5000);                          
    myservoDoor.write(3);                 //Door close
  }
  
  for(posArrow = 0; posArrow < 180; posArrow += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservoArrow.write(posArrow);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(posArrow = 180; posArrow>=1; posArrow-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservoArrow.write(posArrow);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

put code in the for loops (servo sweeps) that checks the button state. As it is you check once through loop() but it never checked while the servos are moving.

I still have the same problem with the button check in the for loops. The sweep stops and the button only works at sweep position zero. What did change is after the door opens it then closes one degree for every 5000 delay. Did I not move it to the correct place or is there more that I am missing?

void loop(){
  
  buttonState = digitalRead(buttonPin);    // read the state of the pushbutton value:

  for(posArrow = 0; posArrow < 180; posArrow += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    if (buttonState == HIGH)                 // check if the pushbutton is pressed.
    {       
      digitalWrite(ledPin, HIGH);            // turn LED on:
      myservoDoor.write(3); ;
    } 
    else 
    {
      digitalWrite(ledPin, LOW);             // turn LED off:
      myservoDoor.write(91);
      delay(5000);
      myservoDoor.write(3); 
    }
    myservoArrow.write(posArrow);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(posArrow = 180; posArrow>=1; posArrow-=1)     // goes from 180 degrees to 0 degrees 
  {  
    if (buttonState == HIGH)                 // check if the pushbutton is pressed.
    {       
      digitalWrite(ledPin, HIGH);            // turn LED on:
      myservoDoor.write(3); ;
    } 
    else 
    {
      digitalWrite(ledPin, LOW);             // turn LED off:
      myservoDoor.write(91);
      delay(5000);
      myservoDoor.write(3); 
    }    
    myservoArrow.write(posArrow);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }

I think @groundfungus meant that you need to read the button state in the FOR loops so that your code can respond to a new button press.

You really should rewrite your code so it only moves the servos 1 step for each iteration of loop() and then you can also read the button state once in loop().

...R

After a good nights sleep I did get it working. @groundfungus and @Robin2 were correct I did not move all the code into the for loop. When I did read the button in the for loop the door would open at any time but the sweep would stop. I will have to do some hard thinking to get @robin2 idea going but I think he is right that is the only way to get both going at the same time. Thanks for the help and good ideas!

I will have to do some hard thinking to get @robin2 idea going but I think he is right that is the only way to get both going at the same time.

It is really pretty simple. On each pass through loop(), check the switch and step the servo one position.

The only "hard" part is figuring out the next position to step to. And, a little thought reveals that that is not even hard. The servo is moving some direction. If that direction is positive, and the servo is not at the limit, increment the position and apply to the servo.

If the direction is positive, and the servo is at the limit, change the direction to negative, decrement the position, and apply to the servo.

If the direction is negative, and the servo is not at the limit, decrement the position and apply to the servo.

If the direction is negative, and the servo is at the limit, change the direction to positive, increment the position, and apply to the servo.

You can see that there are only 4 things to keep track of (and two of them are constants) - the direction, the current position, the upper limit (constant) and the lower limit (also constant).

Give this a try. It's not that hard. Six if or else statements altogether.

(Yes, it can be simplified, but this is the easiest to understand).

Thanks @Pauls I was thinking it would be a bunch of if statements to replace the for loop requirement. Now that I know I am on the right track it makes it easier to push on without questioning if I am doing the right thing. This forum is great!

Code I suggested in this Thread may be of interest.

...R

Thanks for the help! Robin I gave your idea a try and it really cleaned up the code. Done for tonight but I will try to get the if statements working so both servos can move at the same time. I am happy with what all of you have helped me get working so far!

void loop()
{
  sweep();                          // sweep arrow and check for button press
}

void door()                                         // open door wait then close door
{
  buttonStateDoor = digitalRead(buttonPinDoor);     // read the state of the pushbutton value:
  if (buttonStateDoor == HIGH)                      // check if the pushbutton is pressed.
  {       
    digitalWrite(ledPinDoor, HIGH);                 // turn LED on
    myservoDoor.write(3);                           // close door
  } 
  else 
  {
    digitalWrite(ledPinDoor, LOW);                  // turn LED off
    myservoDoor.write(91);                          // open door
    delay(5000);                                    // wait
    myservoDoor.write(3);                           // close door
    digitalWrite(ledPinDoor, HIGH);                 // turn LED on
  }
}

void sweep()                                        // sweep arrow and check for button press
{
  for(posArrow = 0; posArrow < 180; posArrow += 1)  // goes from 0 to 180 degrees 
  {                                  
    door();                                         // checks for button press if true then open and closes door
    myservoArrow.write(posArrow);                   // tell servo to go to position in variable 'posArrow' 
    delay(15);                                      // waits for the servo to reach the position 
  } 
  for(posArrow = 180; posArrow>=1; posArrow-=1)     // goes from 180 to 0 degrees 
  {  
    door();                                         // checks for button press if true then open and closes door    
    myservoArrow.write(posArrow);                   // tell servo to go to position in variable 'posArrow' 
    delay(15);                                      // waits for the servo to reach the position 
  } 
}