struct position {
int x;
int y;
int z;
position(){ 0; 0; 0; }
position(int X,int Y, int Z){ x = X; y = Y; z = Z; }
} CurrentPosition, DefaultPosition;
void GRBLCommand()
{
int XChange = CurrentPosition.x - DefaultPosition.x;
int YChange = CurrentPosition.y - DefaultPosition.y;
int ZChange = CurrentPosition.z - DefaultPosition.z;
if(
(XChange > 10 || XChange < -10) ||
(YChange > 10 || YChange < -10) ||
(ZChange > 10 || ZChange < -10)
)
{
Serial.print("G1 ");
// This can be shortened to a method that takes the Change,Axis letter and returned the string.
if(XChange < -10) Serial.print("X-1000");
if(XChange > 10) Serial.print("X1000");
if(YChange < -10) Serial.print("Y-1000");
if(YChange > 10) Serial.print("Y1000");
if(ZChange < -10) Serial.print("Z-1000");
if(ZChange > 10) Serial.print("Z1000");
Serial.println("F100");
}
else
{
Serial.print("!");
}
}
This is my working code. My original idea was to create method that returned a string that gets printed with Serial.print();
I could not get that going because String kept on locking up my Arduino. Because I can't get strings to work properly I ended up just doing the Serial.print inside the method. (It works but I don't like it cause I am sure the code can look much simpler.)
Any ideas on how I can optimize this method and removing the dependency on Serial?
Thanks for everyone's help up to now.