I'm trying to read and convert the phase difference to a voltage, between two square input signals at 40kHz into the arduino, I also want to see if the phase difference is leading or lagging. I'm not sure what I'm doing wrong, I've been told the timing functions are too slow at 40kHz, here is my code for doing so:
(2) What you can do with attachInterrupt(...) depends upon what Arduino you have. What Arduino do you have?
(3) To post code and/or error messages:
Use CTRL-T in the Arduino IDE to autoformat your complete code.
Paste the complete autoformatted code between code tags (the </> button)
so that we can easily see and deal with your code.
Paste the complete error message between code tags (the </> button)
so that we can easily see and deal with your messages.
If you already posted without code tags, you may add the code tags by
editing your post. Do not change your existing posts in any other way.
You may make additional posts as needed.
Please provide links to any libraries that are used
(look for statements in your code that look like #include ). Many libraries
are named the same but have different contents.
Before posting again, you should read the three locked topics by Nick Gammon near the top of the Programming Questions forum, and any links to which these posts point.
If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.
(4) I am guessing that when you wrote:
if(phase1 & phase2 ==true)
you really meant
if(phase1 && phase2)
because there is no need to compare to true, and there is a big difference between & and &&.
besides the poor formatting, many things are creating problems...
1/ in your ISR your shared variables should be volatile (and protected against interrupts when you use them in the loop esp. if not 1 byte long)
2/ what do you think happens when you do pulseIn() inside ISR1 and the other pulse is starting?
3/ what do you need pulseIn() for anyway?
4/ if(phase1 & phase2 ==true) not really a problem in your case but better use && for boolean arithmetic (& = bitwise)
5/ analogWrite(A0, v); what Arduino do you have? The analogWrite function has nothing to do with the analog pins or the analogRead function. When used with the right pins the value you pass is the PWM duty cycle (0 to 255)
6/ byte t1, t2, t --> byte, seriously?
7/ byte v = (5*t)/1000000; would suggest to think about how the formula is calculated when t is a byte
8/ t=t2-t1; with bytes (unsigned) that will lead to interesting values esp if t1 is larger than t2
I'm sure there are many more comments to make...
would suggest you think about what is a "phase" and a square wave, what you need to capture, and what type of data is large enough to represent that
pulseIn() is the wrong approach, as it measures a pulse length.
You want to capture the times of the two rising edges. The function microseconds() can do that, but it has only 4 microseconds resolution. Also, the interrupt takes a variable amount of time to be acknowledged and executed.
A better approach would be to have Timer1 running, say at 1 microsecond per tick, and capture the timer value when the interrupt occurs.
See the data sheet and this excellent tutorial for how to do the timing with hardware, using Timer1 capture mode.