toggle switches with arduino and pca9685

Hello, I wish to connect 16 servos on a pca9685 board with 16 toggle switches connected to an arduino uno but am unable to figure out how to code the arduino to have each toggle switch move one servo each.
All the examples I could find show all the servos moving back and forth.

Really? I find dozens of hits when googling.... Some even on this forum like this

I know it is probably something simple that I am not seeing but everything I have looked at seems not show how to attach each switch pin to each servo so that each switch only controls the servo it is meant to. Any help would be greatly appreciated.

Here's a simple way, with 8 switches and 8 servo's, no PCA9865, to test out your wiring to start.
Add the PCA9865 after you get this working.

#include <Servo.h> // supports 12 servos on an Uno, 48 on a Mega - something else to consider
byte switch0 = 2;
byte switch1 = 3;
byte switch2 = 4;
byte switch3 = 5;
byte switch4 = 6;
byte switch5 = 7;
byte switch6 = 8;
byte switch7 = 9;


Servo myservo0;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
// create servo object to control a servo
// twelve servo objects can be created on most boards


byte pos = 0;    // variable to store the servo position


void setup() {
  myservo0.attach(10);  // attaches the servo on pin 9 to the servo object
  myservo1.attach(11);
  myservo2.attach(12);
  myservo3.attach(13);
  myservo4.attach(14);
  myservo5.attach(15);
  myservo6.attach(16);
  myservo7.attach(17);


  pinMode (switch0, INPUT_PULLUP); // connect toggle to connect pin to Gnd only
  pinMode (switch1, INPUT_PULLUP);
  pinMode (switch2, INPUT_PULLUP);
  pinMode (switch3, INPUT_PULLUP);
  pinMode (switch4, INPUT_PULLUP);
  pinMode (switch5, INPUT_PULLUP);
  pinMode (switch6, INPUT_PULLUP);
  pinMode (switch7, INPUT_PULLUP);
}


void loop() {


  if (digitalRead(switch0) == LOW) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo0.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  else {
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo0.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  if (digitalRead(switch1) == LOW) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  else {
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo1.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  
// read more switches, move more servos


}

You'll see some replication there - maybe figure out how to use arrays to remove some of that.

Probably want to make it smarter, and save the last pos written for each servo (pos0, pos1, etc).
If you read the switch, and it's low, check the posX, and if it's already at 180, don't do anything.
Same for when it's high - check the posX, and if it's already at 0, don't do anything.
Otherwise you may see it jerk back to the opposite side and then rotate right back to where it was, and I doubt that's what you want when the switch hasn't been toggled.

I thank you for that code but I already have arduinos moving servos on my railroad with a similar code, the issue I am having is with telling the arduino which switches control which servos on the pca9685 board. Any ideas?

trainguy56:
I thank you for that code but I already have arduinos moving servos on my railroad with a similar code, the issue I am having is with telling the arduino which switches control which servos on the pca9685 board. Any ideas?

Your code makes the connection between switch and servo. If your code detects that switch 0 is on, then you send the appropriate i2c command to the pca9685 to set the PWM for channel 0. Repeat for 1...15

Adafruit sells a complete board with library and examples here
I don't know if you have something like this or just the chip. Concept is still the same.

Thank you, I think that is the information I was looking for. I thought it was something simple but for the life of me I could not see it.