Hi there,
I'm working on controlling several PWM devices and I need to be sure that my widths are accurate to within about hundred milliseconds. Originally I was using this code, which worked OK:
digitalWrite(pin1, HIGH);
delayMicroseconds(pulse);
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delayMicroseconds(pulse);
digitalWrite(pin2, LOW);
// etc...
However we have since discovered that this is just too long of a delay as all of the pulses add up together. I'm fully aware that delayMicroseconds() disables interrupts, but our new plan involves something along these lines:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
delayMicroseconds(pulse);
digitalWrite(pin1, LOW);
delayMicroseconds(difference);
digitalWrite(pin2, LOW);
This way we only have to spend some time sorting the timings beforehand. Problem is that these delays are no longer atomic. I am worried that if serial data or something similar comes in at the point in between the two delays, the pulse widths will be extended beyond where they should be.
My hope is that there is a way to disable interrupts so that I can wrap the entire pulse code. I'm not concerned with losing serial data; our protocol has a mechanism to recover from this; however I am concerned if the devices are not operating as they should be. In theory, disabling interrupts should fix this problem.
Any ideas?
Thanks,
-- Matt