Hello, I have a problem with my program. I want to control my servo motors with potentiometers and then save that movements on SD card and read it afterwards. That works perfectly when I have only one servo but when I try to do this with two I cant open second file on SD card. Here is the code. Please help if you spot what’s the problem here.
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
// CS pin for SD Card Module
const int chipSelect = 4;
// Analog pin for potentiometer
int analogPin = A0;
int analogPin1 = A3;
// Integer to hold potentiometer value
int val = 0;
int val1 = 0;
// Create a Servo object
String buffer;
String buffer1;
// Create a Servo object
Servo myservo;
Servo myservo1;
int pin_Button = 2;
int pin_Button_State = 0;
int pin_Button_State_Last = 0;
int recording = 0;
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“Initializing SD card…”);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1);
}
Serial.println(“card initialized.”);
SD.remove(“servopos.txt”);
SD.remove(“servoposa.txt”);
// Attach servo on pin 9 to the servo object
myservo.attach(9);
myservo1.attach(6);
}
void loop()
{
pin_Button_State = digitalRead(pin_Button);
if (pin_Button_State != pin_Button_State_Last) {
if (pin_Button_State == HIGH) {
recording++;
Serial.println();
Serial.print(" Buttonpress: ");
Serial.println(recording);
}
else {
}
delay(50);
}
if ( recording == 1) {
Record();
}
else if (recording > 1){
Play();
}
}
void Record()
{
String dataString = “”;
val = map(analogRead(analogPin), 0, 1023, 0, 180);
dataString += String(val);
myservo.write(val);
delay(15);
String dataString1 = “”;
val1 = map(analogRead(analogPin1), 0, 1023, 0, 180);
dataString1 += String(val1);
myservo1.write(val1);
delay(15);
File dataFile = SD.open(“servopos.txt”, FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
Serial.println(dataString);
}
File dataFile1 = SD.open(“servoposa.txt”, FILE_WRITE);
if (dataFile1) {
dataFile1.println(dataString1);
}
else
Serial.println(“error”);
dataFile.close();
dataFile1.close();
}
void Play()
{
File dataFile = SD.open(“servopos.txt”);
File dataFile1 = SD.open(“servoposa.txt”);
// If the file is available, read it
if (dataFile) {
while (dataFile.available()) {
// Write one line to buffer
buffer = dataFile.readStringUntil(’\n’);
// Print to serial monitor
Serial.println(buffer);
// Convert string to integer and position servo
myservo.write(buffer.toInt());
delay(15);
}
}
// If the file is available, read it
if (dataFile1) {
while (dataFile1.available()) {
// Write one line to buffer
buffer1 = dataFile1.readStringUntil(’\n’);
// Print to serial monitor
// Convert string to integer and position servo
myservo1.write(buffer1.toInt());
delay(15);
}
}
dataFile.close();
dataFile1.close();
}
So to clear out I want to record both of my potentiometers every 15ms and save that position on SD card. So if I don’t change value on my pot the position of servo is saved as the same every 15ms until I move my pot again. And when I hit button for the second time I want my servo to read everything from those files at the same time so that motors move exactly as I was controling it in Record mode.
Thanks!