Code behaves differently in 101 than in Uno

I’m seeing something strange on my Arduion 101. I’m trying to get some 4 axis cnc (g code) software working from Marginally Clever on my 101.

I’m on IDE 1.6.7

I’m having issues with one parsing function:

/**
 * Look for character /code/ in the buffer and read the float that immediately follows it.
 * @return the value found.  If nothing is found, /val/ is returned.
 * @input code the character to look for.
 * @input val the return value if /code/ is not found.
 **/
float parsenumber(char code,float val) {
  char *ptr=buffer;
  while(ptr && *ptr && ptr<buffer+sofar) {
    
    Serial.println("ptr");
    Serial.println(ptr);
    if(*ptr==code) {
      Serial.println("I am returning from parsenumber code");
      Serial.println(atof(ptr+1));
      return atof(ptr+1);
    }
    ptr=strchr(ptr,' ')+1;
    Serial.print(ptr);
  }
  Serial.println("I am returning from parsenumber");
  return val;
}

I’m feeding this command “G00 X90 Y9 Z8 E8 ;” into the function and it hangs in the 101. I’ve spent most of two days trying to figure out why it is hanging. Then I loaded the same code into a Uno and the code works fine. It has previously worked fine on a Mega as well.

Here is the serial output from the 101. It never seems to exit for of the first iteration.

>G00 X90 Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;
Entering Process
Parsing N
ptr
G00 X90 Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;
Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;ptr
Y9 Z8 E8 ; G00 X90 Y9 Z8 E8 ;
Z8 E8 ; G00 X90 Y9 Z8 E8 ;ptr
Z8 E8 ; G00 X90 Y9 Z8 E8 ;
E8 ; G00 X90 Y9 Z8 E8 ;ptr
E8 ; G00 X90 Y9 Z8 E8 ;
; G00 X90 Y9 Z8 E8 ;ptr
; G00 X90 Y9 Z8 E8 ;
G00 X90 Y9 Z8 E8 ;ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
E8 ;ptr
E8 ;
;ptr
;

Here is the serial output from the Uno. It works fine. It runs through multiple iterations fine and the code works.

>G00 X90 Y9 Z8 E8 ;
Entering Process
Parsing N
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
E8 ;ptr
E8 ;
;ptr
;
I am returning from parsenumber
-1
Got out of parsenumber
Entering G Parsing
ptr
G00 X90 Y9 Z8 E8 ;
I am returning from parsenumber code
0.00
0
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
E8 ;ptr
E8 ;
;ptr
;
I am returning from parsenumber
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
E8 ;ptr
E8 ;
I am returning from parsenumber code
8.00
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
I am returning from parsenumber code
8.00
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
I am returning from parsenumber code
9.00
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
I am returning from parsenumber code
90.00
Start >
XXXXXXXXXXYXXZXXXXXXXXYXXXZXXXXXXXYXXXXZXXXXXXYXXXXXZXXXXXYXXXXXXXZXXXYXXXXXXXXZXXYXXXXXXXXXZXYXXXXXXXXXXYZ< Done.
Entering M Parsing
ptr
G00 X90 Y9 Z8 E8 ;
X90 Y9 Z8 E8 ;ptr
X90 Y9 Z8 E8 ;
Y9 Z8 E8 ;ptr
Y9 Z8 E8 ;
Z8 E8 ;ptr
Z8 E8 ;
E8 ;ptr
E8 ;
;ptr
;
I am returning from parsenumber
-1
>

The code is the same. All I’ve done is change the board to the Uno from the 101 and changed the com port. So there is no coding difference. Why would this work on the Uno and Mega, but not the 101?

Thank you for any help.

Hi nathancamp

Can you post your whole arduino 101 sketch that you are using to test the parsenumber? Are you putting the command into the buffer variable and using the code variable are "G" or "G00"?

regards Mike

   ptr=strchr(ptr,' ')+1;

If a space is not found, strchr() returns NULL and ptr will be (char*)1. You don't want that.

Merlin513:
Hi nathancamp

Can you post your whole arduino 101 sketch that you are using to test the parsenumber? Are you putting the command into the buffer variable and using the code variable are “G” or “G00”?

regards
Mike

Thanks for the responses. I did not write the original, and the full code is too long to post. I was trying to modify it to use the AccelStepper library instead of the Adafruit drivers.

The code is available here: https://github.com/MarginallyClever/GcodeCNCDemo

It is the GcodeCNCDemo4AxisV2.ino file. The parameter passed into the function is a char array that in this example was set to “G00 X90 Y9 Z8 E8 ;”.

oqibidipo:
ptr=strchr(ptr,’ ')+1;

If a space is not found, strchr() returns NULL and ptr will be (char*)1. You don’t want that.

You are right about that, and that looks to be where the function is hanging. What I don’t understand is why it hangs on the 101, but not on the Uno or Mega.

Thanks for the input. You have given me a couple of ideas. When I am building the char array at the end I add “buffer[sofar]=0” to terminate the array. I need to dump out on that or else scan one less char when reading the buffer.

nathancamp:
What I don’t understand is why it hangs on the 101, but not on the Uno or Mega.

On AVR memory location 1 happens to be always(*) 0, which ends the while loop.
I have no idea what the 101 has at that address.

(*) Explanation: processor registers r0-r31 are mapped to RAM addresses 0-31 and r1 is used as zero_reg, which holds the value 0 all the time (and is reset to 0 after instructions that may alter its value).