Once again, thank you for all the help, especially mromani and paulS.
Thats exactly why it's there, I'm just testing for the moment. If I take it away, there is no way I could read that fast.
I have moved and chaged this:
A_flag = B_flag = false;
To be honest, I not exactly sure where to use volatile, unsigned etc... just going with the flow. I have not yet found a cheat sheet I like that I can refer to.
Removed it.
To deal with A being bigger than B, I put in some (a bit clumsy, alternatives are welcome) code that checks If A is smaller than B and if not, waits a bit before checking again.
These are the results:
1199228;216032;16804
273232;295968;22736
356684;373508;16824
514172;530980;16808
588208;610932;22724
671596;688392;16796
829040;845852;16812
903092;925812;22720
986480;1003276;16796
1143852;1160636;16784
1217820;1240544;22724
1301236;1318040;16804
1458620;1475408;16788
1532576;1555280;22704
1615904;1632696;16792
1773228;1790012;16784
1847200;1869908;22708
1930532;1947328;16796
2087892;2104680;16788
2161848;2184552;22704
Nice smooth output.
In graph form:
The weird thing I'm noticing is that it makes these lovely jumps, always to similar values. [note]I am using a spinning motor, instead of blowing, at a set speed to trigger the sensors to keep it constant.
Blue is clockwise rotation and green is counterclockwise.
Another weird observation - the motor is powered from a bench PSU, set at .4V and rotating at a slowish speed. The clockwise rotation inverts the graph a couple of times, while the counter clockwise rotation is constant. Could this be the motor that has more friction in one direction - or the location of the sensors? I actually thought clockwise would be the most accurate direction.
What can cause the consistant double low reading in both directions - why not a continuos seesaw?
Is the gap (1000-10000 microseconds) big enough to accurately determine direction? Why such a big, consistant difference? Arduino's clockspeed?
Oh and my code:
volatile unsigned long A_time;
unsigned long A_time_set;
volatile unsigned long B_time;
unsigned long B_time_set;
volatile boolean A_flag = false;
volatile boolean B_flag = false;
void A_ISR()
{
A_flag = true;
A_time = micros();
}
void B_ISR()
{
B_flag = true;
B_time = micros();
}
void setup()
{
Serial.begin(115200);
Serial.println("Testing:");
attachInterrupt(0, A_ISR, FALLING); //A getting pulled low on pin 2
attachInterrupt(1, B_ISR, RISING); //B rising on pin 3
}
void loop()
{
noInterrupts();
start: //Test if A is smaller than B, otherwise, wait a bit.
if (A_time > B_time)
{
goto wait; //wait if true
}
else
{
A_time_set = A_time;
B_time_set = B_time;
}
interrupts();
goto cont; //skips the wait routine
wait:
interrupts(); //Start interrupts again, otherwise the Arduino crashes since it keeps on looping
delay(10);
goto start;
cont:
if (A_flag && B_flag)
{
Serial.print(A_time_set);
Serial.print(";");
Serial.print(B_time_set);
Serial.print(";");
Serial.println(B_time_set - A_time_set);
A_flag = B_flag = false;
}
delay(100);
}