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);
}