Hello, I am having puzzling operation my DC motor project that only happens during power up or after reset.
First, a thank you to “KAS” on the forum( @ http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282384853) as I am using this code.
I am trying to control a DC motor to go to 3 discrete positions, using 3 buttons, as described below:
Push button 1: motor turns forwards 1 revolution
Push button 2: motor turns forwards 2 revolutions
Push button 3: motor turns backwards 1 revolution
This code is working wonderfully except right after reset – if I push button 3 first I get 1 rev backwards (which is correct), then if I push button 2 next I get 4 revs forwards, not my desired 2 revs forwards.
I tried a debounce after each button push, suspecting that button 2 may be incorrectly pressed twice, but any other combination of button pushes after a re-set works fine without a debounce, and after this initial incorrect 4 forward revs, then I can push buttons in any combination and the DC motor responds correctly and will never do the 4 revs again after pushing button #2.
I am using an Uno board,
using this motor driver board from Pololu: Pololu - Dual VNH2SP30 Motor Driver Carrier MD03A
And this motor with encoder from pololu. Pololu - 131:1 Metal Gearmotor 37Dx57L mm 12V with 64 CPR Encoder (No End Cap)
I admit to having a pretty poor level of understanding of everything that is going on in this code and I have tried a couple of things (for the last two days) based off hunches but to no avail.
The only thing that makes a change is if I change button #2 to only go 1 revolution, then the motor responds correctly to any combination of button pushes after a reset.
I tried to set up the serial read to help with trouble shooting, but soon realized I do not have the knowledge to set up variables to show my encoder direction and count #.
Below is my code, any suggestions or insight is appreciated.
// MD03A_Motor_basic + encoder
//credit to: KAS http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282384853
//
#define InA1 10 // Pin 10, goes to Pololu INA motor direction pin (low forward, high reverse)
#define InB1 11 // Pin 11, goes to Pololu INB motor direction pin (low reverse, high forward)
#define PWM1 6 // Pin 6, goes to Pololu PWM pin (speed)
#define encodPinA1 3 // Pin 3, reads encoder A signal (yellow)
#define encodPinB1 8 // Pin 8, reads encoder B signal (White)
#define FORWARD 1 //
#define BACKWARD 2 //
#define buttonPin A0 // Analog pin A0, reads Voltage of button 1, 2, or 3
//#define LOOPTIME 100 // PID loop time - what is this ?
unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
long count = 0; // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false; // motor moves
int buttonValue = 0; // defines switchValue as an interger, sets value at 0 (of 0 -1000)
//
void setup() {
pinMode(InA1, OUTPUT); // direction to Pololu motor board
pinMode(InB1, OUTPUT); // direction to Pololu motor board
pinMode(PWM1, OUTPUT); // PWM output to Pololu motor board
pinMode(encodPinA1, INPUT); // input from encoder
pinMode(encodPinB1, INPUT); // input from encoder
pinMode(buttonPin,INPUT); // pin A0, reads V input from buttons
digitalWrite(encodPinA1, HIGH); // turn on pullup resistor pin 10
digitalWrite(encodPinB1, HIGH); // turn on pullup resistor pin 11
attachInterrupt(1, rencoder, FALLING);
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() {
buttonValue = analogRead(buttonPin); // Read Voltage on pin A0
if(buttonValue>940) // Button #1 470 resistor 992 max (1023=5v)
moveMotor(FORWARD, 250, 2000*1); // direction, PWM (speed), ticks number (2000=360°)
buttonValue = analogRead(buttonPin);
if(buttonValue>750 && buttonValue<800) // Button #2 4.7k resistor 790 max, min = less than bttn #1
moveMotor(FORWARD, 250, 2000*2); // direction, PWM (speed), ticks number (2000=360°)
buttonValue = analogRead(buttonPin);
if(buttonValue>600 && buttonValue<630) // Button #3 10k resistor 620 max, min = less than bttn #2
moveMotor(BACKWARD, 250, 2000*1); // direction, PWM (speed), ticks number (2000=360°)
}
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);
}
void rencoder() { // pulse and direction, direct port reading to save cycles
if (PINB & 0b00000001) count++; // if(digitalRead(encodPinB1)==HIGH) count_r ++;
else count--; // if(digitalRead(encodPinB1)==LOW) count_r --;
if(run)
if((abs(abs(count)-abs(countInit))) >= tickNumber)
motorBrake();
}
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;
}