Go Down

Topic: Global variable not available in local scope (Read 234 times) previous topic - next topic

MrCamjames

Aug 23, 2019, 01:21 am Last Edit: Aug 23, 2019, 01:27 pm by MrCamjames
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:
Code: [Select]

#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.

larryd

What does this do?

if (commandBuffer < MAX_BUF)


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

MrCamjames

#2
Aug 23, 2019, 01:43 am Last Edit: Aug 23, 2019, 01:45 am by MrCamjames
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

Grumpy_Mike

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.

MrCamjames

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

Grumpy_Mike

#5
Aug 23, 2019, 04:54 am Last Edit: Aug 23, 2019, 04:54 am by Grumpy_Mike
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?

MrCamjames

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

TheMemberFormerlyKnownAsAWOL

#7
Aug 23, 2019, 10:51 am Last Edit: Aug 23, 2019, 11:01 am by TheMemberFormerlyKnownAsAWOL
You need to post all your code.

Quote
When I try to Serial.print it, it just sends nothing
Perhaps there's nothing in it.

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

wildbill

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.

jremington

#10
Aug 24, 2019, 04:04 pm Last Edit: Aug 24, 2019, 04:05 pm by jremington
Please explain what you think this is supposed to do:
Code: [Select]
  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.

Go Up