Servo control with I2C

Hello, I am totally new to arduino but have been doing a lot of reading about using arduinos and servos to control turnouts on a model railroad. I have written this code for an arduino mega hooked up to 25 toggle switches and 6 servos with an I2C connection to 2 unos as slaves with the rest of the servos. My question to you is am I even close to having something that will work? This is what I have written.

[/co#include <Wire.h>
#include <Servo.h>
const int switchPin1 = 1; //active low
const int servoPin1 = 7;

const int switchPin2 = 2; //active low
const int servoPin2 = 8;

const int switchPin3 = 3; //active low
const int servoPin3 = 9;
const int switchPin4 = 4; //active low
const int servoPin4 = 10;

const int switchPin5 = 5; //active low
const int servoPin5 = 11;

const int switchPin6 = 6; //active low
const int servoPin6 = 12;
const int switchPin7 = 13;
const int switchPin8 = 14;
const int switchPin9 = 15;
const int switchPin10 = 16;
const int switchPin11 = 17;
const int switchPin12 = 18;
const int switchPin13 = 19;
const int switchPin14 = 22;
const int switchPin15 = 23;
const int switchPin16 = 24;
const int switchPin17 = 25;
const int switchPin18 = 26;
const int switchPin19 = 27;
const int switchPin20 = 28;
const int switchPin21 = 29;
const int switchPin22 = 30;
const int switchPin23 = 31;
const int switchPin24 = 32;
const int switchPin25 = 33;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;
Servo servo9;
Servo servo10;
Servo servo11;
Servo servo12;
Servo servo13;
Servo servo14;
Servo servo15;
Servo servo16; 
Servo servo17;
Servo servo18;
Servo servo19;
Servo servo20;
Servo servo21;
Servo servo22;
Servo servo23;
Servo servo24;
Servo servo25;





void setup() {
  Wire.begin();

  pinMode(switchPin1, INPUT_PULLUP);
  servo1.attach(servoPin1);

  pinMode(switchPin2, INPUT_PULLUP);
  servo2.attach(servoPin2);

  pinMode(switchPin3, INPUT_PULLUP);
  servo3.attach(servoPin3);

  pinMode(switchPin4, INPUT_PULLUP);
  servo4.attach(servoPin4);

  pinMode(switchPin5, INPUT_PULLUP);
  servo5.attach(servoPin5);

  pinMode(switchPin6, INPUT_PULLUP);
  servo6.attach(servoPin6);

}

void loop() {
  if (digitalRead(switchPin1))
    servo1.write(85);  // HIGH (switch is off)
  else
    servo1.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin2))
    servo2.write(85);  // HIGH (switch is off)
  else
    servo2.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin3))
    servo3.write(85);  // HIGH (switch is off)
  else
    servo3.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin4))
    servo4.write(85);  // HIGH (switch is off)
  else
    servo4.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin5))
    servo5.write(85);  // HIGH (switch is off)
  else
    servo5.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin6))
    servo6.write(85);  // HIGH (switch is off)
  else
    servo6.write(110);  // LOW (switch is on)

  delay(500);  //delay for debounce

  if (digitalRead(switchPin7 == HIGH));
  Wire.beginTransmission(1);
  Wire.write(1); servo7.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin7 ==LOW));
  Wire.beginTransmission(1);
  Wire.write(1); servo7.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin8==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1); servo8.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin8==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo8.write(110); 
  Wire.endTransmission(1);
  if (digitalRead(switchPin9==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo9.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin9==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo9.write(110);
  Wire.endTransmission(1); 
  if (digitalRead(switchPin10==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo10.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin10==LOW));
  Wire.write(1);servo10.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin11==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo11.write(85);
  if (digitalRead(switchPin11==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo11.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin12==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo12.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin12==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo12.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin13==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo13.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin13==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo13.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin14==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo14.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin14==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo14.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin15==HIGH));
  Wire.beginTransmission(1);
  Wire.write(1);servo15.write(85);
  Wire.endTransmission(1);
  if (digitalRead(switchPin15==LOW));
  Wire.beginTransmission(1);
  Wire.write(1);servo15.write(110);
  Wire.endTransmission(1);
  if (digitalRead(switchPin16==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo16.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin16==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo16.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin17==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo17.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin17==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo17.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin18==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo18.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin18==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo18.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin19==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo19.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin19==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo19.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin20==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo20.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin20==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo20.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin21==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo21.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin21==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo21.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin22==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo22.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin22==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo22.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin23==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo23.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin23==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo23.write(110);
  Wire.endTransmission(2);
  if (digitalRead(switchPin24==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo24.write(85);
   Wire.endTransmission(2);
   if (digitalRead(switchPin24==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo24.write(110); 
  Wire.endTransmission(2);
  if (digitalRead(switchPin25==HIGH));
  Wire.beginTransmission(2);
  Wire.write(2);servo25.write(85);
  Wire.endTransmission(2);
  if (digitalRead(switchPin25==LOW));
  Wire.beginTransmission(2);
  Wire.write(2);servo25.write(110);
  Wire.endTransmission(2);[code]

de]

A PCA9685 breakout board (I2C) can drive 16 servos, and has pinheaders for the servo connectors.
Up to 60 boards can be daisy-chained.

25 switches can be matrixed (10 pins).
An Uno/Nano could have done the job.
Leo..

Thanks for your insight,I will have to do some more reading to find out what a matrix is and what a pca9685 breakout board is. As it stands right now with what I have will this function or am I way out in left field?

A PCA9685 board is a hardware solution. A servo position is writen to the PCA9685 once.
Servo timing pulses are now generated by the PCA9685.
The Arduino is not in the loop anymore, and is 100% free to do other things.
The boards also have a dedicated servo power connector.
I gave you the Adafruit link. Lots of info and code there.
PCA9685 clones on ebay.
Not sure what you want to do with the switches.
A matrix is usually for momentary buttons.
Google "Arduino switch matrix".
Leo..

Thank you for that information,have been looking at the pca boards on e bay. Its a steep learning curve for someone who can barely turn the computer on. Does the pca board get programmed prior to connecting to arduino or does the arduino tell it what to do?

trainguy56:
Does the pca board get programmed prior to connecting to arduino or does the arduino tell it what to do?

It's just a slave chip. No programming.
I use these chips to control LED drivers. No experience with PCA9685 and servos.
There are tutorials and examples on the page I linked to.

Leo..

The Arduino tells it what to do and the Arduino only has to tell it once what the servo position is. After that it generates the precision timed pulses that keep the servo at the required position.

If you want to change the position you tell it again what the new position is.

OK I think I understand, I just need to change the address (pca board) that the arduino is sending positions to.
The only other question I have is if the code I have written is going to work?

trainguy56:
The only other question I have is if the code I have written is going to work?

No, but you were going to rewrite it anyway to shorten it and get rid of all the repetition.

AWOL:
No, but you were going to rewrite it anyway to shorten it and get rid of all the repetition.

I keep looking at the code and everything there seems needed to connect the components, I am new at this and may not know shortcuts to accomplish my goal.

  if (digitalRead(switchPin5))
    servo5.write(85);  // HIGH (switch is off)
  else
    servo5.write(110);  // LOW (switch is on)

  if (digitalRead(switchPin6))
    servo6.write(85);  // HIGH (switch is off)
  else
    servo6.write(110);  // LOW (switch is on)

Two almost identical if expressions.
Tell me about the differences.

if (digitalRead(switchPin7 == HIGH));oops. Two boo-boos on one line, repeated over again, no doubt due to cut-and-paste

if (digitalRead(switchPin7 == HIGH)); //???
  Wire.beginTransmission(1);
  Wire.write(1); servo7.write(85);  // ????????
  Wire.endTransmission(1);

The if statement might as well not even be there when it end with a semicolon. Are you expecting servo7, which is connected to a remote Uno via I2C, to move to 85 degrees?

groundFungus:

if (digitalRead(switchPin7 == HIGH)); //???

Wire.beginTransmission(1);
  Wire.write(1); servo7.write(85);  // ????????
  Wire.endTransmission(1);




The if statement might as well not even be there when it end with a semicolon. Are you expecting servo7, which is connected to a remote Uno via I2C, to move to 85 degrees?

Ignore the semicolon for a moment, and take another look at the condition - doesn't look very likely it'll ever be true does it?

Forest for the trees. :wink:

I badly phrased the question - it"ll be true depending on the instantaneous state of the serial interface, pin zero.
Tricky one.

You can do what you want with one Mega by itself. As mentioned by Wawa, the 25 switches could be in a 5x5 matrix. 10 pins max, less if you use a shift register (or port expander) or 2. The Servo reference says that the Mega can handle 48 servos.

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega.

groundFungus:
You can do what you want with one Mega by itself. As mentioned by Wawa, the 25 switches could be in a 5x5 matrix. 10 pins max, less if you use a shift register (or port expander) or 2. The Servo reference says that the Mega can handle 48 servos.

I understand that and it would probably be simpler to code,but the mega is going to be in the control panel close to the switches and the uno's or pca9685's will be out on the train set closer to the servos that they will operate,hopefully, if I can ever figure out how to write a working code.

How far are the Unos/PCAs going to be from the Mega?

There are libraries (Adafruit) for the PCA modules that come with example code. That should make the coding easier.

groundFungus:
How far are the Unos/PCAs going to be from the Mega?

There are libraries (Adafruit) for the PCA modules that come with example code. That should make the coding easier.

The uno,s will be approx. 4 feet away from the mega in a sort of L configuration with the mega at the base of the L. I have looked at some of the examples in various libraries but most seem to involve sweeping back and forth or all moving at once, but all I'm trying to do is to flip a toggle switch and have the servo move to a preset angle and when toggle is switched back have servo go to other preset angle. From what others have said my code is no good but I can not understand why.