I am trying to wait for "ok" to continue sending commands to GRBL controller, using this simple function wait_for_ok() as below:
void loop()
{
readingMode = PIND & 0xF0;
switch (readingMode)
{
case B11110000:
break;
//X moves
case B01110000: // X move
if (encoderPos != oldEncPos)
{
if (oldEncPos < encoderPos)
{
Serial.println("G91 G0 X0.01 F20");
oldEncPos = encoderPos;
}
else if (oldEncPos > encoderPos)
{
Serial.println("G91 G0 X-0.01 F20");
oldEncPos = encoderPos;
}
wait_for_ok();
}
break;
case B11110001 // etc.
}
}
The code above works OK when instead of wait_for_ok()) function I use delay(5), but sometimes GRBL controller buffer is overloaded (because responses are ignored this way) so I really need to wait for "ok" acknowledgement before sending the next command.
I found the above function on the forum, but it doesn't work at all.
What am I doing wrong?
I was thinking overnight about it, and I realised that detecting the single character (even CR or LF, whatever is sent by GRBL) should be enough for my purpose...
So, am I right assuming the following code should be sufficient - "while" loop should terminate the moment anything is sent from GRBL:
void wait_for_ok() {
char resp;
while (Serial.available() == 0)
{
while (Serial.available() >0)
{
resp = Serial.read();
; // waiting here for "ok" on serial
}
}
}
Will the above code clear the serial buffer for the next command?
Rather than just guessing what the code should look like, you should REALLY look at the documentation for how Serial.available() and Serial.read() work, and how to use them properly. Look at a few simple examples (there are THOUSANDS of them available), and learn how to use Serial properly. No part of your code is even close to correct, and you apparently have made no effort to learn the correct way to do it.
You are right, my code is not OK and that is exactly the reason why I am asking for help here, in hope I will get the specific answer..
I am NOT a programmer like you guys and I do not have time to learn how to program properly - I only want to make this gadget to work somehow.
And I am sure I am not the only one...