Go Down

Topic: Using both interrupt, pitfalls (Read 2281 times) previous topic - next topic

gharryh

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

Robin2

#1
Feb 16, 2015, 10:17 pm Last Edit: Feb 16, 2015, 10:18 pm by Robin2
Without seeing your code how can we help?

The problem could be any of a dozen things.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

bluejets


el_supremo

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

Pete
Don't send me technical questions via Private Message.

gharryh

Its rather large so i added the archive.

el_supremo

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
Don't send me technical questions via Private Message.

Riva

Don't PM me for help as I will ignore it.

gharryh

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

el_supremo

#8
Feb 17, 2015, 09:07 pm Last Edit: Feb 17, 2015, 09:08 pm by el_supremo
Any variables, such as coderXrotor and coderYrotor, which are modified in an interrupt routine and used outside the routine must be declared volatile.

Pete
Don't send me technical questions via Private Message.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

gharryh

Cant do that as a single file becourse the zip contains several files that complete the sketch, sorry
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


Robin2

#11
Feb 18, 2015, 08:52 pm Last Edit: Feb 18, 2015, 08:57 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

gharryh

#12
Feb 19, 2015, 04:55 pm Last Edit: Feb 19, 2015, 04:56 pm by gharryh
Hi Robin,

The used code is quit simple and they all in the ArduKMoto sketch, the first one.
Code: [Select]
#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:
Code: [Select]
 // 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:
Code: [Select]
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--;
  }
}

el_supremo

You need to declare coderXrotor and coderYrotor as volatile.

Pete
Don't send me technical questions via Private Message.

Robin2

#14
Feb 19, 2015, 07:28 pm Last Edit: Feb 19, 2015, 07:29 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up