Micro Gear Motor with Encoder Position Control

Hi
Im using a micro gear motor with a 12 CPR encoder. I am trying to use two buttons for motor control, one for clockwise and the other for counter clockwise. My goal is to control the step size of the gear motor. For example, pressing the clockwise button once will turn it clockwise to the desired position. Currently I have a code that seams to allow me to control step size, however, the motor moves on its own and is not controlled using the buttons. Attached is my code and schematic. Thanks

// 

#define InA1            10                      // INA motor pin
#define InB1            11                      // INB motor pin 
#define PWM1            6                       // PWM motor pin
#define encodPinA1      3                       // encoder A pin
#define encodPinB1      8                       // 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
long count = 0;                                 // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false;                                     // motor moves

void setup() {
 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(1, rencoder, FALLING);
}

void loop() {
 moveMotor(FORWARD, 100, 175);                        // direction, PWM, ticks number
 delay(3000);
 moveMotor(BACKWARD, 100, 175);                           // 464=360°
 delay(3000);
}

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

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

Multi-line if()s without braces are legal but they're very difficult to maintain. Get used to using braces ({}). They will save you a lot of hair-tearing debugging.

Then once you've done that, use the Arduino auto-formatter to clean up your indenting. It's very good. If it scrambles your code then your code was wrong to begin with.

Then we get to the real problem. You start the motor moving and then shut your eyes and ears and wait for 3 seconds before looking out of your hole. Yes, I can see you've given the dirty work to an interrupt but that's a very poor way to do it. Make your loop cycle quickly. Never use delay(). Have the loop check all the inputs thousands of times per second. Then it can make decisions quickly.

Read Robin's Planning and Implementing an Arduino Program. Look closely at his loop() function.

Right now the code only allows for backwards motion, although the forwards code is the same thing

// MD03A_Motor_basic + encoder

#define InA1            10                      // INA motor pin
#define InB1            11                      // INB motor pin 
#define PWM1            6                       // PWM motor pin
#define encodPinA1      3                       // encoder A pin
#define encodPinB1      8                       // encoder B pin

#define LOOPTIME        100                     // PID loop time
#define FORWARD         12                      // direction of rotation
#define BACKWARD        13                       // direction of rotation

int button = 0;

unsigned long lastMilli = 0;                    // loop timing
unsigned long lastMilliPrint = 0;               // loop timing
long FORWARD_HELD;
long BACKWARD_HELD;
long current_time;
long count = 0;                                 // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false;                                     // motor moves

void setup() {
  Serial.begin(9600);
  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(1, rencoder, FALLING);
}

void moveMotor(int direction, int PWM_val, long tick)  {
  countInit = count;    // abs(count)
  tickNumber = tick;
  if (direction == FORWARD) {
    motorForward(PWM_val);
    Serial.println("motorForward");
  }
  else if (direction == BACKWARD) {
    //  Serial.println("motorBackward initiated");
    motorBackward(PWM_val);
  }
}

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);
  // Serial.println(InA1);
  // Serial.println(InB1);
  digitalWrite(InA1, HIGH);
  // Serial.println(InA1);
  // Serial.println(InB1);
  digitalWrite(InB1, LOW);
  Serial.println(InB1);
  run = true;
}

void motorBrake()  {
  analogWrite(PWM1, 0);
  digitalWrite(InA1, HIGH);
  digitalWrite(InB1, HIGH);
  run = false;
}

void loop() {
  button = digitalRead(FORWARD);
  Serial.println(button);
  if ( digitalRead(FORWARD) == 0) {
    Serial.print("forward button pressed");
    moveMotor(FORWARD, 255, 50);
    FORWARD_HELD = millis();
    if (FORWARD_HELD == 3000) {
      digitalWrite(InA1, HIGH);
      digitalWrite(InB1, HIGH);
    }
  }

  else if ( digitalRead(BACKWARD) == 0) {
    Serial.println("backward button pressed");
    moveMotor(BACKWARD, 255, 50);
    BACKWARD_HELD = millis();
    if (BACKWARD_HELD == 3000) {
      digitalWrite(InA1, HIGH);
      digitalWrite(InB1, HIGH);
    }
  }
}

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