This a newbie type question....
I would to put a for loop in the header area, before Setup ();
such as :
for (int i =0; i<numberOfServos; i++) // create servo[i] objects {
VarSpeedServo servo[i];
}
The compiler has a fit with this....If I put it in setup() it compiles, but the results are not global and they need to be.
I want to do this as I can have up to 48 servos that I need to create objects for and was looking for a short hand method.
The program , as written, functions perfectly.
//Turnout Controller for 8 turnouts (could be expanded to 48 with Arduino Mega!)
//Arduino 2009
//uses VarSpeedServo library by Korman found at :http://rapidshare.com/files/425318464/VarSpeedServo.zip
//by David Garrison, May 2011
//This drives up to 48 servos (with the Arduino Mega) it is limited to 8 servos on the Uno or 2009 boards due to lack of I/O pins.
//Used to control model railroad turnouts with inexpensive RC servos. The input is a panel mounted DPDT toggle switch (for each turnout) that is driving (by reversing a +5V and Gnd connection) two bi-color LEDs.
//The amount of movement (in degrees), direction and speed of the servos is programmable.
//The LEDs are placed in the track diagram at the turnout locations and indicate the turnout position by changing color Red -closed , green open for each leg of the turnout.
//The switched +5 and ground is used as the level input.
//A servo centering function is provided to aid in the installation of the servos.
//All- in- all this has been a fun project that was really made very simple by using the VarSpeedServo library...lots going on here but not much code to write!
#include <VarSpeedServo.h>
int numberOfServos=7; //number of servos ( I only need 7 for this project)
VarSpeedServo servo1; // create servo1 object
VarSpeedServo servo2; // create servo2 object
VarSpeedServo servo3; // create servo3 object
VarSpeedServo servo4; // create servo4 object
VarSpeedServo servo5; // create servo5 object
VarSpeedServo servo6; // create servo6 object
VarSpeedServo servo7; // create servo7 object
int portNumber[7] ={14,15,16,17,18,19,9}; //initalize input pin number array
int servoPin[7] ={2,3,4,5,6,7,8}; //intialize output pins servos connect to array
int closepos = 120; //value (in degrees) for servo closed position value
int openpos = 60; //value (in degrees) for servo open postion
int mid = 90; //value (in degrees) for servo mid postion
VarSpeedServo servo[7]= {servo1,servo2,servo3,servo4,servo5,servo6,servo7,}; //intilize servo # array
void setup()
{
for (int i =0; i<numberOfServos; i++) // attach servos to output pins
{
//VarSpeedServo servo[i];
servo[i].attach(servoPin[i]);
}
for (int i=0;i<numberOfServos;i++)
{
pinMode (portNumber[i], INPUT); //set up input pins
}
pinMode(10, INPUT); //set up input pin for centering mode switch
digitalWrite(10, HIGH); //pull up resitor set for center mode switch
}
void loop ()
{
//tests for setup mode
if (digitalRead(10) == 0) centerAll(); //looks for a low on pin 10, provided by a SPST switch to GND
// reads input conditions and moves servos if required
for (int i=0;i<numberOfServos;i++){
if (digitalRead(portNumber[i]) == 0) //read all inputs and outputs servo.move commands
servo[i].slowmove (openpos,20); // second arg is servo speed , 20 is about 3 seconds
else
servo[i].slowmove (closepos,20); // second arg is servo speed , 20 is about 3 seconds
}
}
void centerAll()
//test if switch on input 10 is closed to ground and if so center all servos
//blink on board LED 13 to indicate Center Mode of operation
{
for (int i=0;i<numberOfServos;i++)
{
servo[i].slowmove (mid, 20); //center all servos
}
do
{
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);
}
while (digitalRead(10) == 0); //blink led 13 until setup switch is opened
}
Thanks for being there!
David Garrison