I understand this part, but here is my problem:
When the sensor is first initialised by the Arduino UNO, the data received via I2C is not correct for the first 10s. The sensor will take about 10 seconds before it starts transmitting correct and useful data. So, my plan is to simply not allow the Arduino to receive this inaccurate/garbage data until the sensor stabilises 10s after it is first initialised in the setup() function of the Arduino code. My approach is to add a delay() function with 10000ms but i don’t know if this will work or if there is a better approach…
Or maybe a second (better?) option would be to use millis() and set the loop to 10 seconds and after that the rest of the code can be executed?
Inside of void loop() each function or block of structure can run on condition as a continuing task.
if ( micros() - accurateStart >= accurateWait )
// do after time is up
accurateStart += accurateWait; // restarts on cycle even if trigger is late
if ( accurateWait > 0 ) // some other task sets accurateWait to run this once
if ( micros() - accurateStart < accurateWait )
// do until time is up
accurateWait = 0; // end the 1-shot
Using event-driven tasks works as long as none of it blocks execution.
Here are some tutorials, only use the 3rd where you must and leave it for after you know the others.
Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs ← tasking Arduino 1-2-3
Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking ← techniques howto — be sure to get the State Machine example.
- Gammon Forum : Electronics : Microprocessors : Interrupts
How it works once you have overview:
Your sketch can sense ongoing process events in time. (save the time as ‘it’ happens, in time)
Your sketch can make events to control it over time. (set up timers, things happen over time)
Event: pin state change, variable value change, timeout, Serial available, etc.
Use variables to pass values between tasks, you need less structure depth than one do-everything spaghetti knot.