I have read before that you can't use delay() inside I2C receiveEvent because it is an ISR.
What I want to do is as follows: I have a servo connected to my Arduino Uno and I want to control that using an I2C signal. Since I want to decrease the speed of the servo, I was previously using a loop with delay inside, which doesn't work when the function is called by I2C.
This is the code that I used before to control the servo:
void setServo(bool status) {
if (status) {
for (int pos = 0; pos <= 180; pos += 5) {
servo.write(pos);
delay(50);
Serial.println(pos);
}
} else {
for (int pos = 180; pos >= 0; pos -= 5) {
wasteFlapServo.write(pos);
delay(50);
Serial.println(pos);
}
}
servoStatus = status;
}
How could I still control the speed of the servo even if I use I2C? Thanks for your help.
Kind regards, Moritz
servo
andwasteFlapServo
? – DataFiddler Jul 6 '20 at 13:11Serial.println()
is another no-no during an ISR. Any sort of serial communication is quite slow. – Duncan C Jul 6 '20 at 14:04print()
just places the data in the buffer. The actual transmission is done via ISRs, which place each byte into the UART hardware register. So all the data would fill the buffer, untils it's full. And the data would only get send, when going out of the ISR. I think in the newer core versionsprint
will also not block at full buffer, when inside an ISR, though I don't remember, where I read that – chrisl Jul 6 '20 at 14:14