I'm using a Arduino UNO (ATMega 328) with the AF Motorshield to drive two 12V stepper motors.
Each motor is controlled by a pot.
Everything works fine - except a strange behavior when the system is powered on.
In order to get it running I have to press the reset button first.
I need to have it powered-up by itself because it is hard to reach the reset in my hardware set-up.
I'm not sure whether I have done something wrong - PLEASE help ... >
Code: Select all
#include <AFMotor.h>
AF_Stepper motor_L(24, 2);
AF_Stepper motor_R(24, 1);
int val_R = 0;
int current_R = 0;
int diff_R = 0;
int potpin_R = 0;
int val_L = 0;
int current_L = 0;
int diff_L = 0;
int potpin_L = 1;
int jitter = 1;
void setup() {
motor_R.setSpeed(500);
motor_L.setSpeed(500);
}
void loop () {
val_R = analogRead(potpin_R);
val_L = analogRead(potpin_L);
val_R = map(val_R, 0, 511, 0, 780);
val_L = map(val_L, 0, 511, 0, 780);
diff_R = abs(current_R - val_R);
diff_L = abs(current_L - val_L);
if (diff_R < (jitter)){
diff_R = 0;
}
if(val_R > current_R){
motor_R.step(diff_R, FORWARD, SINGLE);
motor_R.release();
}
else if (val_R < current_R){
motor_R.step(diff_R, BACKWARD, SINGLE);
motor_R.release();
}
current_R = val_R;
if (diff_L < (jitter)){
diff_L= 0;
}
if(val_L > current_L){
motor_L.step(diff_L, FORWARD, SINGLE);
motor_L.release();
}
else if (val_L < current_L){
motor_L.step(diff_L, BACKWARD, SINGLE);
motor_L.release();
}
current_L = val_L;
delay(20);
}
Juergen