Hi,
I have a digital input channel that change state every few hundreds millis.
I need to collect the state change times in an array with 1 mSec resolution.
I can not use millis() because I need to restart at 0 each time.
I try to use a Timer, but it gives me a random series of times rather than the real times.
I am probably doing something wrong.
Here is a test code:(that does not work as I expect)
HardwareTimer timer(1);
uint16 time[20]; //Time stamp array
uint8 state;
uint16 counts;
void setup()
{
timer.setPeriod(1000);
Serial1.begin(115200);
}
void loop()
{
int index = 0;
timer.setCount(0); //Zero and reset the timer.
timer.resume();
do
{
state = digitalRead(15);
while (digitalRead(15)== state) { } //loop wait for state change
time[index] = timer.getCount(); //Add time stamp to array.
index++;
}
while (digitalRead(16));
int i;
for (i=0;i<10;i++) //Print the array (testing)
Serial1.println(time[i]);
}