Hey all,
I have a piece of code here to read in data from an AHRS (sparkfun razor).
char dataset[27]="";
char incoming=-1;
int counta=0;
int count=0;
String StringData="";
int comma1;
int comma2;
int datastart;
int dataend;
String rollstring;
String pitchstring;
String yawstring;
float rollval;
float pitchval;
float yawval;
char dan1='D';
char checker=-1;
int searchcount=0;
float rollval2;
int memavail=0;
void setup()
{
Serial.begin(57600);
Serial1.begin(57600);
}
void loop()
{
// memavail=0;
for (int mastercounter=0;mastercounter<=15;mastercounter++)
{
for(int i=0;i<=30;i++)
{
dataset[i]=0;
}
count=0;
char rollstringchar[8]="";
char pitchstringchar[8]="";
char yawstringchar[8]="";
count=0;
StringData="";
while (incoming!='X')
{
incoming=-1;
if (Serial1.available())
{
incoming=Serial1.read(); // Read a byte of the serial port
dataset[count]=incoming;
count++;
}
//delay(100);
}
StringData=dataset;
// Serial.println(StringData);
datastart=StringData.indexOf(':');
dataend=StringData.indexOf('X');
comma1=StringData.indexOf(',',datastart+1);
comma2=StringData.indexOf(',',comma1+1);
rollstring=StringData.substring(datastart+1,comma1);
pitchstring=StringData.substring(comma1+1,comma2);
yawstring=StringData.substring(comma2+1,dataend);
rollstring.toCharArray(rollstringchar,7);
rollval=atof(rollstringchar);
//rollval2=atof(rollstringchar);
yawstring.toCharArray(yawstringchar,7);
yawval=atof(yawstringchar);
pitchstring.toCharArray(pitchstringchar,7);
pitchval=atof(pitchstringchar);
//memavail=availableMemory();
//Serial.print("Roll Check: ");
//Serial.println(rollstringchar);
Serial.print("Roll: ");
Serial.println(rollval);
Serial.print("Yaw: ");
Serial.println(yawval);
Serial.print("Pitch: ");
Serial.println(pitchval);
Serial.println(mastercounter);
Serial.println(memavail);
Serial.println("---------------------------------");
}
delay(100);
/* for(int i=0;i<=7;i++)
{
rollstringchar[i]=0;
yawstringchar[i]=0;
pitchstringchar[i]=0;
}
count=0;
*/
}
int availableMemory() {
int size = 8000; // Use 2048 with ATmega328
byte *buf;
while ((buf = (byte *) malloc(--size)) == NULL)
;
free(buf);
return size;
}
This reads in the data just fine (I have added some commented out code to try and find out how much ram this was using) the problem is that if it does anything else, such as even a delay(100); after the code has executed things start getting weird. What really happens is that the code executes with non uniform speed and instead of executing the loop 15 times as it is supposed to it will execute 3 or 4. I originally thought that it might be a memory issue, but the code works, just unexpectedly. I might be really off base here but I was beginning to think that once the code finishes and the arduino refreshes it loses synch and has to reestablish it... Is this at all accurate, this is really concerning me because I have to run another similar code to access data from a gps which means that while this isnt running the machine will have no guidance so I would like this time to be as short as possible. Any tips?
Thanks
Dan