Differential steering with stepper motors

I am designing a small three wheel platform with two stepper motors the rear. The idea is to drive this with a kind of differential steering. The controls will be provided with two hall sensors which are used like in a throttle of a E-bike. The forward reverse is controlled with one sensor and the left right steering with another. So you get two analog signals. One controls the forward and revers speed an the second one the left and right direction. The idea is to have the first one control the speed and maximum limit of both motors in straight forward movement, and to make a turn I subtract a certain speedfactor of one motor, depending on the turn signal of the second analog input.
My problem is that most Arduino scripts are made to work with position and steps to go, and not just speedcontrol. A second issue is that the loop can’t be to complex because in that case I loose speed in the motors due to a longer cycle time. I tried working with “accelstepper” but at certain speeds the motor is grumpy and noisy, the speed changes are not always smooth. I tested it with only one motor and I am afraid that with two the speed drop will be higher.
I tried a routine I found to not read the analog values every loop but I am not certain it does anything.
Can anyone give me some pointers in the right direction.

differntial_steering.ino (2.7 KB)

Try doubling the current capability of your power supply for the motors.
Paul

Members can be reluctant to download code. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Because with the IDE installed, it means loading the IDE and using system resources, slowing things down.

Without the IDE, it would mean loading an editor.

If the code is properly cited in the forum, it can be edited in situ. :+1:

I Tried stepper .h instead of accelstepper and it seems to run better. the motor doesn't run super smooth but maybe this is not so abnormal for a stepper. i only use one motor for the test ( motor_rechts).
one thing is strange, when I change from forward to reverse the loop seems to stop and the motor runs at a slow idle speed for a few seconds.

#include <Stepper.h>

 int StepsPerRevolution = 400;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:

Stepper myStepper(StepsPerRevolution, 9,4);
//int stepCount = 0;  // number of steps the motor has taken

int x_key = A2;
int y_key = A3;
static int x_pos;  // forward reverse
static int y_pos;   //  stearing

#define dirPinStepper    5
#define enablePinStepper 6


static int motor_speed;
int motor_speed1;
 static int motor_links;
static int motor_rechts;

static int faktorL1;  //steering correction factor
static int faktorR1;  //steering correction factor


void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  


  pinMode (4, OUTPUT);
 pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
 
    Serial.begin(9600);

}

void loop() {




 //static int current_speed = 0;         // Holds current motor speed in steps/second
  //static int analog_read_counter ;    // Counts down to 0 to fire analog read
 // static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
 // static int analog_value = 0;              // Holds raw analog value.

 //if (analog_read_counter > 0) {
  // analog_read_counter--; }
  
   //   analog_read_counter = 100;
      
  faktorL1 = map (y_pos, 530, 650, 1, 3000);
  faktorL1 = constrain ( faktorL1, 1, 3000);

  faktorR1 = map (y_pos, 510, 410, 1, 3000);
  faktorR1 = constrain ( faktorR1, 1, 3000);


 x_pos = analogRead (x_key) ;  //Reading the forward reverse value 240 tot 870 center value 520
 
  y_pos = analogRead (y_key) ;  //Reading the steering value


 if (x_pos > 530 && x_pos < 900 ) { 
//forward
 //stepper1.runSpeed();
////
 
 digitalWrite (dirPinStepper, HIGH);
 digitalWrite (enablePinStepper, HIGH);

    motor_speed = map(x_pos, 510, 900, 100, 5000);   
    motor_speed = constrain ( motor_speed, 10, 5000);



    ;
    motor_rechts = (motor_speed - faktorR1);
    motor_links = ( motor_speed -  faktorL1);
    motor_links = constrain ( motor_links, 10, 5000);
    motor_rechts = constrain ( motor_rechts, 10, 5000);

//if (motor_rechts > 0) {
    myStepper.setSpeed(motor_rechts);
    // step 1/100 of a revolution:
    myStepper.step(StepsPerRevolution );
 }

//else if (x_pos > 500 && x_pos < 529) { //X throttle center

 // motor_speed = 0;
 //}






else if (x_pos > 180 && x_pos < 500)  //X reverse


  {digitalWrite (dirPinStepper, LOW);
 digitalWrite (enablePinStepper, HIGH);

  motor_speed = map(x_pos, 520, 200, 100, 5000);
  motor_speed = constrain ( motor_speed, 10, 5000);

  motor_rechts = (motor_speed - faktorL1);
  motor_links = ( motor_speed  - faktorR1);

  motor_links = constrain ( motor_links, 10, 5000);
  motor_rechts = constrain ( motor_rechts, 10, 5000);

  
 myStepper.setSpeed(motor_rechts);
    // step 1/100 of a revolution:
    myStepper.step(StepsPerRevolution );
 
  }




else {
  motor_speed = 1 ;

  digitalWrite (enablePinStepper, LOW);
}


Serial.println(motor_rechts);














 


}