Arduino mega with sabertooth 2x60 coding power wheel motors

Trying to make this unfinished code work. Below is sketch and wiring.

would help us to understand, if you provided the code you're trying to compile.
In code tags. In the IDE, ctrl-T then ctrl-shft-C, then in a new message here, Ctrl-V. That's how simple it is.


// Software Serial Sample
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.

#include <SoftwareSerial.h>

//************************************************
//comment the next line if you do not have Sabertooth      <-------------<<<<<<<  N o t e
#define Sabertooth

#ifdef Sabertooth
#include <SabertoothSimplified.h>
SoftwareSerial SWSerial(NOT_A_PIN, 11);  //Software Serial TX, pin 11 (to S1).
SabertoothSimplified ST(SWSerial);       //Use SWSerial as the serial port.
#endif

//************************************************
#define PUSHED LOW
#define RELEASED HIGH

const int maximum = 867;
const int minimum = 176;

const byte speedPot = A0;

const byte heartbeatLED = 13;
//Sofware Serial connected to    11;
const byte forwardSwitch = 3;
const byte backwardSwitch = 2;

//10 * 50ms = 500ms (1/2 second) before a change is validated
const byte debounceAmount = 10;

//when counters reach 10 (i.e. 10 read cycles) we assumed a valid switch change
byte forwardCounter = 0;
byte backwardCounter = 0;

byte lastForwardSwitch = RELEASED;
byte lastBackwardSwitch = RELEASED;

enum { REVERSE = -1,
       NEUTRAL = 0,
       FORWARD = 1 };

int directionFlag = NEUTRAL;
int speed = 0;

//timing stuff
unsigned long heartbeatTime;
unsigned long checkSwitchesTime;
unsigned long checkSSpeedPotTime;


//                                      s e t u p ( )
//********************************************^************************************************
void setup() {
  Serial.begin(115200);

#ifdef Sabertooth
  SWSerial.begin(9600);
#endif

  pinMode(heartbeatLED, OUTPUT);

  pinMode(forwardSwitch, INPUT_PULLUP);
  pinMode(backwardSwitch, INPUT_PULLUP);

}  //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop() {
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)  //500ms
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //************************************************              T I M E R  checkSwitchesTime
  //is it time to read the switches ?
  if (millis() - checkSwitchesTime >= 50ul)  //50ms
  {
    //restart this TIMER
    checkSwitchesTime = millis();

    checkSwitches();
  }

  //************************************************              T I M E R  checkSpeedPot
  //is it time to read the speed pedal ?
  if (millis() - checkSSpeedPotTime >= 100ul)  //100ms
  {
    //restart this TIMER
    checkSSpeedPotTime = millis();

    checkSpeedPot();
  }

  //************************************************
  //other non blocking code goes here
  //************************************************

}  //END of   loop()


//                               c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches() {
  byte state;

  //************************************************              forwardSwitch
  state = digitalRead(forwardSwitch);

  //has there been a change in the switch's state ?
  if (lastForwardSwitch != state) {
    forwardCounter++;

    //have we met the debounce threshold ?
    if (forwardCounter >= debounceAmount) {
      //get ready for the next cycle
      forwardCounter = 0;

      //update to this new state
      lastForwardSwitch = state;

      //is the switch pushed ?
      if (state == PUSHED) {
        directionFlag = FORWARD;
      }

      else {
        directionFlag = NEUTRAL;
      }
    }
  }

  else {
    //no valid switch change
    forwardCounter = 0;
  }

  //END of this switch

  //************************************************              backwardSwitch
  state = digitalRead(backwardSwitch);

  //has there been a change in the switch's state ?
  if (lastBackwardSwitch != state) {
    backwardCounter++;

    //have we met the debounce threshold ?
    if (backwardCounter >= debounceAmount) {
      //get ready for the next cycle
      backwardCounter = 0;

      //update to this new state
      lastBackwardSwitch = state;

      //is the switch pushed ?
      if (state == PUSHED) {
        directionFlag = REVERSE;
      }

      else {
        directionFlag = NEUTRAL;
      }
    }
  }

  else {
    //no valid switch change
    backwardCounter = 0;
  }

  //END of this switch

}  //END of   checkSwitches()


//                             c h e c k S p e e d P o t ( )
//********************************************^************************************************
void checkSpeedPot() {
  //************************************************              speedPot
  speed = analogRead(speedPot);
  Serial.print(speed);
  Serial.print(" ");

  //speed = (map(speed, minimum, maximum, 0, 1023)) / 4;  //for speed 0 to ±255
  speed = (map(speed, minimum, maximum, 0, 1023)) / 8;  //for speed 0 to ±127

  Serial.print(speed);
  Serial.print(" ");

  //speed = constrain(speed, 0, 255);                     //for speed 0 to ±255
  speed = constrain(speed, 0, 127);  //for speed 0 to ±127

  speed = directionFlag * speed;
  Serial.println(speed);

#ifdef Sabertooth
  ST.motor(1, speed);
#endif

}  //END of   checkSpeedPot()


//********************************************^************************************************

Link to original topic where I found the code

I see the sketch, don't see the wiring/schematic. Be aware, some helpers here will wait for that info to be posted before engaging at all.

AFN
C

Sorry, the wiring diagram I used was still in the origin link. Only difference is I am using a mega. Here they are:


Thx. Will be a few hours before I can begin to look.

At a glance it does not seem you made any substantive changes to the code from the link. Please describe any changes you made that are beyond gratuitous editing or deletions.

Where did you get the library? Are you sure it is the same library used for the original code?

Have you tried any of the earlier versions on that thread? Even though they may yet have shortcomings, no one was having trouble getting them to verify.

a7

1 Like

Change DIP switch 3 to down so it automatically cuts the Sabertooth off if the batteries get too low.
The Sabertooth should flash a repeating pattern matching the number of cells in your lipo.
Switch 6 should be down for micro controller mode. You have it set to RC mode.

1 Like

So I was able to get a successful upload. I did not have the correct library before. I did not make any changes to the code, but I believe it was never finished/working code.

Ok I changed those DIP switches.

Ok, see in the definitions the values for minimum and maximum?
Those values won't necessarily work for you.
You need to calibrate your pedal and adjust the sketch to your analogRead min max

Let us know how it's going now, what works and what doesn't, I'm exhausted and will take a look again tomorrow (when I'm not doing this from my phone)

I appreciate all the help with this. I did find some discrepancies with the wiring. It appears my pedal is 2 wire grounds only. The motors and power to sabertooth are landed. But my car had a weelye board on it with a 7 pin connector. I am not sure where battery positive, power output, or high/low switch wires go on the arduino. This picture is my current facotry wiring

I am assuming either I need to rewire the existing pedal or buy a pwm style pedal?

Hey, give me a bit to take a closer look. Quickly though, no, if you have a switch pedal, reverse switch and a speed switch that's all you need.

1 Like

Assuming your pedal is the thing that looks like you put your foot on it, and that is the same thing as the speed pot in your wiring diagram, then no, you are good to go.

What needs be done, and @hallowed31 has provided for, is to find out what value the pedal,returns from the analogRead() at its extremes.

TBH I couldn't make heads or tails of the procedure to do this "calibrstion", and had no time to reverse engineer the code or try various theories.

Sry, too much of one thing and not enough of another, I was thinking of another thread…

But it should be as simple as

  • take a reading with no foot applied
  • take a reading with pedal on the metal full speed

That minimum and maximum would inform the mapping of the pedal.

To start, a simple sketch that does nothing but read the pedal and print the value would give you an idea of the range you can expect.

Also the end-to-end resistance of the pedal pot is interest; the circuit shows a voltage divider which may be unnecessarily restricting the range and making control less fine.

a7

1 Like

Also, I can't say for certain since that wiring picture leaves me unsure of what YOU specifically did. A hand drawn, clear schematic (a minimum) would be helpful. This is electronics; there's no room for ambiguity since any tiny mistake can and very well might sewer your whole project.

Here's a quick and (very) dirty sketch to get you min max vals to use in the actual Sabertooth sketch. As that sketch stands, your min max needs calibrated and you're using two different versions of sending the serial data to the ST motor controller.
If you want to use software serial, then you don't need the STSimplified stuff and vice versa. The former will send the 8-bit data through software serial pin TX 11 on an Uno R3, the latter will just send it as serial over hardware TX pin 1. Since it's a "permanent" type installation in a vehicle, I'd personally use the hardware serial pin. The control code to the ST motor controller will be the same.

Also about those Sabertooth DIP switches, you have DIP 5 in UP position. That sets linear response which may be jerkier in operation. During experimentation, try switching 5 DOWN for exponential response which should give more gradual accel and deceleration.

Here's the cal code:

/*
  analogPotCalibration

  NOTE: requires changes to code depending on your hardware
        if using two position button type pedal, activate
        the line
        pinMode(sensorPin, INPUT_PULLUP);
        by uncommenting it.
        if using typical potentionmeter, leave that line
        commented out.

  The circuit:
  - potentiomenter  to analog input 0
  OR if you use a switch type pedal
  - switch pin A0 to GND
  - LED on digital pin 13 to ground
  - view values in Serial monitor, 115200 baud

  Based (mostly) off Arduino IDE example sketch "Calibration"
  by David A Mellis and Tom Igoe

  dirty revisions by Hallowed31, 2024-07-27
*/

const int sensorPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;        // pin that the LED is attached to

int sensorValue = 0;         
int sensorMin = 1023;      
int sensorMax = 0;           
int valueToSaber = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  // the line below is for a button type pedal
  // comment this out if using potentiometer
  pinMode(sensorPin, INPUT_PULLUP);
  Serial.println("analogInputCalibration\n");
  delay(500);
  // turn on LED to signal the start of the calibration period:
  digitalWrite(13, HIGH);
  Serial.println("depress pedal to full throttle");
  delay(800);
  // calibrate half during first 3.8 seconds
  while (millis() < 3800) {
    sensorValue = analogRead(sensorPin);
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
  Serial.println("release pedal fully");
  delay(800);
  while (millis() < 7600) {    // filthy timing using sum of delays
    sensorValue = analogRead(sensorPin);
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
  // signal the end of the calibration period
  digitalWrite(13, LOW);
  // the following values will be the min max to import to the ST sketch
  Serial.println("calibration complete");
  Serial.print("pedal val Min: ");
  Serial.print(sensorMin);
  Serial.print("  pedal val Max: ");
  Serial.println(sensorMax);
  delay(5000); // time to read/write down/copy-paste etc
}

void loop() {
  /* IMPORTANT - use only ONE of potType();
     or buttonType(); functions at at time */
  // potType(); // uncomment if pot used for input
  buttonType(); // uncomment if button type input used
  valueToSaber = sensorValue; // unnecessary cast just for uber clarity
  Serial.print("pedal val Min: ");
  Serial.print(sensorMin);
  Serial.print("  pedal val Max: ");
  Serial.print(sensorMax);
  Serial.print("  speed val out to Saber: ");
  Serial.println(valueToSaber);
  if (valueToSaber >= 128) digitalWrite(ledPin, HIGH);
  else digitalWrite(ledPin, LOW);
}
void potType() {
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  sensorValue = constrain(sensorValue, 0, 255);
}
void buttonType() {
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, sensorMin, sensorMax, 255, 0);
  sensorValue = constrain(sensorValue, 0, 255);
}

For the actual Sabertooth sketch, refer to pg 16 of the datasheet to double check all the DIP switches and see that you understand it.
Whether using Arduino hardware serial TX on pin 1 or software serial TX on pin 11, the info as far as the Sabertooth manual is concerned is the same.
Be sure to change the baud rate in the Sabertooth sketch you have to match the note in the manual (why they had it at 115200 when they recommend starting at 9600 is beyond me but they did), so change

void setup() {
  Serial.begin(115200);

to this

void setup() {
  Serial.begin(9600);
1 Like

To be clear, what battery chemistry (SLA or LiPo or something else), voltage and capacity are you using?
eg: for SLA: 12V, 5Ah or something like 24V, 7Ah?
for Lipo: 3S lipo 11.1V, 5000mAh or something like 6S lipo 22.2V, 4000mAh?

24v9Ah

Alright, I will try and come up with a drawing of my exact wiring to this point