Difference in behavior between Sketch and PlatformIO

I have set up an external interrupt on Pin 18 of the Mega 2560 Rev3 and in the ISR, I set a volatile global variable to the current millis(). This works in an Arduino Sketch as shown below:

const byte LED = 40;
const byte BUTTON = 18;
volatile unsigned long automation_sw = 0;

// Interrupt Service Routine (ISR)
void ISR_automation_switch ()
  if (digitalRead (BUTTON) == HIGH) { // switched OFF
    digitalWrite (LED, LOW);
    automation_sw = 0;
  else { // switched on
    digitalWrite (LED, HIGH);
    automation_sw = millis();

}  // end of switchPressed

void setup ()
  Serial.begin( 9600 );
  pinMode (LED, OUTPUT);  // so we can update the LED
  //digitalWrite (BUTTON, HIGH);  // internal pull-up resistor

  attachInterrupt (digitalPinToInterrupt (BUTTON), ISR_automation_switch, CHANGE);  // attach interrupt handler
}  // end of setup

void loop ()
  // loop doing nothing 

  Serial.print(millis() - automation_sw, DEC);
  Serial.println(automation_sw, DEC);

However, when I run the exact same code as part of a bigger program in PlatformIO, the value of automation_sw stays at 0 all the time.

I initially suspected debouncing of the switch, hence I cross-checked by correcting for it. However, in the second case, the value of automation_sw is always 0. This is quite perplexing and I'd like to know if I'm missing something here.


You haven't posted the code that has the problem.

The problematic code is exactly the same. I added prints in both cases of the ISR to check the behavior. Whenever the switch is toggled, the ISR is fired correctly and a call counter is incremented as below:

void ISR_automation_switch()
   if (digitalRead(AUTO_SWITCH_PIN) == HIGH) { // Automation switched OFF
     auto_sw_timeout = 0;
     automation_state = OFF;
     digitalWrite(AUTO_STATUS_PIN, LOW);
     Serial.print(" ");
     Serial.println(count1, DEC);
   else { // Automation switched on
     auto_sw_timeout = millis();
     automation_state = ON;
     digitalWrite (AUTO_STATUS_PIN, HIGH);

     Serial.print(" ");
     Serial.println(count2, DEC);


However the value of auto_sw_timeout does not get updated and stays 0. I am unable to explain this behavior..