Interrupt question

Hi.

I have a routine that senses two teeth from a tooth wheel with an ISR.

What the ISR does is just increment a variable inside it.

Then i choose what to do in the main routine according to the variable value incremented inside the ISR.

The problem is that seems like the main routine is not assuming the variable change inside the ISR.

I am not sure if thats the problem.

Can you point me in the right direction?

Thanks.

psg.ino (868 Bytes)

Doesn't count_pulses_2 need to be volatile?- it's the one that's changed inside the isr.

JimboZA:
Doesn't count_pulses_2 need to be volatile?- it's the one that's changed inside the isr.

It does indeed. Ironically he's made count_pulses_3 volatile, even though it's never used anywhere within the sketch.

count_pulses_2 is an integer of two bytes. Every time that variable is checked in a if-statement or filled with a value, the interrupt could come in between checking/reading/writing those two bytes.
You should disable the interrupts and make a copy of that variable and enable the interrupts again. Or use a single byte to count.

Doesn't count_pulses_2 need to be volatile?- it's the one that's changed inside the isr.

Yes indeed, my mistake, but tried declaring volatile count_pulses_2 and it does the same.

You should disable the interrupts and make a copy of that variable and enable the interrupts again. Or use a single byte to count.

If i understand you give to options for solving the problem. Can you please show me how to do at least one? I dont know how on my own.

Thanks a lot everyone for input.

if (count_pulses_1=500)

Oh dear.

What's on pin 2 and what's on pin 3?

    array_psg[count_pulses_1]=array_psg[count_pulses_1]=analogRead(A0);

Why?

Sorry, here is the code with that small mistakes solved:

int count_pulses_1=0;
volatile int count_pulses_2=0;

  
  int array_psg [500]; 



void setup()

{
  
  Serial.begin(9600);
 // while(!Serial){;};

  pinMode(2,INPUT);
   
  digitalWrite(2,LOW);

  attachInterrupt(1,react,RISING);  
}

void loop ()
{

  
if (count_pulses_2==1)
{
 array_psg[count_pulses_1]=analogRead(A0); 

}

if (count_pulses_2==0)
{
 array_psg[count_pulses_1]=0; 
}

if (count_pulses_2==2)
{
  count_pulses_2=0;
}

delayMicroseconds(50);


if (count_pulses_1=500)
{

  for (count_pulses_1=0; count_pulses_1 <=500; count_pulses_1++)
{
  

Serial.println(array_psg[count_pulses_1]);
Serial.println();

}

Serial.println();
count_pulses_1=0;

}
count_pulses_1++;
}
/////////////////////////////////////////////////////////

void react()
{
count_pulses_2++;

}

Oh dear.

What’s on pin 2 and what’s on pin 3?

Not sure if i understand right your question.

pin 3 not connected. Pin 2 connected as input to an ADC converter from an inductive sensor that detects two teeth from a wheel.

The teeth are spaced 60 degrees, the first fires the arduino to start listening what is on pin A0 and save values to an array, and the second should end that action. (thats the function of the ISR routine)

Before and after just fill the array with zeros.

After 500 values print on the screen for further evaluation of my own.

The array is 500 size because i need the most resolution i can in that 60 degree interval for what i am measuring.

here is the code with that small mistakes solved:

if (count_pulses_1=500)
Nope.

  attachInterrupt(1,react,RISING);

Let’s try again - what is connected to pin 3?

for (count_pulses_1=0; count_pulses_1 <=500; count_pulses_1++)

array_psg[500] doesn’t exist, so there’s no point in printing it.

Ohhh.

I understand your point here, wrong pin for interrupt 1 in mega 2560. so i have to declare pin 3instead and i declared pin 2.

Let's try again - what is connected to pin 3?

array_psg[500] doesn't exist, so there's no point in printing it.

Please explain to me why.

serbar10:
Ohhh.

I understand your point here, wrong pin for interrupt 1 in mega 2560. so i have to declare pin 3instead and i declared pin 2.

Please explain to me why.

Your loop is generating indices to your array in the range zero to five hundred inclusive.
Array indices for a 500 element array run 0 to 499.

You should disable the interrupts and make a copy of that variable and enable the interrupts again. Or use a single byte to count.

Can somebody point me in the right direction to solve this?

Thanks.

Can i for axample disable the interrupt in the beginning of the loop with nointerrupt(). and after make a copy of the variable for example: count_pulses_2=x, then use value x in the if statments. And activate the interrupt again with interrupt()?

Just need a push to get this done...

Thanks.

Yes, if you want to use 16-bit integers. If a single byte can be used, you don't need to disable the interrupts.

There are many ways to do this. I'm don't know yet where your sketch is going, so I'm not sure this is the right way to do it, I would probably do it in a different way myself.

voltage int isr_count;

void react()
{
  isr_count++;
}

void loop()
{
  noInterrupts();
  int count_copy = isr_count;
  interrupts();

  if( count_copy == 1)
  {
  }
  else if( count_copy >= 2)
  {
    noInterrupts();
    isr_count = 0;
    interrupts();
  }
}