Interrupt not working

The goal of this project is to measure the noise inside transistors. For background, we send the noisy signal into an op-amp which amplifies the voltage by 350. That voltage then passes through a 24 bit ADC and then sends that signal to the Arduino. When we run the code, it does not seem to go through the interrupt, getting stuck and not displaying any values to the serial prompt. So we are wondering if it is a problem with the code and how to fix. We are using an Atmel AT MegaV-2560 and a MAX11040K ADC.

https://datasheets.maximintegrated.com/en/ds/MAX11040K-MAX11060.pdf MAX11040K datasheet

I highlighted the interrupt in red.

#include <SPI.h>
#include <LiquidCrystal.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

const int slaveSelectPin = 53; //SPI setup

const int dataReadyOut = 2;
const int serialClock = 52;
const int MI_SO = 50;
const int MO_SI = 51;
double sumValues = 0; //Current sum of samples (used for RMS calc)
int numSamples = 0; //Current number of samples (used for RMS calc)
double rmsToSend = 0; //Value to send to LCD

//For SPI transfer
int b;
int result;
float v;

//*********************************************************************** Setup ******************************************************t
void setup() {

pinMode(slaveSelectPin, OUTPUT);
digitalWrite(slaveSelectPin, HIGH); //Chip select high immediately
pinMode(dataReadyOut, INPUT_PULLUP); //Active low, adc grounds this when data ready
pinMode(MI_SO, INPUT);
pinMode(MO_SI, OUTPUT);
attachInterrupt(digitalPinToInterrupt(dataReadyOut), adcTransfer, RISING);
spiSetup();
lcd.begin(16, 2);
Serial.begin(115200); //115.2k bits/sec
while(!Serial); //Wait until serial connection correctly established
digitalWrite(48, LOW);
spiSetup();
spiTest();
//digitalWrite(49, HIGH);
}

void spiSetup(){
SPI.begin();
SPI.beginTransaction(SPISettings(24576000,MSBFIRST,SPI_MODE2));
digitalWrite(slaveSelectPin, LOW);
//Leading edge is falling (CPOL 1). Data sampled on leading edge (CPHA 0). Very important!
SPI.transfer(B01100000); //Command byte: choose
SPI.transfer(B00110000); //Data byte: chooses 24-bit mode

//Good place for a read-write test of some sort
digitalWrite(slaveSelectPin, HIGH);
}

//Take this out after checking the SPI communication
void spiTest(){

long int configVal = 0;
digitalWrite(slaveSelectPin, LOW);
digitalWrite(dataReadyOut, LOW);
SPI.transfer(B11100000); //Read
configVal = SPI.transfer(0x00);
digitalWrite(dataReadyOut, HIGH);
Serial.print(configVal);
//digitalWrite(slaveSelectPin, HIGH);

// while(1)
// {
// //digitalWrite(slaveSelectPin, LOW);
// SPI.transfer(B11100000); //Read
// //digitalWrite(slaveSelectPin, HIGH);
// }
// Serial.print(configVal);
// if(configVal == B00110000){ //If configVal is the same as the value we just wrote to it, turn on an LED at pin 49 forever.
// digitalWrite(48,HIGH);
// while(1);
// }

}

//*************************************** ADC READ and SERIAL INTERRUPT ***********************************************************
void adcTransfer(){

digitalWrite(slaveSelectPin, LOW); //Starts data transfer

//Put 3-byte data into Arduino variable “result.” Only reading, so argument does not matter.
int dummy;
dummy = SPI.transfer(B11110000); //Command byte: Choose
b = SPI.transfer(0xff); //Read data. Expects 96 bits, unread ones will be overwritten.
result = b;
result <<= 8;
b = SPI.transfer(0xff);
result |= b;
result <<= 8;
b = SPI.transfer(0xff);
result |= b;

digitalWrite(slaveSelectPin, HIGH); //Deselects the peripheral?
v = (float) result;
//Now do calculations to correct voltage
v = (v/16777216)*5-2.5; //Convert int → decimal voltage ___________ 2^24 = 16777216
v = v/1.3; //Undo gain, gives voltage in mV
Serial.println(v);
//delay(500);

numSamples += 1; //Update global RMS input values
sumValues += v;
b = 0;
result = 0;
v = 0;
}

//********************************************************************* Main Loop *****************************************************
void loop() {

rmsToSend = sqrt(sumValues*sumValues/numSamples); //Calculate RMS value

noInterrupts(); //Disable interrupts
lcd.setCursor(0, 0);
lcd.print("RMS V ");
lcd.setCursor(5, 0);
lcd.print(rmsToSend); //Print RMS value to screen

numSamples = 0; //Reset values
sumValues = 0;
interrupts(); //Enable interrupts
delay(400); //?? ms delay between lcd prints

}

You're doing way too much in the interrupt service routine (ISR). At the head of that list is using Serial.print(). The ISR normally just captures a fleeting signal change and leaves a note, as it were, in a [color=blue]volatile[/color] variable for the main program to pick up later.

Somewhere in loop() you check the note left by the ISR and take action if needed.

The red highlighting is a nice touch but folks prefer your code be inside code tags.

I created a new function named ISR as the trigger for attachInterrupt. I set it up as a flag and made a new if statement inside the loop if flag is set true. I then set flag false inside the same if statement to wait for next interrupt. I also set the flag variable to volatile. I wont be able to test code until tommorow, but are their any spotable errors in the new ISR and if so how can i fix or improve it.

#include <SPI.h>
#include <LiquidCrystal.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

//             	BS  E  D4 D5  D6  D7
  LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

  const int slaveSelectPin = 53;  //SPI setup
 
  const int dataReadyOut = 2;
  const int serialClock = 52;
  const int MI_SO = 50;
  const int MO_SI = 51;
  double sumValues = 0;      	//Current sum of samples (used for RMS calc)
  int numSamples = 0;        	//Current number of samples (used for RMS calc)
  double rmsToSend = 0;      	//Value to send to LCD
  volatile boolean flag = LOW;

//For SPI transfer
  int b;
  int result;
  float v;

//*********************************************************************** Setup ******************************************************t
void setup() {
 
  pinMode(slaveSelectPin, OUTPUT);
  digitalWrite(slaveSelectPin, HIGH);   //Chip select high immediately
  pinMode(dataReadyOut, INPUT_PULLUP);	//Active low, adc grounds this when data ready
  pinMode(MI_SO, INPUT);
  pinMode(MO_SI, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(dataReadyOut), ISR, RISING); 
  spiSetup();
  lcd.begin(16, 2);
  Serial.begin(115200);   //115.2k bits/sec
  while(!Serial);     	//Wait until serial connection correctly established
  digitalWrite(48, LOW);
  spiSetup();
  spiTest();
  //digitalWrite(49, HIGH);
}

void ISR()
{
	flag = !flag;

}
void spiSetup(){
  SPI.begin();
  SPI.beginTransaction(SPISettings(24576000,MSBFIRST,SPI_MODE2));
  digitalWrite(slaveSelectPin, LOW);
	//Leading edge is falling (CPOL 1). Data sampled on leading edge (CPHA 0). Very important!
	//Clock is 1 MHz with div16
   SPI.transfer(B01100000); //Command byte: choose <Config Register>
   SPI.transfer(B00110000); //Data byte: chooses 24-bit mode
   
   
   //Good place for a read-write test of some sort
   digitalWrite(slaveSelectPin, HIGH);
}

//Take this out after checking the SPI communication
void spiTest(){

  long int configVal = 0;
  digitalWrite(slaveSelectPin, LOW);
  digitalWrite(dataReadyOut, LOW);
  SPI.transfer(B11100000);    	//Read <Config Register>
  configVal = SPI.transfer(0x00);
  digitalWrite(dataReadyOut, HIGH);
  Serial.print(configVal);
  //digitalWrite(slaveSelectPin, HIGH);
 

//  while(1)
//  {
//	//digitalWrite(slaveSelectPin, LOW);
//	SPI.transfer(B11100000);    	//Read <Config Register>
//	//digitalWrite(slaveSelectPin, HIGH);
//  }
//  Serial.print(configVal);
//  if(configVal == B00110000){ 	//If configVal is the same as the value we just wrote to it, turn on an LED at pin 49 forever.
//	digitalWrite(48,HIGH);
//   while(1);
//  }
 
 
}


//*************************************** ADC READ and SERIAL INTERRUPT ***********************************************************
/*void adcTransfer(){

  digitalWrite(slaveSelectPin, LOW);  	//Starts data transfer

//Put 3-byte data into Arduino variable "result." Only reading, so argument does not matter.
  int dummy;
  dummy = SPI.transfer(B11110000); //Command byte: Choose <read data register>
  b = SPI.transfer(0xff);       	//Read data. Expects 96 bits, unread ones will be overwritten.
  result = b;
  result <<= 8;
  b = SPI.transfer(0xff);
  result |= b;
  result <<= 8;
  b = SPI.transfer(0xff);
  result |= b;

  digitalWrite(slaveSelectPin, HIGH);   //Deselects the peripheral?
  v = (float) result;
//Now do calculations to correct voltage
  v = (v/16777216)*5-2.5;              	//Convert int -> decimal voltage ___________ 2^24 = 16777216
  v = v/1.3;                       	//Undo gain, gives voltage in mV
  Serial.println(v);
//delay(500);

  numSamples += 1;                  	//Update global RMS input values
  sumValues += v;
  b = 0;
  result = 0;
  v = 0;
}*/


//********************************************************************* Main Loop *****************************************************
void loop() {

if(flag)
{   
  digitalWrite(slaveSelectPin, LOW);  	//Starts data transfer
//Put 3-byte data into Arduino variable "result." Only reading, so argument does not matter.
  int dummy;
  dummy = SPI.transfer(B11110000); //Command byte: Choose <read data register>
  b = SPI.transfer(0xff);       	//Read data. Expects 96 bits, unread ones will be overwritten.
  result = b;
  result <<= 8;
  b = SPI.transfer(0xff);
  result |= b;
  result <<= 8;
  b = SPI.transfer(0xff);
  result |= b;

  digitalWrite(slaveSelectPin, HIGH);   //Deselects the peripheral?
  v = (float) result;
//Now do calculations to correct voltage
  v = (v/16777216)*5-2.5;              	//Convert int -> decimal voltage ___________ 2^24 = 16777216
  v = v/1.3;                       	//Undo gain, gives voltage in mV
  Serial.println(v);
//delay(500);
  flag = false; 
  numSamples += 1;                  	//Update global RMS input values
  sumValues += v;
  b = 0;
  result = 0;
  v = 0;
}

  rmsToSend = sqrt(sumValues*sumValues/numSamples);   //Calculate RMS value

 noInterrupts();                       	//Disable interrupts
  lcd.setCursor(0, 0);                 	 
  lcd.print("RMS   	V  ");
  lcd.setCursor(5, 0);
  lcd.print(rmsToSend);                 	//Print RMS value to screen

  numSamples = 0;                       	//Reset values
  sumValues = 0;
 interrupts();                         	//Enable interrupts
  delay(400);                           	//?? ms delay between lcd prints
 
}

It won't compile as is - BB color tags.

void ISR()

{
flag = !flag;

}

Very interesting way of setting a flag. And a dangerous way: a subsequent interrupt would reset it again!

void ISR()
{
	flag = true;

}

is what you probably mean.

dougp:
You're doing way too much in the interrupt service routine (ISR). At the head of that list is using Serial.print().

I would put the SPI reads on top of the list, or doesn't SPI require interrupts?