Multiple Servos Controlled By One Set of Push Buttons

Hi,

I was hoping somebody could help me with this coding. I'm trying to use two push buttons to control the forward and reverse motions of a motor. I've got this coding figured out, what I'm now trying to do is have one push button toggle between any number of motors and still be able to use these two push buttons for forward and reverse.

Edit: Please check my latest post for code.

Thanks in advance,
Mike

You can use a 3rd button to cycle through a set of values in a variable (call it servoNum) - 0-4 if you have 5 servos, say. Each button push increments the value. When it exceeds 4 it goes back to 0.

Then you can choose which servo to operate depending on the variable.

if (servoNum == 0) {
   servoA.write(servoPos);
}
else if (servoNum == 1) {
   servoB.write(servoPos);
}
etc

Alternatively you can create an array of servo objects and just do

servoArray[servoNum].write(servoPos);

…R

Robin2:
You can use a 3rd button to cycle through a set of values in a variable (call it servoNum) - 0-4 if you have 5 servos, say. Each button push increments the value. When it exceeds 4 it goes back to 0.

Then you can choose which servo to operate depending on the variable.

if (servoNum == 0) {

servoA.write(servoPos);
}
else if (servoNum == 1) {
  servoB.write(servoPos);
}
etc



Alternatively you can create an array of servo objects and just do


servoArray[servoNum].write(servoPos);




...R

Thanks for your help. Still can't quite get a hang of this code though. How can I set a push button to toggle through this variable, all while having the same two push buttons control the forward and reverse of every motor. I need my forward and reverse push buttons to increment each motors absolute position by 1 degree.

Thanks,
Mike

Here’s my code… Not sure if I’m using the while loop correctly or if I can have multiple if statements under one while loop. The program doesn’t initiate any of the while loops.

#include <Servo.h>

// Set digital pin numbers:
const int servoPinA = 8;  // The number of the ServoA pin
const int servoPinB = 6; // The number of the ServoB pin
const int ButtonForward = 9;  // The number of the fwd. Pushbutton pin
const int ButtonReverse = 7; // The number of rev. Pushbutton pin
const int ButtonMotorToggle = 10; // Motor toggle push button

int ButtonStateFwd = 0;  // Variable for reading the pushbutton status
int ButtonStateRev = 0;
int ButtonStateToggle = 0;
int directionState = 0;  // Variable for reading direction of the servo

Servo servoA;  // Create servo object to control a servo
Servo servoB;
int posA = 0;  // Variable to store servoA's position
int posB = 0; // Variable to store servoB's position
int servoNum = 0; // Variable to select servo to be controlled

void setup() {
  servoA.attach(8);  // attaches servoA to pin 8
  servoB.attach(6); // attaches servoB to pin 6
  pinMode(ButtonForward, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonReverse, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonMotorToggle, INPUT);
  servoNum = constrain(servoNum, 0, 2);
  Serial.begin(9600); // initialize serial
  Serial.println("WE ARE LIVE");
  Serial.print("Servo Number:");
  Serial.print(servoNum);
}

void loop() {
  // read the state of the pushbutton value:
  ButtonStateFwd = digitalRead(ButtonForward);
  ButtonStateRev = digitalRead(ButtonReverse);
  ButtonStateToggle = digitalRead(ButtonMotorToggle);

  servoA.write(posA); // sets initial position for servoA to 0 degrees
  posB = servoB.read(); // sets position variable to servoB's current position


  if (servoNum == 2); {
    servoNum = 0;
  }

  while (servoNum == 0); {
    if (ButtonStateFwd == HIGH); {
      posA += 1;
      servoA.write(posA);
      delay(15);}
    
    if (ButtonStateRev == HIGH) {
      posA -= 1;
      servoA.write(posA);
      delay(15);}

    if (ButtonStateToggle == HIGH) {
      servoNum += 1;
      Serial.print("Servo Number:");
      Serial.print(servoNum);
    }
    }

  while (servoNum == 1); {
    if (ButtonStateFwd == HIGH); {
      posB += 1;
      servoB.write(posB);
      delay(15);}

    if (ButtonStateRev == HIGH) {
      posB -= 1;
      servoB.write(posB);
      delay(15);}
      
   if (ButtonStateToggle == HIGH) {
      servoNum += 1;
      Serial.print("Servo Number:");
      Serial.print(servoNum);}
    }
}

Here is a new code I’ve tried. I’m getting closer but the motors seem to be conflicting with each other at some points. Is there any way that capacitance on the pins could possibly cause this as well?

When I try to reverse servoB it, the push button is reversing both servos at the same time.

#include <Servo.h>

// Set digital pin numbers:
const int servoPinA = 8;  // The number of the ServoA pin
const int servoPinB = 6; // The number of the ServoB pin
const int ButtonForward = 9;  // The number of the fwd. Pushbutton pin
const int ButtonReverse = 7; // The number of rev. Pushbutton pin
const int ButtonMotorToggle = 10; // Motor toggle push button

int ButtonStateFwd = 0;  // Variable for reading the pushbutton status
int ButtonStateRev = 0;
int ButtonStateToggle = 0;
int directionState = 0;  // Variable for reading direction of the servo
int posA = 0;  // Variable to store servoA's position
int posB = 0; // Variable to store servoB's position
Servo servoA;  // Create servo object to control a servo
Servo servoB;
int servoNum = 0; // Variable to select servo to be controlled

void setup() {
  servoA.attach(8);  // attaches servoA to pin 8
  servoB.attach(6); // attaches servoB to pin 6
  pinMode(ButtonForward, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonReverse, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonMotorToggle, INPUT);
  servoNum = constrain(servoNum, 0, 2);
  posA = min(posA, 1);
  posA = max(posB, 179);
  posB = min(posA, 1);
  posB = max(posB, 179);
  Serial.begin(9600); // initialize serial
  Serial.println("WE ARE LIVE");
  Serial.print("Servo Selected:");
  Serial.print(servoNum);
}

void loop() {
  // read the state of the pushbutton value:
  ButtonStateFwd = digitalRead(ButtonForward);
  ButtonStateRev = digitalRead(ButtonReverse);
  ButtonStateToggle = digitalRead(ButtonMotorToggle);

  servoA.write(servoA.read()); // sets position variable to servoA's current position
  servoB.write(servoB.read()); // sets position variable to servoB's current position

  if (servoNum == 3) {
    servoNum = 1;
    Serial.print('\n');
    Serial.print("Servo Selected:");
    Serial.print(servoNum);
  }

  if (ButtonStateToggle == HIGH) {
    servoNum = servoNum + 1;
    Serial.print('\n');
    Serial.print("Servo Selected:");
    Serial.print(servoNum);
    delay(3000);
  }

  if (servoNum == 1) {
    if (ButtonStateFwd == HIGH) {
      posA += 1;
      servoA.write(posA);
      Serial.print('\n');
      Serial.print("servoA Angle:");
      Serial.print(posA);
      delay(15);

      if (posA >= 179) {
        posA = 178;
      }

      if (posA <= 1) {
        posA = 2;
      }
    }
  }

  if (ButtonStateRev == HIGH) {
    posA -= 1;
    servoA.write(posA);
    Serial.print('\n');
    Serial.print("servoA Angle:");
    Serial.print(posA);
    delay(15);

    if (posA >= 179) {
      posA = 178;
    }

    if (posA <= 1) {
      posA = 2;
    }
  }

if (servoNum == 2) {
  if (ButtonStateFwd == HIGH) {
    posB += 1;
    servoB.write(posB);
    Serial.print('\n');
    Serial.print("servoB Angle:");
    Serial.print(posB);
    delay(15);

    if (posB >= 179) {
      posB = 178;
    }

    if (posB <= 1) {
      posB = 2;
    }
  }

  if (ButtonStateRev == HIGH) {
    posB -= 1;
    servoB.write(posB);
    Serial.print('\n');
    Serial.print("servoB Angle:");
    Serial.print(posB);
    delay(15);

    if (posB >= 179) {
      posB = 178;
    }

    if (posB <= 1) {
      posB = 2;
    }
  }
}
}

When I try to reverse servoB it, the push button is reversing both servos at the same time.

Have you considered that mechanical push buttons probably need some amount of "debouncing"? In toggling conditions using "click" type push buttons may minimize the time required to accommodate debouncing.

zoomkat:
Have you considered that mechanical push buttons probably need some amount of "debouncing"? In toggling conditions using "click" type push buttons may minimize the time required to accommodate debouncing.

I have not. Does this require any circuitry or just programming? I've searched up debouncers and am now trying to figure out how to include it into my program.

Edit: As for the physical circuitry I added some non polarized caps in parallel to each switch.

Also, is there something in my code that's causing this issue.. When I'm toggled to motor 2 and whole the reverse push button, it reverses and even prints to serial the angle of both motors. Is this an issue with my code or probably just some debouncing code that's required?

Thanks,
Mike

Previous toggle discussions that might have code examples.

https://www.google.com/search?ie=UTF-8&oe=UTF-8&q=toggle&btnG=search&domains=http%3A%2F%2Fforum.arduino.cc&sitesearch=http%3A%2F%2Fforum.arduino.cc&gws_rd=ssl

MikeC93:
When I try to reverse servoB it, the push button is reversing both servos at the same time.

You have a big delay(3000); which should deal with any debounce problem with servo selection. In fact delay(300); would be sufficient.

Assuming that the value of the variable servoNum does not change I don't see why the wrong servo would move - unless your code for servoA and servoB have mistakes.

If you use arrays you can get by with a single piece of code that works with either servo and that greatly reduces the risk of typos.

...R

Well I'm not worried about the debounce for the pushbutton that changes motors rather than the foward and reverse pushbuttons.

I set a 3000 delay purposely just for testing as I'm in no rush to change motors but want it done without a false change.

I guess I'll have to look into arrays. I just starting programming a couple weeks ago so this is all very new to me. Any suggestions and advice is greatly appreciated.

I'll look into ZoomKat's post when I have some time.

Thanks for the responses,
Mike