Controlling 2 servo motors with an UNO

Hello, trying to control two servo motors with two rocker switches and a toggle switch. When the toggle switch is high it controls each motor individually, when the toggle is low it controls them together but in opposite motions. I'm getting a problem where the motors are vibrating and one turns consistently, the rocker switches work but then when pressed they almost latch on and take a long time before returning to a low value.

All criticism welcome, I'm trying my best to learn as I go so will write down anything good for future me. If you could explain why you changed parts as well I'd really appreciate it.

//define pins

#define dir1Pin 2
#define step1Pin 3
#define dir2Pin 4
#define step2Pin 5
#define tog 8
#define rock1Up 10
#define rock1Down 9
#define rock2Up 11
#define rock2Down 12
#define stepsPerRevolution 1600

int switchState=0;

int rock1UpState=0;

int rock1DownState=0;

int rock2UpState=0;

int rock2DownState=0;



void setup() {
 
  // declare inputs and outputs

  pinMode(dir1Pin, OUTPUT); 
  pinMode(step1Pin, OUTPUT);
  pinMode(dir2Pin, OUTPUT);
  pinMode(step2Pin, OUTPUT);
  pinMode(tog, INPUT);
  pinMode(rock1Up, INPUT);
  pinMode(rock1Down, INPUT);
  pinMode(rock2Up, INPUT);
  pinMode(rock2Down, INPUT);

  

}

void loop() {

  switchState=digitalRead(tog); //read if running individual or together


//If selected run individually
  if(switchState==HIGH){

      rock1UpState=digitalRead(rock1Up);
      rock1DownState=digitalRead(rock1Down);
      rock2UpState=digitalRead(rock2Up);
      rock2DownState=digitalRead(rock2Down);

    //operator presses rocker switch to try to move right side up
    
    if(rock1UpState==HIGH){
      //set direction to CCW
      digitalWrite(dir1Pin, LOW);
      digitalWrite(step1Pin, HIGH);
      //set speed
      delayMicroseconds(500);
      digitalWrite(step1Pin, LOW);
      delayMicroseconds(500);
    }
    
    //operator presses rocker switch to try to move right side down
    
    if(rock1DownState==HIGH){
      //set direction to CW
      digitalWrite(dir1Pin, HIGH);
      digitalWrite(step1Pin, HIGH);
      //set speed
      delayMicroseconds(500);
      digitalWrite(step1Pin, LOW);
      delayMicroseconds(500);
    }
  
    //operator presses rocker switch to try to move left side up
    
    if(rock2UpState==HIGH){
      //set direction to CCW
      digitalWrite(dir2Pin, LOW);
      digitalWrite(step2Pin, HIGH);
      delayMicroseconds(500);
      digitalWrite(step2Pin, LOW);
      delayMicroseconds(500);
      
    }
    //operator presses rocker switch to try to move left side down
    
    if(rock2DownState==HIGH){
      //set direction to CW)
      digitalWrite(dir2Pin, HIGH);
      digitalWrite(step2Pin,HIGH);
      delayMicroseconds(500);
      digitalWrite(step2Pin, LOW);
      delayMicroseconds(500);
    }
    
    
  }
  //if selected to run together
  if(switchState==LOW){

      rock1UpState=digitalRead(rock1Up);
      rock1DownState=digitalRead(rock1Down);
      rock2UpState=digitalRead(rock2Up);
      rock2DownState=digitalRead(rock2Down);

    if(rock1UpState==HIGH or rock2DownState==HIGH){
      digitalWrite(dir1Pin, LOW);
      digitalWrite(step1Pin, HIGH);
      delayMicroseconds(500);
      digitalWrite(step1Pin, LOW);
      delayMicroseconds(500);
      digitalWrite(dir2Pin, HIGH);
      digitalWrite(step2Pin,HIGH);
      delayMicroseconds(500);
      digitalWrite(step2Pin, LOW);
      delayMicroseconds(500);
    }
    if(rock2UpState==HIGH or rock1DownState==HIGH){
      digitalWrite(dir1Pin, HIGH);
      digitalWrite(step1Pin, HIGH);
      delayMicroseconds(500);
      digitalWrite(step1Pin, LOW);
      delayMicroseconds(500);
      digitalWrite(dir2Pin, LOW);
      digitalWrite(step2Pin, HIGH);
      delayMicroseconds(500);
      digitalWrite(step1Pin, LOW);
      delayMicroseconds(500);
    }
   
  }

  
 

}

Do you have pull-downs on the switch inputs?

No I don't, could you explain the best sizes to use? and where they would go? I've used Pullup but not down yet

If you've used pullups before, why not here?
They're free, and don't require any soldering.

The following diagram (Fig-1) shows the use of external pull-down and (built-in) internal pull-up resistors. (Use only one and not both at the same time.)


Figure-1:

What sort of motors are you using?

Yeah I think this is a good idea switching it to internal pullups, I'll do that in the morning when I get back into work. The motors are Nema 23 and drivers are TB6600

Thank you for this!

Thank you! changing to pullup made it all work straight away. Thought it was always just a fancy way of doing GND switches so thanks for helping me with that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.