Hello,
I want to determine a direction with two slightly shifted high/low signals S1, S2 (hall sensors)
and give a pwm output.
Now I have following problem: the measurment of the direction is always correct until I write a PWM output on another pin. Then the measurment is sometimes wrong.
If I write the PWM with a second Arduino, the measurement of the first Arduino is always correct.
Now to the code:
So if S2 jumps to HIGH and S1 is already HIGH,
or if S2 jumps to LOW and S1 is already LOW -> forward motion
attachInterrupt(digitalPinToInterrupt(S2), check_state, CHANGE);
void check_state()
{
tFlag = true;
state1 = digitalRead(S1); // volatile variables
state2 = digitalRead(S2);
}
if tFlag == true
I call in the loop function:
void check() {
if ( state1 == state2 && state2 != state2_temp)
{
// forward motion
// measure something on each change of signal 2 when forward motion
{
}
which works perfectly fine till I analogWrite a PWM in the loop function, then I get the forward motion sometimes wrong.
I would guess that writing the PWM takes some time and when it performs digitalRead() the signal already changed again?
Maybe someone has a clue, or at least a better idea for the code?
Thanks!
EDIT: I just found out it works fine if I use PIN 18 (TX1) and 21 (SCL) for S1 and S2 on a Arduino Mega 2560 but not with others Pins (e.g. 18 and 20) and I have the same problem when I use a Arduino nano?