Hello
I am working on a project with my Uno, but I'm totally new to all of this, so this might seem like an easy question for you guys.
I'm making a car with an IR sensor.
When it passes over a black line, it has to start counting the time till it passes over a second black line.
That time between those two lines needs to be stored and printed to the serial monitor.
I already made the car, everything works.
The IR sensor senses the difference between black and white. (Black = 1, white = 0). And this shows up nicely on the serial monitor.
But I'm really breaking my head on the time issue. I have been Googling, trying and searching for about four hours straight now and I'm still coming up blank.
Any help or new insights on how to tackle this problem would be greatly appreciated!
When you pass the first line, save a time stamp using millis(). When you pass the second, subtract millis() from that value. The result will be the time difference in milliseconds.
+1 aarg!
Caracalla, Arduino has the millis() and micros() functions that return unsigned long values as time since the chip started up.
Arduino uses unsigned long for time and not long because unsigned subtraction makes rollover a non-problem.
Consider a 12 hour round clock hour hand instead of a 4-billion-some millisecond round clock.
An input changed state (LOW to HIGH) at 11 and changed back at 1. How long was it HIGH?
Start at 1, the end time, and subtract 11 by turning the hour hand 11 backwards from 1 to get a difference of 2 hours. If the start is at 3 and the end is at 8, it's 8 - (turn leftward) 3 = 5.
You can record event times and determine differences between any of them.
The main thing is that you need to think in terms of state. Your loop should be tight - executing over and over. It generally shouldn't have delays in it.
So you program will have 5 'states' it in.
1 - I have not sensed the first black line yet
2 - I am sensing the first black line
3 - I have crossed the first black line, and am looking for the second
4 - I am sensing the second black line
5 - All done!
Each time the loop runs, you have four pieces of information:
1 - last time the loop ran an iteration, what state did we wind up in?
2 - when did I cross the first black line?
3 - am I crossing a black line now?
4 - what time is it now?
With those four pieces of information, you need to:
1 - decide what state the program shall now be moved to
2 - decide if you need to record the "when did I cross the first black line?" time
3 - decide if you need to compute and print the speed.
+1 Paul
There should be a separate task that only runs the sensor, collects data including time and passes that off to the rest of the sketch. That part only looks for change in sensor state to report, over and over with no end as far as it is designed.
4 possible states between last read HIGH/LOW and this read HIGH/LOW but only report on change.
There should be a separate process task that acts on data from the sensor task. It should have its own states and prepare data for output.
There should be a separate task for output if it requires any special hardware to be driven.
The break the job up into tasks approach can keep the logic for different parts of the program from getting tangled up into each other and leading into monolithic spaghetti code.