Global variable not available in local scope

I’m currently building and programming an arduino based CNC plotter from scratch for a school project. I’m encountering a problem in the g-code parsing system where I cannot reference a global variable, commandBuffer, from within a function, parseNumber().

Code:

#define MAX_BUF 64 // maximum number of chars in buffer


#define DEBUG true // debugging variable: compiler optimizes if statements away

char commandBuffer[MAX_BUF]; // where the commands are stores
int commandIndex = 0; // pointer to where we are in the command buffer

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  ready();
}

void ready() {
  commandIndex = 0; // reset command index
  Serial.print(F("> "));
}

void loop() {
  // only read if data is available
  if (Serial.available() > 0) {
    // read incoming byte
    char c = Serial.read();
    // return command to serial for debugging
    Serial.print(c);

    // store byte as long as space is available
    if (commandIndex < MAX_BUF) commandBuffer[commandIndex++] = c;
    if (c == '\n') {
      processCommand();
      ready();
    }
  }
}

int parseNumber(char code) {
  if (DEBUG) Serial.print("Command Buffer: ");
  if (DEBUG) Serial.println(commandBuffer);
  char *point = commandBuffer;
  point += 1;
  if (DEBUG) Serial.println(point);
  int value = atoi(point);
  return value;
}

void processCommand() {
  int commandNumber = parseNumber('G');
  Serial.println(commandNumber);
}

The strangest part is, that when I placed a fixed string into the global scope, and referenced that instead, the code worked perfectly.

Just to clarify, this code would take in a string like “G11” from the serial port, and return the integer 11.

What does this do?

if (commandBuffer < MAX_BUF)

I copied the receiving bit of the code from another person, and I didn't pay too much attention to that line in particular, but it's supposed to avoid and index out of range error. But that isn't the right code for that. It should be the command index that's less than MAX_BUF

Yes you are confusing an array with the index you are using to access the array.

An array is always a fixed size so that line Larry pointed out is a bit meaningless. It is the commandindex that needs checking.

I've changed that line, but my bigger problem still hasn't changed

MrCamjames:
I've changed that line, but my bigger problem still hasn't changed

And what did you change it to?

And what is your bigger problem?

I changed that specific line to

if (commandIndex < MAX_BUF) commandBuffer[commandIndex++] = c;

but my bigger problem is that whenever I try to access the commandBuffer variable from the parseNumber() function. When I try to Serial.print it, it just sends nothing

You need to post all your code.

When I try to Serial.print it, it just sends nothing

Perhaps there’s nothing in it.

The code in my first post is the whole code running on my arduino right now, and it gets filled from the Serial input

MrCamjames:
The code in my first post is the whole code running on my arduino right now, and it gets filled from the Serial input

Well, no it isn't - you fixed the issue larryd pointed out.

If you want effective help, you really need to post the updated sketch every time you change it.

Please explain what you think this is supposed to do:

  char *point = commandBuffer;
  point += 1;
  if (DEBUG) Serial.println(point);

Whatever you think, this won't work as you expect unless commandBuffer is a properly terminated C-string. Read up on them here, among other places.