We're trying to evaluate the feasibility of a Servo driven system, and were wondering if it is at all possible to read from an accelerometer while data is being output to a Servomotor. Essentially, if we tell the Arduino to send a PWM signal, is it possible to continue sending that exact same PWM signal while doing something else on the device (IE: Reading on Analog, writing Serial, or otherwise)
The Servo library doesn't literally use analogWrite() but it does use timer interrupts to generate the output pulses, which means the microcontroller's CPU is free to do other things such as reading analog ports and writing to hardware serial ports.
Is there any sort of internal 'timer' that Arduino can read? We need to drive this servo motor in such a way that movements begin/end at discreet times. We're not sure how to do this when commands can vary in length from microsecond to microsecond, such that we could ever deliver servo commands every 20ms while performing other functions meanwhile.
That's what millis(); and micros(); are for.
You capture the 'time':
currentTime = millis();
every pass thru loop.
Then you do a little math:
elapsedTime = currentTime - previousTime;
then you compare:
if (elapsedTime >= duration){
// set up for the next time
previousTime = previousTime + duration;
// and do whatever your action is
}