for loop in header area?

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

const int numberOfServos=7;                    //number of servos ( I only need 7 for this project)  
VarSpeedServo servos[numberOfServos};    // create servo objects
int portNumber[numberOfServos] = {14,15,16,17,18,19,9};  //initalize input pin number array
int servoPin[numberOfServos] = {2,3,4,5,6,7,8};  //intialize output pins servos connect to array

void setup()
{
for (int i = 0; i < numberOfServos; i++)
    servos[i].attach(servoPin[i]);
}

You can't put code outside of a method, other than variable declarations. Other than a slight typo, John Wasser's code is the way to do this (VarSpeedServo servos[numberOfServos}; should be VarSpeedServo servos[numberOfServos]:wink:

Well...knock me down ...

That works!-----VarSpeedServo servo[numberOfServos]; I corrected a typo I had made "servo" instead of "servos" and made the other changes John Wasser had suggested.

Can you explain what the compiler does with this statement? I understand the creat object part , but the array part is not clear to me.

Here is the new code:

[quote]
[color=#7E7E7E]//Turnout Controller for 8 turnouts (could be expanded to 48 with Arduino Mega!) [/color]
[color=#7E7E7E]//Arduino 2009[/color]
[color=#7E7E7E]//uses VarSpeedServo library by Korman found at :http://rapidshare.com/files/425318464/VarSpeedServo.zip[/color]
[color=#7E7E7E]//by David Garrison, May 2011[/color]
[color=#7E7E7E]//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.[/color]
[color=#7E7E7E]//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.[/color]
[color=#7E7E7E]//The amount of movement (in degrees), direction and speed of the servos is programmable.[/color]
[color=#7E7E7E]//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.[/color]
[color=#7E7E7E]//The switched +5 and ground is used as the level input. [/color]
[color=#7E7E7E]//A servo centering function is provided to aid in the installation of the servos. [/color]
[color=#7E7E7E]//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![/color]



#include <[color=#CC6600]VarSpeedServo[/color].h>


const [color=#CC6600]int[/color] numberOfServos=7;                    [color=#7E7E7E]//number of servos ( I only need 7 for this project)  [/color]

[color=#CC6600]VarSpeedServo[/color] servo[numberOfServos];   [color=#7E7E7E]//create servo objects [/color]
[color=#CC6600]int[/color] portNumber[numberOfServos] ={14,15,16,17,18,19,9};  [color=#7E7E7E]//initalize input pin numbers[/color]
[color=#CC6600]int[/color] servoPin[numberOfServos] ={2,3,4,5,6,7,8};  [color=#7E7E7E]//intialize output pins servos connect to[/color]
[color=#CC6600]int[/color] closepos = 120;   [color=#7E7E7E]//value (in degrees) for servo closed position value[/color]
[color=#CC6600]int[/color] openpos =  60;    [color=#7E7E7E]//value (in degrees) for servo open postion[/color]
[color=#CC6600]int[/color] mid = 90;         [color=#7E7E7E]//value (in degrees) for servo mid postion[/color]

  

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{

    [color=#CC6600]for[/color] ([color=#CC6600]int[/color] i =0; i<numberOfServos; i++)                [color=#7E7E7E]// attach servos to output pins[/color]
    {
      servo[i].[color=#CC6600]attach[/color](servoPin[i]);
    }
    
   [color=#CC6600]for[/color] ([color=#CC6600]int[/color] i=0;i<numberOfServos;i++)
    {
      [color=#CC6600]pinMode[/color] (portNumber[i], [color=#006699]INPUT[/color]);      [color=#7E7E7E]//set up input pins [/color]
    }
  
  [color=#CC6600]pinMode[/color](10, [color=#006699]INPUT[/color]);      [color=#7E7E7E]//set up input pin for centering mode switch[/color]
  [color=#CC6600]digitalWrite[/color](10, [color=#006699]HIGH[/color]);   [color=#7E7E7E]//pull up resitor set for center mode switch[/color]

}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color] ()
{
  [color=#7E7E7E]//tests for setup mode[/color]
  [color=#CC6600]if[/color] ([color=#CC6600]digitalRead[/color](10) == 0)    centerAll();   [color=#7E7E7E]//looks for a low on pin 10, provided by a SPST switch to GND [/color]

  [color=#7E7E7E]// reads input conditions and moves servos if required[/color]

  [color=#CC6600]for[/color] ([color=#CC6600]int[/color] i=0;i<numberOfServos;i++){
    [color=#CC6600]if[/color] ([color=#CC6600]digitalRead[/color](portNumber[i]) == 0)    [color=#7E7E7E]//read all inputs and outputs servo.move commands[/color]

      servo[i].[color=#CC6600]slowmove[/color] (openpos,20);    [color=#7E7E7E]// second arg is servo speed , 20 is about 3 seconds[/color]

    [color=#CC6600]else[/color] 

      servo[i].[color=#CC6600]slowmove[/color] (closepos,20);   [color=#7E7E7E]// second arg is servo speed , 20 is about 3 seconds[/color]
  }
}


[color=#CC6600]void[/color] centerAll()

[color=#7E7E7E]//test if switch on input 10 is closed to ground and if so center all servos[/color]
[color=#7E7E7E]//blink on board LED 13 to indicate Center Mode of operation[/color]
{
  [color=#CC6600]for[/color] ([color=#CC6600]int[/color] i=0;i<numberOfServos;i++)
    {
      servo[i].[color=#CC6600]slowmove[/color] (mid, 20);     [color=#7E7E7E]//center all servos  [/color]
    }
  [color=#CC6600]do[/color]
  {
    [color=#CC6600]digitalWrite[/color](13,[color=#006699]HIGH[/color]);
    [color=#CC6600]delay[/color](300);
    [color=#CC6600]digitalWrite[/color](13,[color=#006699]LOW[/color]);
    [color=#CC6600]delay[/color](300);
  } 
  [color=#CC6600]while[/color] ([color=#CC6600]digitalRead[/color](10) == 0);     [color=#7E7E7E]//blink led 13  until setup switch is opened[/color]
}


















I really appreciate the responses to my questions!!!!!!!!!!!!!!!!

David Garrison.

sorry about the previous post.. I used the "Copy for Forum" in the Edit menu of the IDE...it produces all that color info , but the forum seems to ignore it...

Here is the code without that stuff:

#include <VarSpeedServo.h>


const int numberOfServos=7;                    //number of servos ( I only need 7 for this project)  

VarSpeedServo servo[numberOfServos];   //create servo objects 
int portNumber[numberOfServos] ={14,15,16,17,18,19,9};  //initalize input pin numbers
int servoPin[numberOfServos] ={2,3,4,5,6,7,8};  //intialize output pins servos connect to
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

  

void setup()
{

    for (int i =0; i<numberOfServos; i++)                // attach servos to output pins
    {
      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
}

Best to all!

David Garrison

Why not?

for (int i =0; i<numberOfServos; i++)                // attach servos to output pins
    {
      servo[i].attach(servoPin[i]);
      pinMode (portNumber[i], INPUT);      //set up input pins 
    }

sierrasmith71:
VarSpeedServo servo[numberOfServos];

Can you explain what the compiler does with this statement? I understand the creat object part , but the array part is not clear to me.

A C++ 'object' is just a data structure that has special functions associated with it. Like any other data structure you can create an array of them by saying how many elements the array has. The compiler will set aside room for the data, like any other global array, and call the constructor method (function) for each of them before your program starts. The constructor, written by the person who defined the object, initializes the object to make it ready to use.

I'm not sure how a global array of objects would be created if the object creation required data. For example if you had to provide the pin number for the servo when you created the servo object. That would require some fancy initializer syntax I don't know offhand. Luckily that step can happen at run time.

Thanks John, your answer makes sense and cleared up the mystery!

Aslo thanks to AWOL for the suggestion to tighten the code even more..yup, that occured to me, but I never did get around to combining the two for loops, until this morning.

Finished(?) code:

//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 (and help provided by Arduino Forum members!)...lots going on here but not much code to write!



#include <VarSpeedServo.h>


const int numberOfServos=7;                    //number of servos ( I only need 7 for this project)  

VarSpeedServo servo[numberOfServos];   //create servo objects 
int portNumber[numberOfServos] ={14,15,16,17,18,19,9};  //initalize input pin numbers
int servoPin[numberOfServos] ={2,3,4,5,6,7,8};  //intialize output pins servos connect to
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

  

void setup()
{

    for (int i =0; i<numberOfServos; i++)               
    {
      servo[i].attach(servoPin[i]);        // attach servos to output pins
      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
}

Many Thanks To All!!!!!!!!!!

David Garrison