#include <Servo.h>
const bool HOME = 0; //alleviates having to think about ones and zeros when evaluating logic
const bool THROWN = 1; //
// Lesson 4 - adding servos
struct Turnout {
int servoPin; //where we send the pulse to to position the servo
int buttonPin; //where we get the setting for the servo from
int homePosition; //the angle needed for 'homing' the turnout
int thrownPosition; //the angle needed for 'throwing' the turnout
bool state; //where we store the present state of the servo
Servo ourServo; //a holder for the instance of the servo
};
//Let's manage two turnouts now, by making turnout into an array of turnouts; this array is the heart of how we don't have to duplicate code
Turnout turnouts[] = { //I have expanded to two turnouts, but you can imagine that this could hold 10, or 20, or 30...
{2, 3, 45, 135}, //a servo on pin 2, state determined by pin 3; note that we do not HAVE to initialize state, it gets read anyway, nor ourServo, as it is managed by 'attach'
{4, 5, 35, 145} //a servo on pin 4, state determined by pin 5, with different angles
};
//I have incorporated only two items right now, because there are complexities coming down the road that will change our data structure, but while we talk through what I've done so far, KISS
//and we'll create a constant that tells us automagically how many turnouts are in the array, so we don't have to use fingers and toes
//In order to manage the array now, we'll need to use a for loop; a loop needs to know how often to iterate, so we need a number that tells us how many items are to be managed:
int numTurnouts = sizeof(turnouts) / sizeof(turnouts[0]);
//all this says is "tell me the size of that turnout array(in bytes), and divide that by the size of the first array element, to arrive at the number of turnouts represented in the array
void setup() {
//of course now, we need to handle both turnouts, so we'll add a for loop to work on each one:
for (int tindex = 0; tindex < numTurnouts; tindex ++) { //so, for each turnout in the array, do the following actions
turnouts[tindex].state = digitalRead(turnouts[tindex].buttonPin); //read what position the switch is in
if (turnouts[tindex].state == HOME) turnouts[tindex].ourServo.write(turnouts[tindex].homePosition);
else turnouts[tindex].ourServo.write(turnouts[tindex].thrownPosition);
turnouts[tindex].ourServo.attach(turnouts[tindex].servoPin); //tells ourServo what pin to send signal on
}//end of for loop
}//end of setup()
void loop() {
for (int tindex = 0; tindex < numTurnouts; tindex ++) { //so, for each turnout in the array, do the following actions
turnouts[tindex].state = digitalRead(turnouts[tindex].buttonPin); //find out what position the switch is in
if (turnouts[tindex].state == HOME) turnouts[tindex].ourServo.write(turnouts[tindex].homePosition);
else turnouts[tindex].ourServo.write(turnouts[tindex].thrownPosition);
} //end of for loop
}
//So, how many more lines would it take for this code to manage 48 servos? If you guessed 46, you'd be right. Yes, just add a line to the turnouts array for every servo to be managed.