Global variable not sticking

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.