array of servo objects

Hello,

I am trying to make an array of servo objects. Here is the sketch:

/*
              Record and Playback program
 Cappy Jack 10/10/13
 
 editing for turn switch fred
 This program takes the manuel manipulation of 4 potentiometers and moves 4 servo motors. 
 Recording each of them individually while playing back SD files for the other three.
 The record button saves the selected pot values to a SD memory card, reads the SD card for the other three and drives all servos.
 The playback button reads the values from the SD card and drives the servos. 
 The stop_ button halts the record or playback function.
 There are 4 pots selected by 2 switches in a state diagram. one at a  time
 */

#include <SD.h>
#include <Servo.h>

const int pin6 = 6;      //attaches servos to pins 6 & 8
const int pin7 = 7;
const int pin8 = 8;
const int pin9 = 9;

Servo servo0;      // declare objects for servos
Servo servo1;
Servo servo2;
Servo servo3;
const int halt = 0;
const int rec = 2;
const int play = 3;
const int switch1 = 4;  // switches to select which pot to record
const int switch2 = 5;
int pot;
int move_val;
File routine0;          // creates a file object for the SD card
File routine1;
File routine2;
File routine3;

File routine[]= {
  routine0, routine1,routine2,routine3};
int pots[] = {
  0,1,2,3};
Servo servo[]= {
 servo0, servo1,servo2,servo3};

void setup() {
  Serial.begin(9600);      // initialize serial communication
  Serial.print("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());
  servo0.attach(pin6);     // attach pins to the servos 
  servo1.attach(pin7);
  servo2.attach(pin8);
  servo3.attach(pin9);
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(switch1, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(switch2, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(10, OUTPUT);           // SD card on
  SD.begin();                    //Initializes the SD library and card
}   // end of set up

int select_pot () {
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 1))
    pot = pots[0];
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 1))
    pot = pots[1];
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 0))
    pot =  pots[2];
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 0))
    pot = pots[3];
  return pot;
}

int readpot(int pot) {
  move_val = analogRead(pot);  // reads the value of the potentiometer (value between 0 and 1023) 
  move_val = map(move_val, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
  return move_val;
}

void move_servo(int pot,int move_val) {  // function to move the servo   
  servo[pot].write(move_val);
  delay(40);   // waits for the servo to get there
}  

void halt_loop(int pot) {
  if (digitalRead(halt)== 0){
    routine[pot].close();
 //   break;
  }
}

void writesd(int pot){
  routine[pot] = SD.open(routine[pot],FILE_WRITE);
  routine[pot].write(move_val);
  delay(40);
  }

  void record(int pot){      // function to read the pot, move the servos and write to the SD card
    //  SD.remove("routine.txt");
    open routine[pot];
    readpot(pot);
    moveservo(pot);
    writesd(pot);
    halt_loop(pot);
  }

void playback(int pot){
  int value;
  open routine[pot];
  value = routine[pot].read();
  moveservo(pot);
  halt_loop(pot);
}

void switch_pot(){
  switch (pot){
  case 0:
    record(pot);
    playback(1);
    playback(2);
    playback(3);
  case 1:
    record(pot);
    playback(0);
    playback(2);
    playback(3);
  case 2:
    record(pot);
    playback(0);
    playback(1);
    playback(3);    
  case 3:
    record(pot);
    playback(0);
    playback(1);
    playback(2);
  case 4:
    playback(0);
    playback(1);
    playback(2);
    playback(3);
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    select_pot();
    switch_pot();
    Serial.print(digitalRead(rec));
    Serial.println("  rec   ");
  }

  if ((digitalRead(playback)) == 0){
    pot = 4;
    switch_pot();
    Serial.print(digitalRead(playback));
    Serial.println("  play   ");
  }
  Serial.print("  looping ");
  delay(100);
}  // end void loop

and here is the compiler error log:
rec_1_play_3k.ino: In function ‘void writesd(int)’:
rec_1_play_3k:93: error: no matching function for call to ‘SDClass::open(File&, int)’
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:74: note: candidates are: File SDClass::open(const char*, uint8_t)
rec_1_play_3k.ino: In function ‘void record(int)’:
rec_1_play_3k:100: error: ‘open’ was not declared in this scope
rec_1_play_3k:100: error: expected ;' before 'routine' rec_1_play_3k:102: error: 'moveservo' was not declared in this scope rec_1_play_3k.ino: In function 'void playback(int)': rec_1_play_3k:109: error: 'open' was not declared in this scope rec_1_play_3k:109: error: expected ;’ before ‘routine’
rec_1_play_3k:111: error: ‘moveservo’ was not declared in this scope
rec_1_play_3k.ino: In function ‘void loop()’:
rec_1_play_3k:154: error: invalid conversion from ‘void ()(int)’ to ‘uint8_t’
rec_1_play_3k:154: error: initializing argument 1 of ‘int digitalRead(uint8_t)’
rec_1_play_3k:157: error: invalid conversion from 'void (
)(int)’ to ‘uint8_t’
rec_1_play_3k:157: error: initializing argument 1 of ‘int digitalRead(uint8_t)’

Could anyone suggest a fix? Thank you!

Declaring the array content is unnecessary. This:

File routine0;          // creates a file object for the SD card
File routine1;
File routine2;
File routine3;

File routine[]= {
  routine0, routine1,routine2,routine3};

should just be this:

File routine[4];

Same with the servos.

As to your errors, solve them one at a time and recompile. The first one is complaining that you passed a File structure when it was expecting a string with a filename in it.

Thank you for the syntax to create an array of objects…it worked. I am trying now to index into an SD file for both reading and writing. I want to write to a file and read three other files at the same index. Here is my attempt to use the file.position syntax-

/*
              Record and Playback program
 Cappy Jack 10/10/13
 
 editing for turn switch fred
 This program takes the manuel manipulation of 4 potentiometers and moves 4 servo motors. 
 Recording each of them individually while playing back SD files for the other three.
 The record button saves the pot values to a SD memory card and drives the servos.
 The playback button reads the values from the SD card and drives the servos. 
 The stop_ button halts the record or playback function.
 There are 4 pots selected by 2 switches in a state diagram. one at a  time
 */

#include <SD.h>
#include <Servo.h>

const int pin6 = 6;      //attaches servos to pins 6 & 8
const int pin7 = 7;
const int pin8 = 8;
const int pin9 = 9;

Servo servo0[4];      // declare objects for servos
File routine0[4];     // creates a file object for the SD card
char rountine[] ={
  'routin0.txt','routin1.txt','routin2','routin3.txt'};

const int halt = 0;   // buttons in main
const int rec = 2;
const int play = 3;

const int switch1 = 4;  // switches to select which pot to record
const int switch2 = 5;

int pot;
int move_val;

void setup() {
  Serial.begin(9600);      // initialize serial communication
  Serial.print("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());
  servo0[0].attach(pin6);     // attach pins to the servos 
  servo0[1].attach(pin7);
  servo0[2].attach(pin8);
  servo0[3].attach(pin9);
  pinMode(halt, INPUT_PULLUP);   // the halt button
  pinMode(rec, INPUT_PULLUP);    // the record button
  pinMode(play, INPUT_PULLUP);   // the playback button
  pinMode(switch1, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(switch2, INPUT_PULLUP);  // selector for pots 1-4
  pinMode(10, OUTPUT);           // SD card on
  SD.begin();                    //Initializes the SD library and card
}   // end of set up

int select_pot () {
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 1))
    pot = 0;
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 1))
    pot = 1;
  if ((digitalRead(switch1) == 1) && (digitalRead(switch2) == 0))
    pot = 2;
  if ((digitalRead(switch1) == 0) && (digitalRead(switch2) == 0))
    pot = 3;
  return pot;
}

int readpot(int pot) {
  move_val = analogRead(pot);  // reads the value of the potentiometer (value between 0 and 1023) 
  move_val = map(move_val, 0, 1023, 0, 89);  // sets the servo position according to the scaled value
  return move_val;
}

void move_servo(int pot,int move_val) {  // function to move the servo   
  servo0[pot].write(move_val);
  delay(40);   // waits for the servo to get there
}  

void halt_loop(int pot) {
  if (digitalRead(halt)== 0){
    routine0[pot].close();
  }
}

void writesd(int pot, int move_val, int index){
  routine0[pot] = SD.open("routine[pot]",FILE_WRITE);
  routine0[pot].position(index);
  routine0[pot].write(move_val);
  delay(40);
}

void record(int pot, int index){      // function to read the pot, move the servos and write to the SD card
  //  SD.remove("routine.txt");
  readpot(pot);
  move_servo(pot, move_val);
  writesd(pot, move_val, index);
  halt_loop(pot);
}

void playback(int pot, int index){
  int value;
  routine0[pot] = SD.open("routine[pot]");
  routine0[pot].position(index);
  value = routine0[pot].read();
  move_servo(pot,value);
  halt_loop(pot);
}

void switch_pot(){
  switch (pot){
    int index = 0;
  case 0:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 1:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 2:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index); 
    if (digitalRead(halt)== 0){
      break;
    } 
  case 3:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  case 4:
    index = index + 1;
    record(pot, index);
    playback(1, index);
    playback(2, index);
    playback(3, index);
    if (digitalRead(halt)== 0){
      break;
    }
  }
}

void loop() {    // main loop

    if ((digitalRead(rec)) == 0){
    select_pot();
    switch_pot();
    Serial.print(digitalRead(rec));
    Serial.println("  rec   ");
  }

  if ((digitalRead(play)) == 0){
    pot = 4;
    switch_pot();
    Serial.print(digitalRead(play));
    Serial.println("  play   ");
  }
  Serial.print("  looping ");
  delay(100);
}  // end void loop

And here are the error messages from the compiler-
rec_1_play_3m:85: error: no matching function for call to ‘File::position(int&)’
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:43: note: candidates are: uint32_t File::position()
rec_1_play_3m.ino: In function ‘void playback(int, int)’:
rec_1_play_3m:101: error: no matching function for call to ‘File::position(int&)’
C:\Program Files (x86)\Arduino\libraries\SD/SD.h:43: note: candidates are: uint32_t File::position()
rec_1_play_3m.ino: In function ‘void switch_pot()’:
rec_1_play_3m:110: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of ‘int index’
rec_1_play_3m:119: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of ‘int index’
rec_1_play_3m:128: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of ‘int index’
rec_1_play_3m:137: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of ‘int index’
rec_1_play_3m:146: error: jump to case label
rec_1_play_3m:109: error: crosses initialization of ‘int index’

Could you suggest syntax that will work? Thanking you in advance!

char rountine[] ={

Is your spell checker not working? This is NOT the name you use later.

The position() method returns where in the file you are. It doesn't take any arguments. Why are you trying to give it one? You can argue with the compiler all day. In the end, you will lose.

  switch (pot){
    int index = 0;

Get the int out of there. You can't declare variables in a switch statement except in the case blocks, and then only with extreme care.