Interrupt not triggering when using while loop

Hi everyone,

I am using an interrupt to turn a flag to True when data is ready from an external ADC. This interrupt is being triggered, however when I add:


to wait for the interrupt to change the flag True, the interrupt function no longer triggers.

Here is my full code:

static volatile bool dataReady = false;

void dataReadyInterrupt()
  dataReady = true;

  ch = 0;
  attachInterrupt(digitalPinToInterrupt(dataReadyPin), dataReadyInterrupt, RISING); 

signed short MCP3464::read()
  // wait for interrupt to turn dataReady True before reading next adc conversion
  dataReady = false;
  // SPI full duplex transfer
  adcReading = (SPI.transfer(0) << 8);
  adcReading += SPI.transfer(0);
  digitalWrite(adcChipSelectPin, HIGH);

  if (ch >= numOfCh)
    ch = 0;

  // Write the new ADC channel to multiplexer
  writeData(&muxRegisters[ch][0], 2);

  // Start the next conversion (single conversion mode)
  writeData(&startConversionFastCmd, 1);
  return adcReading;

Any ideas much appreciated.


try this line : --> while(!dataReady); like this way : -->> while(!dataReady){}

RV Mineirin

What's the point of blocking execution in a while() loop waiting for an interrupt to occur? You might as well just forget the interrupt and poll.

To make sure not to miss the new interrupt, if another task is being completed. Most of the time, the interrupt should trigger before the next read and data should always be ready to read.

Except you have no "another task". The process is stuck in a while() loop doing nothing.

I assume you just mean:




If so, no difference unfortunately.

Currently, yes. When implemented in a bigger project it will be rarely go into the while loop as data should be ready by the time the next read is called

Yes, TBC because the two statements are equivalent.


I think there might be a problem with attaching an interrupt in the constructor. For global objects, the constructor is called before the runtime library is initialized. Move the attachInterrupt() to a 'begin()' method and call that method from setup().

Awesome solve John. This has done the trick. Thank you very much