Hi!
For the sabertooth, it can be controlled by sending a character (1-127 is motor1 and 128-255 motor2, where 1 is full reverse and 127 is full forward).
I have modified the code and now it works well now. However, when I add a receiver code, then it cant work as before, do you know what's wrong?
My code that works is:
#include <SoftwareSerial.h>
#define SABER_TX_PIN 1
// NOT USED (but still init'd)
#define SABER_RX_PIN 12
// Set to 38400 through Sabertooth dip switches
#define SABER_BAUDRATE 38400
// Simplified serial Limits for each motor
#define SABER_MOTOR1_FULL_FORWARD 127
#define SABER_MOTOR1_FULL_REVERSE 1
#define SABER_MOTOR1_FULL_STOP 64
#define SABER_MOTOR2_FULL_FORWARD 255
#define SABER_MOTOR2_FULL_REVERSE 128
#define SABER_MOTOR2_FULL_STOP 192
// Motor level to send when issuing the full stop command
#define SABER_ALL_STOP 0
SoftwareSerial SaberSerial = SoftwareSerial( SABER_RX_PIN, SABER_TX_PIN );
void setup()
{
pinMode( SABER_TX_PIN, OUTPUT );
SaberSerial.begin( SABER_BAUDRATE );
// 2 second time delay for the Sabertooth to init
delay( 2000 );
}
void loop ()
{
setEngineSpeed(550);
}
void setEngineSpeed( signed int a )
{
int motorspeed1;
int motorspeed2;
//char cSpeedVal_Motor1 = char(a);
motorspeed1 = map(a, 1023, 0, 127, 1);
motorspeed2 = map(a, 1023, 0, 255, 128);
//convert the int to char which later is sent to the motor driver
char cSpeedVal_Motor1 = char(motorspeed1);
char cSpeedVal_Motor2 = char(motorspeed2);
SaberSerial.print(cSpeedVal_Motor1);
SaberSerial.print(cSpeedVal_Motor2);
return;
}
and this one, can't work
#include <VirtualWire.h>
#include <SoftwareSerial.h>
#define saber_Tx_Pin 1
#define saber_Rx_Pin 12
#define saber_Baudrate 38400
//definition of the motor
#define saber_Motor1_FullForward 96
#define saber_Motor1_Stop 64
#define saber_Motor1_FullReverse 32
#define saber_Motor2_FullForward 223
#define saber_Motor2_Stop 192
#define saber_Motor2_FullReverse 159
#define saber_Stop 0
//definition of receiver
int ledPin = 13;
char Ychar[5]; //y-coordinate for sabertooth input
char Xchar[5]; //x-coordinate for sabertooth input
char XYchar[10];
String xx, yy;
SoftwareSerial SaberSerial = SoftwareSerial (saber_Rx_Pin, saber_Tx_Pin);
void initSabertooth()
{
char stop1 = 64;
char stop2 = 192;
pinMode(saber_Tx_Pin, OUTPUT);
SaberSerial.begin (saber_Baudrate);
SaberSerial.print (stop1);
SaberSerial.print (stop2);
delay (2000);
}
void setup ()
{
initSabertooth();
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop ()
{
//receiver code
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if(vw_get_message(buf, &buflen))
{
int i;
digitalWrite(ledPin, HIGH);
for(i = 0; i < buflen; i++)
{
XYchar = char(buf*);*
* XYchar[buflen] = '\0';*
* String xy = String(XYchar);*
* if(xy.indexOf(",")>=0)*
* {*
* xx = xy.substring(0,(xy.indexOf(",")));*
* yy = xy.substring(xy.indexOf(",")+1);*
* xx.toCharArray(Xchar, xx.length()+1);*
* yy.toCharArray(Ychar, yy.length()+1);*
* }*
* }*
* }*
* int x = atoi(Xchar);*
* int y = atoi(Ychar);*
* delay(200);*
* //motor coding, x and y are the joystick coordinates*
* if (x == 499 && y == 499)*
* {*
* char stop1 = 64;*
* char stop2 = 192;*
* SaberSerial.print (stop1);*
* SaberSerial.print (stop2);*
* }*
* else if (x == 499 && y != 499)*
* {*
* motorStraight (y);*
* }*
* digitalWrite(ledPin, LOW);*
* delay (100); *
}
void motorStraight (signed int a)
{
* int motorspeed1;*
* int motorspeed2;*
* motorspeed1 = map(a, 1023, 0, 127, 1);*
* motorspeed2 = map(a, 1023, 0, 255, 128);*
//convert the int to char which later is sent to the motor driver
* char cSpeedVal_Motor1 = char(motorspeed1);
char cSpeedVal_Motor2 = char(motorspeed2);*
* SaberSerial.print(cSpeedVal_Motor1);
SaberSerial.print(cSpeedVal_Motor2);
_ return;_
_}*_