Hi! I'm pretty much a beginner at this, so I apologize for the poor code and circuit diagram, but i'm at my wit's end with trying to use the pulseIn() function. For reference, i'm using an Arduino Uno.
I have two red laser dot diodes pointed at each other to induce a voltage in the unpowered one, which activates an LM339 voltage comparator and sends a signal to pin 2, so that it can be registered as HIGH/LOW, as my measured current produced by the LED was barely a micro-amp. Basically, pin 2 reads HIGH when the beam is interrupted, and LOW otherwise.
My intent is to use pulseIn() to measure how long the beam is interrupted to make a laser tachometer and measure rpm, however the function seems to output nonsensical values, typically a random value between 0 and 36, but occasionally a value in the tens of thousands, and a couple of times i saw values that seemed large enough to indicate an integer overflow somehow, all regardless of how long i block the beam for. pulseInLong() didn't seem to make much of a difference. I should also note that for some reason, pulseIn() also returns two values per interrupt, once when the beam is broken, then again when the beam is restored. Is there an issue with rapid current fluctuation, or perhaps do i need some sort of interrupt system in the code?
The code i've been using to try and get the function to work is as follows:
unsigned long lastPulse = 0;
void setup() {
Serial.begin(115200);
pinMode(2, INPUT);
}
void loop() {
lastPulse = pulseIn(2, HIGH, 5000000UL);
Serial.println(lastPulse);
}
Circuit diagram (apologies for how messy it is - first one i've made beyond a simple LED circuit):
Should add that E2 is the receiving LED, and E1 is simply an indicator. I've yet to have solidworks or circuit diagrams covered at uni, so i'm not sure which icons were appropriate.
I apologize for posting this again, but it's been three days without a response, and my alternative method of simply running digitalRead() every time the update function runs just isn't fast enough for my purposes. I won't spam the forum with any further reposts, but if there's something i'm doing wrong in my post format, please tell me - thank you!