The first will entail using a digital joystick, arduino, and potentiometer to control 2 motors. I am assuming the program below will work for this application, but need to make sure my dip switches are set appropriately. Can you please tell me where they should be set? Also, I will be connecting directly to the arduino, not thru a base shield. Do I attach both "NC" to VIN and both GRD to the grounds, or just one?
The code would be as below...
- CODE: SELECT_ALL_CODE
int sw1=10;
int sw2=11;
int sw3=12;
int sw4=6;
int state1;
int state2;
int state3;
int state4;
//MOTOR A
int pwm1 = 9;
int dir1 = 8;
// Motor B
int pwm2 = 3;
int dir2 = 5;
// Speed control potentiometers
int SpeedControll = A0;
// Motor Speed Values - Start at zero
int MotorSpeed1 = 0;
void setup()
{
pinMode(sw1,INPUT_PULLUP);
pinMode(sw2,INPUT_PULLUP);
pinMode(sw3,INPUT_PULLUP);
pinMode(sw4,INPUT_PULLUP);
Serial.begin(9600);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
}
void loop()
{
state1 = digitalRead(sw1);
state2 = digitalRead(sw2);
state3 = digitalRead(sw3);
state4 = digitalRead(sw4);
// Read the values from the potentiometers
MotorSpeed1 = analogRead (SpeedControll);
// Convert to range of 0-255
MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 200);
if (state1 == 0) { //Make a Right turn
analogWrite(pwm2, 120);
analogWrite(pwm1, 120);
digitalWrite(dir2, HIGH);
digitalWrite(dir1, LOW);
}
else if (state2 == 0) { //Forward Movement
analogWrite(pwm1, MotorSpeed1);
analogWrite(pwm2, MotorSpeed1);
digitalWrite(dir1, HIGH);
digitalWrite(dir2, HIGH);
}
else if (state3 == 0) { //Take a left turn
analogWrite(pwm1, 120);
analogWrite(pwm2,120);
digitalWrite(dir1, HIGH);
digitalWrite(dir2, LOW);
}
else if (state4 == 0) { //Backward Movement
analogWrite(pwm1, MotorSpeed1);
analogWrite(pwm2, MotorSpeed1);
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);
}
else {
analogWrite(pwm1, 0);
analogWrite(pwm2, 0);
digitalWrite(dir1, LOW);
digitalWrite(dir2, LOW);
}
Serial.print("1-");
Serial.print(state1);
Serial.print(" 2-");
Serial.print(state2);
Serial.print(" 3-");
Serial.print(state3);
Serial.print(" 4-");
Serial.println(state4);
delay(250);
}
My second application will be using an analog joystick, with a potentiometer (to further control the speed), with the driver, arduino and 2 motors. I have previously used a different brand driver using the serial packetized mode, but need to know if I can use the same program, or need to modify it for the MDDS30. Once again, I need to know the position of the dip switches. I am running the motors way below their max speed. The code is below....
- CODE: SELECT_ALL_CODE
SoftwareSerial SWSerial(NOT_A_PIN, 11); // RX on no pin (unused), TX on pin 11 (to S1).
Sabertooth ST(128, SWSerial); // Address 128, and use SWSerial as the serial port.
int sensorPin = A0;
int S2= A1;
int sensorValue = 0;
int sensorvalue2 =0;
int mapedsen2 =0;
int mapedsen1=0;
void setup()
{
SWSerial.begin(9600);
Serial.begin(9600);
ST.autobaud();
}
void loop()
{
int SPEED_POT = map(analogRead(A3), 0, 1023, 10, 50);
//Serial.println(SPEED_POT);
sensorValue = analogRead(sensorPin);
sensorvalue2= analogRead(S2);
mapedsen1 = map(sensorValue,0,1023,-SPEED_POT,SPEED_POT);
mapedsen2 = map(sensorvalue2,0,1023,-SPEED_POT,SPEED_POT);
// Change this 30 to 50 may be if it doesn't work with the load on it.
int b = map(sensorvalue2,0,1023,25,-25);
if ( (mapedsen1 >=5) && (abs(mapedsen2)<= 20) ) {
ST.motor(1,mapedsen1);
ST.motor(2,-mapedsen1);
}
else if((mapedsen1 <= -5)&&(abs(mapedsen2)<=20)) {
ST.motor(1,mapedsen1);
ST.motor(2,-mapedsen1);
}
else if((abs(mapedsen1)<= 10) && (mapedsen2 >= 0)) {
ST.motor(1,b);
ST.motor(2,b);
}
else if((abs(mapedsen1)<= 10) && (mapedsen2 <= -3)) {
ST.motor(1,b);
ST.motor(2,b);
}
else {
ST.motor(1,0);
ST.motor(2,0);
}
}