Controlling Multiple Servos Separately And Together

Hi, I was wondering if anyone would know the code for controlling multiple servos with multiple potentiometers, then using a button to toggle to a state where a separate potentiometer controls all of the servos at once. In my project, I have 4 servos that I want to first be controlled by 4 seperate potentiometers, then to have a button toggle so that all 4 servos are controlled by the movements of a fifth potentiometer. Is this possible? I have tried to find some code for this, but both it and the hardware don't seem to be working.

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int pot1pin = A1;
int input1;
int pot2pin = A2;
int input2;
int pot3pin = A3;
int input3;
int pot4pin = A4;
int input4;
int pot5pin = A5;
int input5;

const int buttonPin = 9;

void setup() {
  servo1.attach(10);
  servo2.attach(11);
  servo3.attach(12);
  servo4.attach(13);

  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // Activate internal pull-up resistor  
  Serial.begin(9600);
}

void loop() {
  input1 = analogRead(pot1pin);
  input1 = map(input1, 0, 1023, 0, 180);
  servo1.write(input1);
  delay(15);

  input2 = analogRead(pot2pin);
  input2 = map(input2, 0, 1023, 0, 180);
  servo2.write(input2);
  delay(15);

  input3 = analogRead(pot3pin);
  input3 = map(input3, 0, 1023, 0, 180);
  servo3.write(input3);
  delay(15);

  input4 = analogRead(pot4pin);
  input4 = map(input4, 0, 1023, 0, 180);
  servo4.write(input4);
  delay(15);

Have the button change the state of a boolean variable when it becomes pressed

Read the values from all 5 of the pots into an array. One of the values, say the one at index 0 of the array will be the master value.

If the boolean value is true then write the master value to all 4 servos else loop through the array values 1 to 4 and write the individual values to the corresponding servo.
An array of servos would he handy

So let's get the two states thing working first. You'll read the pushbutton into one of two states, which you can think of as cases in the switch/case control scheme (a finite state machine, in other words).
If this is your first time seeing control done with switch/case, it's a very powerful tool in Arduino that I prefer over if/else.
It lets you drive levels or modes in projects like a game.
Consider the following sketch.
Imagine the servos were planes in an airplane simulation game.
In the first case, you have 4 potentiometers (controllers), that you can imagine as a multiplayer mode: you and three friends flying in formation for an airshow.
In the second case, you might imagine that you're playing in a single player mode, with one controller (just you), flying all the planes in the formation.

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int pot1pin = A1;
int input1;
int pot2pin = A2;
int input2;
int pot3pin = A3;
int input3;
int pot4pin = A4;
int input4;
int pot5pin = A5;
int input5;

const int buttonPin = 9;
boolean buttonState;
void setup() {
  servo1.attach(10);
  servo2.attach(11);
  servo3.attach(12);
  servo4.attach(13);

  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // Activate internal pull-up resistor
  buttonState = HIGH;
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  delay(10);
  buttonState = digitalRead(buttonPin); //read again after short delay for debouncing
  switch (buttonState) {
    case HIGH:
      fourPotControl();
      break;
    case LOW:
      fifthPotControlAll();
      break;
  }
}
void fourPotControl() {
  input1 = analogRead(pot1pin);
  input1 = map(input1, 0, 1023, 0, 180);
  servo1.write(input1);
  delay(15);

  input2 = analogRead(pot2pin);
  input2 = map(input2, 0, 1023, 0, 180);
  servo2.write(input2);
  delay(15);

  input3 = analogRead(pot3pin);
  input3 = map(input3, 0, 1023, 0, 180);
  servo3.write(input3);
  delay(15);

  input4 = analogRead(pot4pin);
  input4 = map(input4, 0, 1023, 0, 180);
  servo4.write(input4);
  delay(15);
}

void fifthPotControlAll() {
  input5 = analogRead(pot5pin);
  input5 = map(input5, 0, 1023, 0, 180);
  servo1.write(input5);
  servo2.write(input5);
  servo3.write(input5);
  servo4.write(input5);
  delay(15);
}

Good idea but I think that @lucasb35 wants the button to be a toggle rather than holding it down, but I could be wrong

@lucasb35 please clarify the requirement. Should the "one pot controls all" only happen whilst the button is pressed or should a button press toggle the state so that you don't need to hold it down ?

I was thinking the same thing but I have to leave some tinkering left over :wink:
I'm inclined to think you're correct since it shows a non latching typical pushbutton.
@lucasb35 the technique to do what @UKHeliBob and I suspect you can find in the IDE examples under Digital > StateChangeDetection

Yes I was thinking of a toggle button to control all of the potentiometers @UKHeliBob

Then look at the StateChangeDetection example in the IDE to see how to implement it. Adding an indication of the current state, perhaps an LED, could be useful too

I am still unclear on how to implement the StateChangeDetection, eould you be able to provide sample code? I have tried to modify my code to no avail.

Here is an example taken (stolen) from the StateChangeDetection example

const int buttonPin = A3;
const int ledPin = 13;
byte ledState = HIGH;

int buttonState = 0;
int lastButtonState = 0;

void setup()
{
    pinMode(buttonPin, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
}

void loop()
{
    buttonState = digitalRead(buttonPin);   //read the button
    if (buttonState != lastButtonState)     //test whether it has changed
    {
        if (buttonState == LOW)              //if it has changed and is now pressed
        {
            ledState = !ledState;            //toggle the state variable
        }
        delay(50);
    }
    lastButtonState = buttonState;
    digitalWrite(ledPin, ledState);
}

Every time you press the button the ledState variable flips from HIGH to LOW or vice versa. Then, in loop() the current value is written to the LED

NOTES :

  • the sketch uses the internal pullup resistor so the pin is wired so that the button pin is LOW when pressed
  • the button pin could be any pin of your choosing
  • the debouncing in the sketch is crude and could be done better
  • the value of ledState could be used to control any binary action

State change detection for active low inputs. may have information of use.

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