I have a code here where I collect an array of data and do some data analysis with the data. The only thing I'm struggle with is how to exit the ISR after I am done running my code. I am using a pre-scalar of 256 and running the ISR once (1Hz)
const signed int numReadings = 21;
volatile int gauge0[numReadings];
volatile int gauge1[numReadings];
volatile int add[numReadings];
volatile int deltaGAUGE;
volatile int slopeADD;
volatile int mean;
const int A0_pin = PC0;
const int A1_pin = PC1;
const uint16_t t1_load = 0;
const uint16_t t1_comp = 62500;
void setup() {
//Set A0 and A1 pins as inputs
DDRC &= ~(1 << A0_pin);
DDRC &= ~(1 << A1_pin);
//Reset Timer1 Control Reg A
TCCR1A = 0;
//Set CTC mode
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << WGM12);
//Set to prescaler of 1024
TCCR1B |= (1 << CS12);
TCCR1B &= ~(1 << CS11);
TCCR1B &= ~(1 << CS10);
//Reset Timer1 and set compare value
TCNT1 = t1_load;
OCR1A = t1_comp;
//Enable Timer1 compare interrupt
TIMSK1 = (1 << OCIE1A);
//Enable global interrupt
sei();
Serial.begin(9600);
Serial.println(" ");
Serial.println("START");
Serial.println(" ");
}
ISR(TIMER1_COMPA_vect){
Serial.println(" ");
for (unsigned int i=0; i<numReadings; i++)
{
gauge0[i] = (analogRead(A0_pin))*(100.00/1024.0);
gauge1[i] = (analogRead(A1_pin))*(100.00/1024.0);
add[i] = ((gauge0[i]+gauge1[i]));
deltaGAUGE = abs(add[20] - add[0]);
slopeADD = abs((add[20]-add[0])/(20-0));
mean = (add[1]+add[2]+add[3]+add[4]+add[5]+add[6]+add[7]+add[8]+add[9]+add[10]+
add[11]+add[12]+add[13]+add[14]+add[15]+add[16]+add[17]+add[18]+add[19]+add[20])/20;
if(i == 20)
{
Serial.print("This of the delta AVG reading: ");
Serial.println(deltaGAUGE);
Serial.println(" ");
Serial.print("This of the slope reading: ");
Serial.println(slopeADD);
Serial.println(" ");
Serial.print("This of the mean reading: ");
Serial.println(mean);
Serial.println(" ");
Serial.println("END");
}
delay(50); //wait .05 sec
}
}
void loop()
{
}
When I run the circuit, the interrupt keep executing every seconds for an infinite amount of time, while I just want it to run once and exit.
I have tried the exit(0)
command but I get an output that looks like this
START
This of the delta AVG reading: 0
This of t
As you can see, The program does not execute completely. Anyone have any idea how I can fix this, or anyhow I can better my code? thank you.
if
block insideloop()
... set a flag inside the ISR ... set theif
block to execute when the flag is setSerial.print()
and thedelay()
. Let the main program handle the calculations and the output. The ISR only needs to do the measurements.delay
and to some extentSerial.print
require interrupts to work, but all interrupts are disabled while running your ISR code. So the code will just hang.millis()
(ormicros()
, if you need more precision) to time the measurements? That would be way easier