sabertooth simplified serial with slave select.

hi!
i want to drive four motors form 2 sabertooth 2X12 motor driver i have written one program to do the same kindly please check where i am wrong....
i have connected left front and right front motors with one sabertooth and left rear and right rear with another sabertooth. i am controlling these motors from xbox 360 controller.left stick will control left rear and front motor and right stick will control right rear and front motor. kindly please check whether i have written a wrong program or not.

int mr=22;
int ml=24;
int sig;
int pwmread;
void setup() {
pinMode(ml,OUTPUT);
pinMode(mr,OUTPUT);
digitalWrite(ml,LOW));
digitalWrite(mr,LOW);
Serial.begin(9600);
Serial3.begin(9600);
}

void loop() {

if(Serial.available()>0)
{
sig=Serial.read();

if(sig>=58 && sig<=107)
{
pwmread=sig-58;
pwmread=64+round(pwmread63/49);
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(pwnread);
delay(0.5);
}
else if(sig>=107 && sig <=155)
{
pwmread=sig-155;
pwmread=abs(round(pwmread
64/49));
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(pwnread);
delay(0.5);

}
else
{
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(0);
delay(0.5);
}
}

if(sig>=157 && sig<=205)
{
pwmread=sig-157;
pwmread=192+round(pwmread63/49);
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(pwnread);
delay(0.5);
}
else if(sig>=206 && sig <=254)
{
pwmread=sig-206;
pwmread=192-round(pwmread
64/49);
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(pwnread);
delay(0.5);

}

else
{
digitalWrite(ml,HIGH);
digitalWrite(mr,HIGH);
Serial3.write(0);
}
}

}
}

pwmread=64+round(pwmread*63/49);

Rounding an integer doesn't make sense.

What, exactly, is writing one value to Serial3 supposed to make 4 motors do? Or even 2?

PaulS:
What, exactly, is writing one value to Serial3 supposed to make 4 motors do? Or even 2?

i want to drive a car and i want to give the sig in such a way that when I move left stick up the left rear and front motors should move in forward direction. and same in the case when i move right stick up the right rear and front motors will move in forward direction.i would like to know what will happen i move both the sticks up or down simultaneously.or in reverse sense i.e., lift up right down and vice versa.

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

This will make it sooo much easier to read on the forum.

Did you write this sketch all at once, or did you write it section at time, making sure each worked before putting them together?
eg,
Write sketch to control one sabertooth
write sketch to control two sabertooth unit independantly.
write sketch to communicate between x-box and controller, verify sent instructions

Then combine sketches...
You have Serial.begin setup but aren't using it to debug using the IDE monitor.

Tom.... :slight_smile:

int mr=22;
int ml=24;
int sig;
int pwmread;
void setup() {
  pinMode(ml,OUTPUT);
  pinMode(mr,OUTPUT);
  digitalWrite(ml,LOW));
  digitalWrite(mr,LOW);
  Serial.begin(9600);
  Serial3.begin(9600);
  }

void loop() {
  
 if(Serial.available()>0)
  {
    sig=Serial.read();

    if(sig>=58 && sig<=107)
      {
       pwmread=sig-58;
       pwmread=64+round(pwmread*63/49);
        digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(pwnread);
        delay(0.5);
      }
     else if(sig>=107 && sig <=155)
     {
       pwmread=sig-155;
       pwmread=abs(round(pwmread*64/49));
       digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(pwnread);
        delay(0.5);
       
     }
     else
     {
      digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(0);
        delay(0.5);
     }
     }
  
     
 if(sig>=157 && sig<=205)
      {
       pwmread=sig-157;
       pwmread=192+round(pwmread*63/49);
      digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(pwnread);
        delay(0.5);
      }
     else if(sig>=206 && sig <=254)
     {
       pwmread=sig-206;
       pwmread=192-round(pwmread*64/49);
       digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(pwnread);
        delay(0.5);

   
  }
     
   else
     {
      digitalWrite(ml,HIGH);
        digitalWrite(mr,HIGH);
        Serial3.write(0);
     }
     }

  }
}

i have written the above code all at once.
initially i was using the sabertooth 2x12 on analog mode by providing the pwm signals from the arduino all works good on the same program at 0(0v) and 255(5v) but the motors didn't stop at 127. so i switched to serial mode.

int mlf=13;
int mlr=9;
int mrf=12;
int mrr=8;

int sig;
int pwmread;
void setup() {
  pinMode(mlf,OUTPUT);
  pinMode(mlr,OUTPUT);
  pinMode(mrf,OUTPUT);
  pinMode(mrr,OUTPUT);// put your setup code here, to run once:
  analogWrite(mlf,127);
  analogWrite(mlr,127);
  analogWrite(mrf,127);
  analogWrite(mrr,127);

  Serial.begin(9600);
  }

void loop() {
  
 if(Serial.available()>0)
  {
    sig=Serial.read();

    if(sig>=58 && sig<=107)
      {
       pwmread=sig-58;
       pwmread=127+round(pwmread*128/49);
      analogWrite(mlf,pwmread);
      analogWrite(mlr,pwmread);
      }
     else if(sig>=107 && sig <=155)
     {
       pwmread=sig-155;
       pwmread=abs(round(pwmread*127/49));
       analogWrite(mlf,pwmread);
        analogWrite(mlr,pwmread);
     }
  
     
 if(sig>=157 && sig<=205)
      {
       pwmread=sig-157;
       pwmread=127+round(pwmread*128/49);
       analogWrite(mrf,pwmread);
        analogWrite(mrr,pwmread);
      }
     else if(sig>=206 && sig <=254)
     {
       pwmread=sig-254;
       pwmread=abs(round(pwmread*127/49));
       analogWrite(mrf,pwmread);
        analogWrite(mrr,pwmread);
   
  }
  }
}

i don't know how to use low pass rc filter with arduino i.e., how to select the resistor and capacitor so as to perform smooth functioning of motors.