Go Down

Topic: control dc gear motor using a rotary encoder. (Read 1 time) previous topic - next topic

claudilla

Mar 29, 2018, 05:17 am Last Edit: Mar 29, 2018, 06:04 am by claudilla
I have a rotary encoder and a DC gear motor connected to my Arduino UNO. I would like to rotate the DC motor at a certain angle. As I right now, I have written a code that reads the position of my encoder as I rotate it and prints our the position and the angle.

I would like to rotate the dc motor at a certain angle. For example, if I input 90 degrees my motor should rotate 90 degrees and stop.
 
- I'm using pin 2 and pin 3 for my channel A and B (ENCODER)
- I'm using pin 9 to control my dc motor.


I have no clue how to approach this... any thoughts?

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_case = 0;
volatile int timeToRun = 0;
int motorPin = 6;


#define BTN_RIGHT 0
#define BTN_LEFT 1
#define BTN_UP 2
#define BTN_DOWN 3
#define BTN_SELECT 4
#define BTN_NONE 5
#define SELECT 6
#define RESET 7
#define PIN


#define encoderPinA 2
#define encoderPinB 3
#define CPR 256
volatile int counter =0;
volatile boolean flag;

volatile int var_degrees =0;

void setup() {
  pinMode(3, OUTPUT);
 
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  Serial.begin (9600);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), isr_2, RISING);
  lcd.clear();
 
}

void loop() {
 
   if(flag == true){     
        var_degrees = ((360/256.0)*counter);
        Serial.println(var_degrees);
        //lcd.setCursor(0, 1);
        //lcd.print("Degrees: ");
        //lcd.setCursor(9, 1);
        //lcd.print(var_degrees);
        flag = false;
  }

  lcd_case = readButtons();
  int  read_button = analogRead (0);
  //Depending on which button we pressed, we performan action
  switch (lcd_case) {

    case BTN_RIGHT:
      {
        break;
      }

    case BTN_LEFT:
      {
        break;
      }
    case BTN_UP:
      {
       
        timeToRun = timeToRun +1;
     
        lcd.setCursor(0, 1);
        lcd.print("Degrees: ");
        lcd.setCursor(9, 1);
        lcd.print(timeToRun);
        delay(500);
        break;

      }
    case BTN_DOWN:
      {

        if (timeToRun > 0) {
          timeToRun = timeToRun - 1;

          lcd.setCursor(0, 1);
          lcd.print("Degrees: ");
          lcd.setCursor(9, 1);
          lcd.print(timeToRun);
          delay(500);
          break;

        }

      }

    case BTN_SELECT:
      {

        analogWrite(motorPin, 255);
       
// ROTATE MOTOR BASED ON ANGLE IN ENCODER
        analogWrite(motorPin, 0);
        break;

      }

    case RESET:
      {
        break;
      }
  }
   
}
//Interrupts

void isr_2(){

 
flag = true;
  if(digitalRead(encoderPinA) == HIGH){
    if(digitalRead(encoderPinB) == LOW){
      counter = counter -1; //COUNTER CLOCK WISE
    }
    else{
      counter = counter +1; //CLOCK WISE
    }
  }
  else{ //IF PIN A IS LOW
    if(digitalRead(encoderPinB) == LOW){
      counter = counter +1; //CLOCK WISE
    }
    else{
      counter = counter -1 ; //COUNTER CLOCK WISE
    }
  }
}



int readButtons() {

  int read_button;
  read_button = analogRead (0);

  if (read_button < 50) {
    return BTN_RIGHT;
  }
  if (read_button < 195) {

    return BTN_UP;

  }
  if (read_button < 400) {
    return BTN_DOWN;

  }
  if (read_button < 600) {
    return BTN_LEFT;
  }
  if (read_button < 800) {
    return BTN_SELECT;
  }
  else
    return BTN_NONE;
}

Paul_KD7HB

I have a rotary encoder and a DC gear motor connected to my Arduino UNO. I would like to rotate the DC motor at a certain angle. As I right now, I have written a code that reads the position of my encoder as I rotate it and prints our the position and the angle.

I would like to rotate the dc motor at a certain angle. For example, if I input 90 degrees my motor should rotate 90 degrees and stop.
 
- I'm using pin 2 and pin 3 for my channel A and B (ENCODER)
- I'm using pin 9 to control my dc motor.


I have no clue how to approach this... any thoughts?


First, is the encoder on the gear output shaft or on the motor? Are you asking to turn the motor 90 degrees or the gear output shaft?

IF the encoder is on the gear output shaft and that is what you want to turn 90 degrees, divide the number of counts from the encoder for a complete turn by 4 and count that number. You have to convert degrees to some number of encoder counts. And you can't go a fraction of a count.

Then you will ask how to stop the motor right on that count? Pretty hard to do with what you have there. Need a brake of some sort.

Paul


Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up