Using two servos with Nano board, only one works at a time

I am building a prop for a friend that is detailed pretty well here: https://www.instructables.com/Dead-Space-Schofield-Tools-211-V-Plasma-Cutter/

Edit
Updated the new code that @runaway_pancake was kind enough to provide. Now both servos properly cycle when power is applied, so that is working like it should. The current snag I have is that in changing the code it also changed the rotate and extend buttons. When pressing the button to extend, both servos engage. Pressing the button again makes the servos rotate/extend and then rotate/extend back to their starting point without releasing the button. No wires were changed, only the code. The button for rotating does not do anything.

Origional issue
I have followed all the steps and wiring 3 times now and still the same issue, even using 2 other arduino Nano boards for testing. I have two servos that are supposed to reset themselves as soon as the model gets power. Then pressing a button will rotate one servo, or both depending on the way the buttons are pressed. The issue is that when the model gets power only the servo on pin 10 works. The servo on pin 8 does not do anything. I swapped the servos to make sure they were both good and the previously unworking servo does work fine when swapped, but the previously working servo does not. When I continue testing hitting the switch that should make servo 10 move does work fine too, nothing happens when hitting the button to make servo 8 spin though the servo does not respond when the power is plugged in anyway. I have been able to fully rule out hardware and I have adjusted and readjusted the wires over and over. I am out of ideas to test for the issue. Any advise would be very helpful

#include <Servo.h> 
 
Servo rotator;                        // create rotator servo
Servo extender;                       // create extender servo

int rotpin = 8;                       // rotator servo on pin 8
int extpin = 10;                      // extender servo on pin 10
int buttonpin = 2;                    // rotate button on pin 2
int triggerpin = 3;                   // trigger on pin 3
int pos = 0;                          // servo position variable
int rotflag = 0;                      // rotation flag, 0 vertical, 1 horizontal
int extflag = 0;                      // extension flag, 0 retracted, 1 extended
int extupper = 125;                   // upper travel limit for extender servo
int extlower = 0;                     // lower travel limit for extender servo
int rotupper = 126;                   // upper travel limit for rotator servo
int rotlower = 36;                    // lower travel limit for extender servo
 
void setup() 
{ 
  extender.attach(extpin);                   // attach extender servo
  rotator.attach(rotpin);                    // attach rotator servo
  
  extender.write(extlower);                  // ensure fins are extended in case the rotator needs to move
  delay(300);                                // wait for servo to get there
  
  rotator.write(rotupper);                   // move rotator to vertical
  delay(300);                                // wait for servo to get there
  
  extender.write(extupper);                  // retract fins
  delay(300);                                // wait for servo to get there
  
  pinMode(buttonpin, INPUT_PULLUP);   // set up rotation button with internal pull-up enabled
  pinMode(triggerpin, INPUT);         // set up trigger, uses external pull-down
} 
 
void loop() 
{ 
  if (digitalRead(triggerpin) == HIGH) {          // if trigger is released
    if (extflag == 1) {                           // and if fins are extended
      for(pos = (extlower); pos < (extupper); pos += 1)  // retract fins 
        {                                   
          extender.write(pos);               
          delay(0);                        
        }
        extflag = 0;                              // set flag to retracted
      }
    } else {                                         // otherwise if trigger is pulled
      if (extflag == 0) {                            // and if fins not already extended
        for(pos = (extupper); pos >= (extlower); pos -= 1)  // extend fins 
        {                                   
          extender.write(pos);               
          delay(0);                        
        }
        extflag = 1;                                 // set flag to extended
      }
      if (digitalRead(buttonpin) == LOW) {           // then check if rotate button is pressed
        if (rotflag == 0) {                          // if blade is vertical
          for(pos = (rotupper); pos >= (rotlower); pos -= 1)// rotate to horizontal
            {                                   
              rotator.write(pos);               
              delay(2);                        
            } 
            rotflag = 1;                              // set flag to horizontal
        } else {
          for(pos = (rotlower); pos < (rotupper); pos += 1)  // otherwise rotate to vertical
            {                                
              rotator.write(pos);              
              delay(2);                       
            } 
            rotflag = 0;                             // set flag to vertical
        }
        while (digitalRead(buttonpin) == LOW) 
        {
          delay(1);                         // wait until rotate button is released
        }
        delay(200);                         // mitigate accidental double presses
      }
    }
}

Show a circuit diagram of your complete setup including power supplies .
NOT fritzing thankyou.

Never mind, I misread something.

Shouldn't rotpin be assigned to a PWM pin ?

In "ruling out" hardware - have you tried reassigning rotpin to another 'pin' anyway?

I set it to pin 8 right after posting this. It was set to pin 9 origionally in the instructable build tutorial. It did not work on pin 9 either.

I am not sure why that is there, I used the code provided in the instructable that the origional author used as well as a few others who copied his build over the years.

Here are direct links to the parts I am using too if it helps.

Battery:
Voltage Regulator
Servo Pin 8
Servo Pin 10

Does it make a difference having the attach() staggered ?
What if --

void setup() 
{ 
  extender.attach(extpin);                   // attach extender servo
  rotator.attach(rotpin);                    // attach rotator servo
  
  extender.write(extlower);                  // ensure fins are extended in case the rotator needs to move
  delay(300);                                // wait for servo to get there
  
  rotator.write(rotupper);                   // move rotator to vertical
  delay(300);                                // wait for servo to get there
  
  extender.write(extupper);                  // retract fins
  delay(300);                                // wait for servo to get there
  
  pinMode(buttonpin, INPUT_PULLUP);   // set up rotation button with internal pull-up enabled
  pinMode(triggerpin, INPUT);         // set up trigger, uses external pull-down
} 

Thats a fair point. I dont know enough about it to jump right in and code it myself so I went with what other people who made the project said worked for them.

I am very new with the arduino side of things, please forgive the ignorance. Is that just putting spaces in the code you posted vs. the code I posted?

No - if you look at it critically you will see that the attach() operations are done in succession.(Maybe it doesn't make a difference.)
Did you try it (before posing your question)?
It's not like I'm asking you to carry a hod full of bricks down the block.

I was just making sure I understood what you were asking so I can understand/learn the difference. With your code both servos activate when the battery is plugged in as they should. When the trigger switch is pressed both servos also activate instead of just the extender servo. When the trigger is pressed/helded and the rotate button is pressed neither servo activates.

Diagram shows servos on pins 9 and 10 yet you say pins 8 and 10.
Pin 2 when open (sw off) could float anywhere.
I find using an internal pullup resistor and not this pull down configuration avoids problems such as this.
Code naturally would need to normally be adjusted to allow for this.
Looks like your "physical" wiring is different from the circuit diagram eg a dodgy common ground somewhere.

The 5V power should be connected to Arduino 5V pin, not VIN.

1 Like

This is progress then, yes?

That is true. I tried pin 8 after pin 9 was not working to test as someone else suggested and just kept saying pin 8. I should have noted that again when I posted the diagram.

The trigger to extend the blade servo was working before the code adjustment but the rotate servo was not working at all. It is progress for one but caused an issue with another.

Straightening out a preliminary problem revealing problems down the line is not causing problems.

Best of luck.

right...just noting the new issue resulting from a code change...