Hi, I'm working on a self-sailing boat, and I want to be able to operate servos and log the boat position on an SD drive. The problem I am having is that the action of writing to the SD card appears to intefere with the servo timers, causing servo jitter - the more I log, the worse it is. My understanding is that writing to the SD card is quite slow and during the write interrupts are disabled (is this true?) so that the timers no longer work and it messes up the PWM modulation signal to the servos.
I could buffer the writes and only do them occasionally, but I will still always get the problem, and I suspect (because ultimately I only have one power source) that thie jitter is causing spikes which cause infrequent crashes (about every 30 minutes). I know that if I increase the frequency of logging the crashes increase, and vice-versa.
Attached is a simple program which demonstrates the problem, based on the examples from the two libraries, using "sweep" as the basis. I can see a small "hiccup" every time the logging takes place - doing multiple writes in a loop here makes it more obvious, but even a single write is visible. I've also attached a circuit diagram.
I'm at the point of moving the driving of servos to a separate servo controller board, but just wanted to check that I need to do this, and there isn't some obvious software fix that will circumvent this problem.
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
#define SERVO_PIN 5
#define CHIP_SELECT 4
Servo servo;
int pos = 0; // variable to store the servo position
File dataFile;
char dataString[20] = "some data";
void setup() {
servo.attach(SERVO_PIN);
// see if the card is present and can be initialized:
if (!SD.begin(CHIP_SELECT)) {
// don't do anything more:
while (1);
}
}
void loop() {
for (pos = 35; pos <= 125; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 125; pos >= 35; pos -= 1) {
servo.write(pos);
delay(15);
}
for (int i=0; i < 10; i++) {
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
}
}
