double analog footpedal to sabretooth pwm output issue

Hi, as you probably will see I’m new to this forum.
I’ve been an old-school plc-programmer and switched over to the arduino, in this case the mega.

In my attempt to power a small car with double electro motor over a sabretooth 2x25 with an arduino mego (will be replaced by a mini!), the output from the arduino to the sabretooth was between 0.9v and 4.5v . I expected from 0 to 5v!
I tried to keep the program as short and compact as possible so I came with the following script:

/* ArduinoMega Example: 2 analog pot-footpedals for sabretooth 2x25 synchron

  • Moves two motors on a pegperego gator (kidstoycar)
    through a range of positions in response to the position
    of two footpedals analog pot-type attached to 2 Analog inputs.
  • the sabretooth needs 2.5v for zero output.
    0-2.5v is full backwards to stop
    2.5-5v is stop to full forward
  • acc. pedal and backw. pedal kicked in should result in 2.5v output or full stop.
  • acc. pedal and backw. pedal not actuated should result in 2.5v output or full stop.
    */

const int potpinA = A0; // input analog pot accelerator pedal
const int potpinB = A2; // input analog pot backup pedal

const int sabretoothS1out = 9; // output to pin S1 on sabretooth 2x25 pwm

int sensorAvalue = 0; // value from potpinA, acc. pedal
int sensorBvalue = 0; // value from potpinB, backup pedal
int valuedifferenceAB = 0; // if input from potpin A and B is zero, the output to sabretoothS1out should be 128 (range 0-255) or 2.5volt
// this is going to be the output from the arduino to the sabretooth S1 input signal PWM

void setup() /****** SETUP: RUNS ONCE ******/
{
valuedifferenceAB = 128; // at startup the output of the sabretooth should be 0 volt or full stop. input S1 on sabratooth should been half-value
}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
sensorAvalue = analogRead(potpinA); // reads value of accelerator pedal pot
sensorBvalue = analogRead(potpinB); // reads value of backup pedal pot
valuedifferenceAB = map((sensorAvalue-sensorBvalue),-512,512,0,255); // gives output from 0.9v to 4.5v ?? why not 0v to 5v ?

analogWrite(sabretoothS1out,valuedifferenceAB);

delay(10);

}//–(end main loop )—

Anyone an idea?

Best regards, Hugo

  valuedifferenceAB = 128;     // at startup the output of the sabretooth should be 0 volt or full stop. input S1 on sabratooth should been half-value

You never use this value, so the comment and code should hit the bit bucket.

 sensorAvalue = analogRead(potpinA);                                   // reads value of accelerator pedal pot
 sensorBvalue = analogRead(potpinB);                                   // reads value of backup pedal pot
 valuedifferenceAB = map((sensorAvalue-sensorBvalue),-512,512,0,255);  // gives output from 0.9v to 4.5v ?? why not 0v to 5v ?

You have no idea what value you are getting for sensorAvalue or sensorVvalue. Yet, you assume that the difference will be between -512 and 512. I missed something there, I guess. How did you arrive at that range?

The value in the varaible valuedifferenceAB is not 0.9v to 4.5v, so the comment is completely wrong.

sorry for late reply!

I changed a few things (including the bit bucket!)

Indeed the value isn’t constant from the pot inputs. The value difference however was quite stable. I noticed (after using the scope) that the signal was backwards so I changed it to:
valuedifferenceAB = map((sensorAvalue-sensorBvalue),512,-512,0,255);
The output ran from 2.54 volts middle (zero-point) to 4.99 volts when the acc pedal was pressed for 95% and then ran over the top to 0.20 volts. Same results when using the backup pedal. 95% resulted in 0.09 volts and 100% in 4.85 volts.
So I changed the values and ended at:
valuedifferenceAB = map((sensorAvalue-sensorBvalue),650,-670,10,255);
the zero value was 2.47 volts and the 100% acc pedal gave 4.89 volts, the 100% backup pedal gave 0.08 volts.

Of course you were right about the poor quality of this solution! So I’m working on a new setup.
I trying to find out a way to read the analog input and convert it to a stable signal. Have to say that the exact value isn’t that important, the only important signals are full backup, zero and full forward. Because the sabretooth bandwidth is very accurat. It needs 2.5 volt for 0 volt output and this is also the regenerating and breaking position.