main.cpp (1.7 KB)
Hi!
New to Arduino, I want to build my own interrupt service for an Arduino Due (SAM38XE)
Since I am counting interrupts from an encoder with a list of other functions and
using attachInterrupt() seems to slow for the task.
I assume that I need to setup registers for interrupt handling and write an ISR routine,
so I wrote the code accordingly.
Buuuut I am quite lost.
It will tell you things like how to post your code. Many members use mobile devices and they will not handle a .ino file, although basically it is only text. That is why the forum has code tags.
Note all variables used inside and outside an interrupt must be declared as volatile.
What is an interrupt service?
I am assuming that because you mentioned an encoder you need to decode this, it is normal to use a state machine to do this.
Do you mean "too slow" to operate, or too much to set up?
Thanks man. hm ok let me put it like this: The encoder is wired towards an optocoupler and then to arduino. When Arduino picks up a pulse from the optocoupler it handles it as an interrupt. The ISR for this is just an adding counter. With attachInterrupt() it works fine on it's own, but when put together with all other functions it counts all to many interrupts. So I assume that it is to slow to operate since it's built for a very general purpose and the extra pulses must appear from the function being inefficient?
Sorry words do not cut it in electronics. The words "wired towards" convey nothing. As you are a beginner you could be making all sorts of mistakes in your wiring. We need to see a schematic to see exactly what you have.
Do you have difficulty distinguishing the words "to" and "too"? It makes an big difference to the meaning that comes from your words.
No you can not conclude that from your observations, in fact exactly the opposite is true. I would say your interrupts are working so fast that they are counting way more pulses from your encoder than you expect from just turning the encoder slowly.
That is why I said that :-
That is a natural way of suppressing false values from contact bounce. This is what a state machine looks like for a rotary encoder.
Why are you doing this? Surly there is no need as they are setup already to a suitable default.
What ever the result of the if statement it always does the same thing, which is nothing.
I also have no idea what your interrupt routine is actually doing in trying to read your encoder. At the very least if you are not using a state machine you need to generate an interrupt from one encoder signal and in that interrupt look at the state of the other encoder signal.
The only thing that attachInterrupt() does is setting up some registers related to the desired interrupt so when a pin changes the result is an interrupt and the respective ISR is called. And usually it's only called once in setup().
If you post the problematic code that triggered the need to start digging into interrupts, somebody might point you to mistakes in your code.
Thank you, I will look into that more before I make another statement:) .
The encoder is built into the motor, drylin® E stepper motor with encoder, NEMA 17. I drew a simple scheme of how my construction is, besides from that for handling the interrupts I just use arduino's attachInterrupt (interruptpin, function, FALLING)
function(){
counter++;
}
Still doesn't help much. Why are you using opto couplers, you do know these are slow, and the schematic does not show how the encoder is going to power the LEDs in the optos.
You show one wire from the Arduino to the opto couplers. How are you going to get the direction signal to work with only one connection.
I think you need to search the net for an example of how encoders work.
The A and /A is powering the LED. I don't have any use of estimating if the motor is going cw or ccw since the important thing is just the correct number of pulses per rotation in this particular project. And yes to estimate direction you have to look at both A and B as stated in the diagram. Yes thinking about it now there is probably a lot of other things taking in consideration before optimizing software.