Ok, so I've really appreciated everyone's advice on this forum for simplifying code, making it more efficient, etc... So far, I've removed 3000 bytes from my autopilot base code and counting.
Here is some code which reads an incoming data stream from the IMU via the Serial port. It starts with the '*' character and ends with '#'. Is there a way I can make this simpler/faster?
float Plane::current(byte input)
{
char x;
char inData[512];
String inDataString = "*";
while (Serial.available() == 0) { }
delay(0.5); // need delay to prevent gibberish
while (1) { if(Serial.read() == '*') { break; } }
while (1) { /// if statement used to be below...
x = Serial.read();
inDataString.concat(x);
if (x == '#') {break;}
}
inDataString.toCharArray(inData, 512);
int returnVal = sscanf(inData, "*%[^,],%[^,],%[^,],%c,%c,%[^,],%[^,],%[^,]#", &initRollReading, &initPitchReading, &initYawReading, &rollDirection, &pitchDirection, &initRollSpeed, &initPitchSpeed, &initAltitude);
if (input == ROLL) {
rollReading = atof(*&initRollReading);
return rollReading;
}
else if (input == PITCH) {
pitchReading = atof(*&initPitchReading);
return pitchReading;
}
else if (input == YAW) {
yawReading = atof(*&initYawReading);
return yawReading;
}
else if (input == ROLLSPEED) {
rollSpeed = atof(*&initRollSpeed);
return rollSpeed;
}
else if (input == PITCHSPEED) {
pitchSpeed = atof(*&initPitchSpeed);
return pitchSpeed;
}
else if (input == ALT) {
alt = atof(*&initAltitude);
return alt;
}
else if (input == SPEED) {
currentSpeed = atof(*&initSpeed);
return currentSpeed;
}
}
Thanks in advance,
Zachary