Hi all, I seem to have reached an impasse code wise.
My desire is to load code onto an micro SD card and then have that code run a multitude of different devices. I have been able to get the SD card to work and a shift register to work separately, and have even been able to run several servos based off of code read off the SD card when the servos are connected to the arduino directly and not the shift register.
When i include the shift register i come into problems. My guess is that my attempts to deselect the sd card by pulling its CS high is not working. Would you wise programming sages, be willing to take a look at a young fools code and perhaps make some suggestions?
#include <SD.h>
File myFile;
char code;
int h = 2;
char buffer[5];
int b = 0;
long data[5];
int d = 0;
int pos = 0;
long duration(long start)
{
long now = millis();
long interval = now - start;
return interval;
}
void setup()
{
//Serial.begin(9600);
pinMode(11,OUTPUT);
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);
pinMode(10,OUTPUT);
digitalWrite(2,HIGH);
while(!SD.begin(10))
{
}
myFile = SD.open("test2.txt");
while(myFile.available())
{
while(d < h)
{
code = myFile.read();
if(code != '\r')
{
buffer[b] = code;
b++;
}
else
{
buffer[b] = '\0';
data[d] = atol(buffer);
b = 0;
d++;
}
}
long servo = data[0];
long angle = data[1];
//Serial.println(servo);
//Serial.println(angle);
pos = myFile.position();
myFile.close();
digitalWrite(10,HIGH);
for(int i = 0; i < 20; i++)
{
digitalWrite(2,LOW);
shiftOut(11,13,MSBFIRST,servo);
digitalWrite(2,HIGH);
long starttime = millis();
while(duration(starttime) < angle)
{
}
digitalWrite(2,LOW);
shiftOut(11,13,MSBFIRST,0);
digitalWrite(2,HIGH);
starttime = millis();
while(duration(starttime) < 25 - angle)
{
}
}
digitalWrite(10,LOW);
myFile = SD.open("test2.txt");
myFile.seek(pos);
d = 0;
}
}
void loop()
{
}
the Serial communication data is just for testing whether the SD card part is working right, and is "//" out for the real run.
as additional info - the SD transmits code fine, and when the SD read part is "//" out and the two variables "servo" and "angle" are replaced with numbers like 128 and .5, the servo runs just fine. This is why i believe my attempts deselect the SD are faulty.
Any help would be greatly appreciated.
As a side note - for some reason, the atol does weird things with the numbers read off the card such as rounding (turning 0.5 to 0 and 3.5 to 3, along with some other odd stuff). This isnt a big deal, and doesn't impede the process, and is easy enough to get around, but is there a way to fix this without changing the data type to float??
Any advice would be deeply appreciated!!!
