Hi,
I am using an Arduino mega 2560 to try to rotate a single dc motor with encoder a set number of rotations. I am using a Dual VNH2SP30 Motor Driver Carrier (link at the bottom) and an ASLONG JGY-370B-12V worm gear motor with encoder. I copied code from another post and adapted it to match the pins i was using but i am having some issues.
In the code below i am trying to make the motor do 2 rotations forwards at full speed, 2 rotations backwards at half speed and then stop.
// MD03A_Motor_basic + encoder
#define InA1 11 // INA motor pin
#define InB1 12 // INB motor pin
#define PWM1 13 // PWM motor pin
#define encodPinA1 21 // encoder A pin
#define encodPinB1 20 // encoder B pin
#define LOOPTIME 100 // PID loop time
#define FORWARD 1 // direction of rotation
#define BACKWARD 2 // direction of rotation
unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
volatile long count = 0; // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false; // motor moves
void setup() {
//Serial.begin(9600); // baud rate
//Serial.flush();
pinMode(InA1, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(encodPinA1, INPUT);
pinMode(encodPinB1, INPUT);
digitalWrite(encodPinA1, HIGH); // turn on pullup resistor
digitalWrite(encodPinB1, HIGH);
attachInterrupt(2, rencoder, FALLING); // 2 is pin 21
}
void loop() {
moveMotor(FORWARD, 255, 510*2); // direction, PWM, ticks number
delay(4000);
moveMotor(BACKWARD, 128, 510*2); // 510=360°
delay(4000);
//Serial.print("countInit : " );
//Serial.println(countInit);
}
void moveMotor(int direction, int PWM_val, long tick) {
countInit = count; // abs(count)
tickNumber = tick;
if(direction==FORWARD) motorForward(PWM_val);
else if(direction==BACKWARD) motorBackward(PWM_val);
}
// ********* Encoder counting ********
void rencoder() { // pulse and direction, direct port reading to save cycles NB so this might be the bit that tells if the motor is going forwards or backwards
if (PINB & 0b00000001) { count++; } //if (digitalRead(encodPinB1)==HIGH) count ++;
else { count--; } // if (digitalRead(encodPinB1)==LOW) count --;
if(run)
if((abs(abs(count)-abs(countInit))) >= tickNumber) motorBrake();
}
// ****Code for forward backward and brake ****
void motorForward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, LOW);
digitalWrite(InB1, HIGH);
run = true;
}
void motorBackward(int PWM_val) {
analogWrite(PWM1, PWM_val);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, LOW);
run = true;
}
void motorBrake() {
analogWrite(PWM1, 0);
digitalWrite(InA1, HIGH);
digitalWrite(InB1, HIGH);
run = false;
}
What actually happens is that it does the 2 rotations forwards, pauses to complete the delay, 2 rotations backwards, and then goes into a loop of pausing for 8 seconds then going backwards. Around half way through that 8 second pause, there is a spike in the PWM signal which makes the motor twitch but that is all, I have no idea what is causing this.
I think there is something wrong in the "rencoder" code but if i change it then the motor no longer rotates the specified amount of turns it just rotates for the duration of the delay. How cani make my motor complete the revolutions I want and then stop? I hope to add a second motor once i have accomplished this.
I look forwards to your responses.
Motor Driver: Pololu - Dual VNH2SP30 Motor Driver Carrier MD03A
Motor:http://jslmotor.en.alibaba.com/product/60248815875-211952029/Electric_12v_Dc_Motor_with_Gear_Reduction_JGY_370B_Worm_Gear_Motor_with_Encoder.html