Using both interrupt, pitfalls

I have a project where i am using both interrupts to count pulses simultaneity. Ik looks that we are missing pulses. Is there a good common practice on working with both interrupts. Such as a maximum pulse that can be counted.

Any suggestions are welcome

Without seeing your code how can we help?

The problem could be any of a dozen things.

…R

Only one interrupt at a time.

What rates are the two interrupting at? And show your code.

Pete

Its rather large so i added the archive.

ArduKMotoStep15ba.zip (7.44 KB)

Where does it use interrupts? The strings "interrupt" and "attach" do not occur anywhere in your code and the setup function doesn't appear to do anything related to interrupts.

Which Arduino does this use?

Pete

I cannot find ISR either.

O stupid me, attached the stepper motor version.
But is should be the DC motor version.

ArduKmotoDC_V05b.zip (5.86 KB)

Any variables, such as coderXrotor and coderYrotor, which are modified in an interrupt routine and used outside the routine must be declared volatile.

Pete

gharryh: O stupid me, attached the stepper motor version. But is should be the DC motor version.

Please attach it as a .ino text file so I don't have to download and extract a ZIP file that could contain anything.

...R

Cant do that as a single file becourse the zip contains several files that complete the sketch, sorry

Robin2: Please attach it as a .ino text file so I don't have to download and extract a ZIP file that could contain anything.

...R

gharryh: Cant do that as a single file becourse the zip contains several files that complete the sketch, sorry

You can attach several files - but I will take a risk and look at your zip file.

Several files suggests it is a complex program. It is usually much easier to test out new stuff in a short experimental program.

Edit to add ... There is so much stuff in that that it would take me a couple of hours to make any sense of it. It's not even immediately obvious where the interrupt code is.

...R

Hi Robin,

The used code is quit simple and they all in the ArduKMoto sketch, the first one.

#define X_MOVE 0        // The X pulses are on INT0
#define Y_MOVE 1        // The Y pulses are on INT1

long coderXrotor        =   0L;         // Number of pulse from 0 degrees
long coderYrotor        =   0L;

In setup these two lines:

 // Tell the Interrupts to do what where
  attachInterrupt(X_MOVE, X_Counter, RISING);
  attachInterrupt(Y_MOVE, Y_Counter, RISING);

And then at the end of that file:

void X_Counter() {                      // After a rising puls on INT0 or INT1 the puls whil be counted
  if (XAxisCW) {
    coderXrotor++;
  }
  else {
    coderXrotor--;
  }
}

void Y_Counter() {
  if (YAxisCW) {
    coderYrotor++;
  }
  else {
    coderYrotor--;
  }
}

You need to declare coderXrotor and coderYrotor as volatile.

Pete

In addition to declaring those variable volatile there is the question of reading them. Because they are long (i.e. 4 bytes) they cannot be read with a single instruction and an interrupt could occur in the middle of reading them. You need to suspend interrupts while you read the variables.

Where is the code that reads those variables ?

In case the variables are required more than once it would be a good idea to read them into another variable for general use.

...R