I have an infrared camera that updates very quickly, but in the same setup I need a ultrasonic sensor that sends data much more infrequently. I've got the infrared camera working great with an if loop and im trying to figure out the best way to run both sensors at the same time and read the data in the serial monitor without slowing down the speed of the infrared camera. Whats the most logical way I can make the camera loop run N times, and every Nth time the ultrasonic will update? or is there a better way to go about this?
What ultrasonic sensor are you using ?
It's a ping rangefinder. I guess it wouldn't matter what the sensors are, I just can't figure out how to make one update all the time and another update only every nth time.
I just can't figure out how to make one update all the time and another update only every nth time.
You need to look at the Blink Without Delay example to see how to do things based on time.
You could increment a counter every time sensor A reads a value then, when it reaches a set number take a reading from sensor B. Reset the counter to zero and do it all over again.
Much better though, as Paul suggests, to take the readings at defined times, which means that they can be independant of one another if you want. The BlinkWithoutDelay example shows you the principle of using one timer but this can be expanded. I am currently experimenting with a 2 wheel robot that reads a ping sensor, increases speed and reads wheel sensors to even out wheel speed all at different intervals. Once you have got one timer working then adding others is easy.
set timer1 start time to now set timer2 start time to now start of loop if desired time 1 has elapsed do stuff set timer1 start to now end of if if desired time 2 has elapsed do other stuff set timer2 start to now end of if end of loop