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]