Read and write integers to a SD card.

Hello! I am using an Arduino Uno and an Adafruit SD card shield to read 2 potentiometers, write the values (0- 1023) to the SD card and drive 2 servos. This I can do. I also want to read the values from the SD card and drive the servos. The write works but the read does not. Here are the record and playback functions:

void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // delay for the amount of time we want between readings
    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023) 
    routine.print(readpot1);      // write to the SD card
    readpot2 = analogRead(pot2);
    routine.print(readpot2);
    move_servo(readpot1,readpot2);   //  move the servos
    delay(10);
    if (digitalRead(halt)== 0){       //  halt button push escapes the loop
      routine.close();               //  write to and close the SD card
      return;
    }
  }  
}

void playback() {      // function to read SD card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {    // read until the end of the file
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); // delay for the amount of time we want between readings
    value1 = routine.read();       // read the SD card file
    value2 = routine.read();
    move_servo(value1,value2);     // move the servos
    halt_loop();                   // halt function to escape the loop
  }  
}

The values that the routine.read() returns are not correct. They do not drive the servos either. I would appreciate any suggestions to write and read integers to the SD card. Thanking you in advance!
Regards,
Cappy Jack

    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023) 
    routine.print(readpot1);      // write to the SD card
    readpot2 = analogRead(pot2);
    routine.print(readpot2);

The print() method converts the values to strings and writes the strings to the file. You are not putting a separator between the values, so you haven't a hope in hell of reading them correctly.

    value1 = routine.read();       // read the SD card file
    value2 = routine.read();

The read() method reads ONE character, or byte, from the card.

If you change the write code to use write(), instead of print(), to write binary data, you will need to write the high order byte and the low order byte for each value. Then, you'll need to read 4 bytes and combine them properly to get the two ints.

Hello, Thank you for the suggestion. I found a simple way to deal with it and it works. Here are the record and playback functions:

void move_servo(int read1,int read2) {  // function to move the servos  
  servo1.write(read1);                  
  servo2.write(read2);
  delay(15);   // waits for the servo to get there
 }

void record(){      // function to read the pots, move the servos and write to the SD card
  int readpot1, readpot2;
  routine = SD.open("routine.txt", FILE_WRITE);
  while(1) {       // end less loop
    readpot1 = analogRead(pot1);  // reads the value of the potentiometer (value between 0 and 1023) 
    readpot1 = map(readpot1, 0, 1023, 0, 89);  // sets the servo position according to the scaled value 
    routine.write(readpot1);
    readpot2 = analogRead(pot2);
    readpot2 = map(readpot2, 0, 1023, 0, 89);
    routine.write(readpot2);
    move_servo(readpot1,readpot2);
    if (digitalRead(halt)== 0){
      routine.close();
      return;
    }
  }  
}

void playback() {      // function to read Sd card and move the servos
  int value1, value2;
  routine = SD.open("routine.txt");
  while (routine.available()) {
    value1 = routine.read();
    value2 = routine.read();
    value1 = int(value1);
    value2 = int(value2);
    move_servo(value1,value2);
    halt_loop();
  }  
}

I changed the value of the integer I am writing from the value of the potentiometer (0 - 1023) to the value scaled for the servo (0 -180). This way I could use the one byte read to get the true value. (one byte = 0 - 255 chars) Then I cast it as an integer with int(). Voila! It works except for one thing. When I open the file with notepad, I see only gibberish...no numbers! I was hoping to be able to tweak the numbers to improve on the motions a manually operated pot would produce. Have you any suggestions to change the gibberish back to numbers?

Thank you again for your help!

Regards,
Cappy Jack

Then I cast it as an integer with int().

Why? The write() method expects a byte, not an int.

When I open the file with notepad, I see only gibberish...no numbers!

Well, duh! You are writing a binary file. Expecting a text editor to convert it to text is unrealistic.