I have an arduino UNO.
I have a Sabertooth 2x25 motor contoroller.
I send a short pulse to the motorcontroller and I get the desired speed.
I print a line to serial before I send a short pulse to the motorcontroller and I get a faster speed.
The higher the baud rate on the serial the faster the motor goes.
All speeds sent after a print line are faster than expected.
This is bugging me. Can anyone explain what I have done wrong?
Please see the example below:
int S1 = 5;
int S2 = 6;
void setup() {
// Sabertooth 2x25 v2
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
// PC Serial
Serial.begin(57000);
}
void loop() {
if (Serial.available()) {
char Command = char(Serial.read());
if (Command == '1') {
digitalWrite(S1, HIGH);
delayMicroseconds(1600);
digitalWrite(S1, LOW);
// Motor turns at expected speed
}
else {
Serial.println("Some Test");
digitalWrite(S1, HIGH);
delayMicroseconds(1600);
digitalWrite(S1, LOW);
// Motor turns at higher than expected speed
}
}
}
Isn't your pulse a bit assymetrical? You have:
digitalWrite(S1, HIGH);
delayMicroseconds(1600);
digitalWrite(S1, LOW);
But then you re-enter loop immediately. So the time between LOW and HIGH would be very short.
In any case the Serial.println will be causing an interrupt to occur (if you are using version 1.0+ of the IDE). That will affect things.
This is their code to send a single pulse to the motor controller. I like it because it is so fast and after the pulse is sent I don't even need the arduino hooked up to keep it at that speed if need be.
digitalWrite(S1, HIGH);
delayMicroseconds(1600);
digitalWrite(S1, LOW);
My target environment will not be in the arduino IDE. I will try this from .0023 and from C# and see if the issue goes away. Thanks for the tip.
Why might 1.0 cause this as opposed to other serial connections?
EDIT: This issue exists in .0023 as well.
I didn't understand a lot of that. However the 1.0 version of the Serial library implements interrupts to clock out serial data outwards. Earlier versions blocked. So if you do a Serial.println you are effectively building in a delay.
I like it because it is so fast and after the pulse is sent I don't even need the arduino hooked up to keep it at that speed if need be.
Yes, well disconnect the Arduino and then the serial prints won't affect it.