Global variable not sticking

This is the second time I’ve run into this issue in my code where I have a global boolean variable that doesn’t want to hold the change. When I check it before and after from within the function loop it changes there, but as soon as the program exits the function it resets back to 0. I’ve included the code snipped below, as the variable isn’t being changed anywhere else in the code, but included the zip file for the (rather large) rest of the code as well. I’m stumped.

One oddity I noticed is if I initialize sensorOrPen as 1 then the code works as intended. If I initialize it as a 0 or declare it without initializing then it will switch to 1 within the function, but reset back to 0 on the next call to the function.

bool sensorOrPen = 1;
enum {x, y} axis;
  
void toggleSensorPen(){
  enum {sensor, pen} sensorOrPenEnum;
  long positions[2];

  Serial.print("Current device is the ");
  Serial.print(sensorOrPen);
  Serial.print(". Sensor is ");
  Serial.print(sensor);
  Serial.print(" Pen is ");
  Serial.print(pen);
  
  if(sensorOrPen == pen) {
    Serial.print(". We are currently using the pen");
    positions[x] = xMotor.currentPosition() - sensorOffset[x]*stepsPerMM;
    positions[y] = yMotor.currentPosition() - sensorOffset[y]*stepsPerMM;
    sensorOrPen = sensor;
  }
  else {
    Serial.print(". We are currently using the sensor");
    positions[x] = xMotor.currentPosition() + sensorOffset[x]*stepsPerMM;
    positions[y] = yMotor.currentPosition() + sensorOffset[y]*stepsPerMM;
    sensorOrPen = pen;
  }

  Serial.print(". New device is the ");
  Serial.println(sensorOrPen);

  xyMotors.moveTo(positions);
  xyMotors.runSpeedToPosition();
}

XY_Plotter.zip (8 KB)

I’ve included the code snipped below

I’ve included a snippet of an answer.

You need to

Hope that helps.

PaulS:
I've included a snippet of an answer.

You need to

Hope that helps.

I don't think I understand

If global variables are mysteriously changing value, start by looking for array bound overflows.

Example from your code snippet:

long positions[2];
. . .
positions[x] = xMotor.currentPosition() - sensorOffset[x]*stepsPerMM;

If subscript x has a value of 2 or more, the array positions[2] will over flow and corrupt other memory space, maybe causing the phenomenon you have experienced.

This is one of those things that works in C but really shouldn't.

  if(sensorOrPen == pen) {

The compiler thinks in types. If you break this down into types then you have...

  if(boolean == enum_I_just_created) {

This shouldn't be a valid expression. How do you compare true and false to an unlimited list of things with unlimited meanings? It "just" works because they're really integers behind the scenes and the compiler lets you get away with this for historical reasons.

Declare the enum type in global scope. sensorOrPen should be that type. Then you can sensibly compare it to values in the list or other variables of the same type.

Or do it the C++ way, where you can 'overload' the equality operator to do whatever you want with your special type you just created. True might be 'equal' to the third item on the list if you want.

6v6gt:
If subscript x has a value of 2 or more, the array positions[2] will over flow and corrupt other memory space, maybe causing the phenomenon you have experienced.

Sorry, I forgot to include that x is an enum for the axis (x or y).

MorganS:
This shouldn't be a valid expression. How do you compare true and false to an unlimited list of things with unlimited meanings? It "just" works because they're really integers behind the scenes and the compiler lets you get away with this for historical reasons.

I tried making sensorOrPen of typdef byte thinking along a similar train of thought to yours, but it gave no change in results.

So the problem is in the code you didn't post.

MorganS:
So the problem is in the code you didn't post.

I included the code in it's entirety in a zip file. Is there a better way to post it?

Is there a better way to post it?

Put it directly in a post using code tags if it will fit. Otherwise attach the code itself, not a zip, to the post

UKHeliBob:
Put it directly in a post using code tags if it will fit. Otherwise attach the code itself, not a zip, to the post

It's somewhat of a big project with about 5 different .cpp files plus header files, that was the reason I went for a zip. Should I just upload each file individually?

noingwhat:
...sensorOrPen...

Only appears in colorSensor.cpp, correct?

This will serve you better...

  • Remove sensorOrPenEnum

  • Add this to the top...

enum sensorOrPen_t : byte {sensor, pen};
  • Change sensorOrPen to the new datatype...
sensorOrPen_t sensorOrPen = pen;

This is the essence of the sensorOrPen code...

//enum sensorOrPen_t : byte {sensor, pen};
//sensorOrPen_t sensorOrPen = pen;

byte sensorOrPen = 1;
  
void toggleSensorPen()
{
  enum {sensor, pen} sensorOrPenEnum;

  Serial.print("Current device is the ");
  Serial.print(sensorOrPen);
  Serial.print(". Sensor is ");
  Serial.print(sensor);
  Serial.print(" Pen is ");
  Serial.print(pen);
  
  if(sensorOrPen == pen) {
    Serial.print(". We are currently using the pen");
    sensorOrPen = sensor;
  }
  else {
    Serial.print(". We are currently using the sensor");
    sensorOrPen = pen;
  }

  Serial.print(". New device is the ");
  Serial.println(sensorOrPen);
}

void setup() 
{
  Serial.begin( 250000 );
}

void loop() 
{
  toggleSensorPen();
  delay(1000);
}

This is the result of running that code...

Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
...

toggleSensorPen works as expected.


I suspect an overrun.

Can you try initializing sensorOrPen as sensor / 0?That's what was causing a bug on my end.

void loop() {
  gcode();
  runPlanner();
  extern void loopingColorSearch();
}

That last line is interesting. What do you think the net affect is?

noingwhat:
Can you try initializing sensorOrPen as sensor / 0?

I can. It is a waste of time. The sensorOrPen code can be debugged with eyeballs and a brain.

That's what was causing a bug on my end.

Oh specious reasoning. Good times are a coming...


byte sensorOrPen = 0;
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
...

byte sensorOrPen;
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
Current device is the 0. Sensor is 0 Pen is 1. We are currently using the sensor. New device is the 1
Current device is the 1. Sensor is 0 Pen is 1. We are currently using the pen. New device is the 0
...

Ohhhhhhh, that's probably the issue! I was just playing around with the code and tried that out but I could see why that could cause the bug.

I'll fix that and test it out when I get home, thank you!