I want to use the external interrupt of my classic nano board. I want to call two functions in 1 external interrupt when it rising and falling.
like :
attachInterrupt(0, function 1, RISING);
attachInterrupt(0, function 2, FALLING);
But seems it is not working.
So my questions:
Is arduino support such calling?
If yes, how to make it work?
I've seen multiple buttons with different resistors on a single interrupt pin, so I believe it is possible. You might have to use the interrupt to call a function and use that function to determine if it is rising or falling and call the appropriate function from there.
In order to respond to a rising interrupt the pin must already be low. If it's low it cannot fall so cannot respond to a falling interrupt. The same applies the other way round for responding to a falling interrupt.
I doubt that, I think that you have seen multiple buttons with resistors connected to an analogue input pin. If you think I'm mistaken please provide an example.
You are probably right, but I'll see if I can find that guide again.
Edit: It was just an analogRead. Would that mean that the buttons won't work when the void loop is going through other parts of the code? I guess that would then rely on the fact that the code is processing frequently enough that a button press is usually caught anyway?
1. If you want FALLING edge triggering mode for INT0-interrupt, the interrupting device (K1) must be connected with a pull-up resistor (Fig-1: external/internal).
2. If you want RISING edge triggering mode for INT0-interrupt, the interrupting device (K1) must be connected with an external pull-down resistor (Fig-2).
Figure-2:
3. Study Fig-1 and Fig-2 and then judge your opinion as to the possibility of implementing your idea.
Hi,
you can do like this:
use the CHANGE option for the interrupt.
In the function called by the interrupt you test the level of the pin that caused the interruption. HIGH or LOW.
Depending on the result you set a flag to 0 or 1 (false or true).
Then in the loop() function you use an if or a switch testing this variable to call function1 or function2.
The real appication is a motor lifter with 2 hall sensors. Using 2 hall sensors can determine the motor running direction(going up or down) and the distance. See pictures:
When going up:
So I think the best way is to acquire either signal's level when the other is falling and rising.
Now seems only one function can be called for one external interrupt.
Do you have any advise?
All an interrupt does is save the current machine state (limited) and jumps to an address (of your function) stored in the interrupt vectors of low memory.
If you want a single interrupt to handle different situations, you have to determine the 'situation' in your code during the interrupt and act accordingly.
Hopefully I didn't misunderstand you, If both signals are going to one pin, how could you tell which is which?
You have to have access to each pin to determine it's state.
Might try looking at code that does rotary switches, as it's the same kind of signal to determine direction and steps.
Might pick up the micro documentation and see if you can find your way though it... The education of reading a datasheet is most valuable...
Those are the same signals as you get from a rotary encoder. There are libraries for encoders, find one and use it. There are also lots of topics in the forum about them, do some searching.
While it doesn't solve your problem you might find this helpful:
I try to learn by helping others when I am between my own projects. I seek out posts with zero replies so that I can try to work with someone through a problem. I didn't know much about this topic, to be honest.
void setup()
{
pinMode(2, INPUT);
attachInterrupt(0, doInterrupt, CHANGE);
}
void loop()
{}
void doInterrupt()
{
if (digitalRead(2) == HIGH) // Must have been rising edge interrupt
function_1();
else // Must have been falling edge interrupt
function_2();
}
void function_1()
{}
void function_2()
{}
Both functions operate within the interrupt so want to keep them as quick as possible. Could potentially just set a variable and then call the functions from loop();
2 signals are connected to 2 external interrupt pins on NANO board.
Each pulse needs around 5ms. When motor stop running, these signals stay on high or low (Not firm, maybe both high or both low, or one high one low). I need to check the phase difference to check the motor running direction.
But when it is stopped there is no direction. So do you want a motor stopped function?
If so you can make a note of the millis timer when each interrupt is called and then have a function that checks that the time since the last interrupt call has not exceeded the stopping time of the motor. If it has the motor has stopped.
I can't help thinking this is an X-Y problem (look it up) exactly why are you needing to know about the motor and what do you want to do with this information?