I need help with this project I am currently working on.
I have an IR sensor that is connected to an LED Light. The LED is always on and whenever an object is over the light it goes off. On the serial monitor, it constantly gives off the number "0" if nothing is detected and the number "1" if there is something detected.
I am trying to program it to where in the serial monitor, it can tell me how long the "0" and "1" intervals are.
Lets say I have the sensor underneath a pendulum, it can tell me how long the intervals are because of it's interference with the sensor. I have looked everywhere and I do not know how to set this up.
This is the same as the blinkwithoutdelay, except that youa re using the time interval to measure time, not to turn the LED on and off.
When the first transition from 0 to 1 (or 1 to 0) occurs, set a variable (timeStart) to the current mills(). When you detect the second transition, millis() - timeStart will give you how long it took.
If you want more accuracy, you can use the microsecond timer, but this has some limits to resolution.
I am a bit confused on where you'd like to stet the variable
Not MY variable, it is yours :).
Please look at the sticky at the top of the forum for how to post code.
To learn how to detect a change in the digital, look at the StateChangeDetection example to understrand how to detect the change in the value.
Once you have detected the first change, save your variable to the current millis(). Once the other change is detected, then you simply subtract save time from current time to get elapsed time.
Okay, so I have successfully set up a StateChangeDetection code into with my IR Sensor code. Whenever the state changes from 0-1 or 1-0, it reads that to me and counts how many times it changes.
What variable am I looking for in order to save into the current millis()?
My apologies, I am not familiar too familiar with Arduino and I am learning the best I can but it's difficult finding help with the project I am working on, so I am sorry for my questions.
Good progress then. The variable to save a millis() value is just an unsigned long value or uint32_t (same thing). It is just a big number. The BlinkWithoutDelay example is relevant here, as they save the millis() value.