Home > Genel > Aduino Stepper Motor Acceleration and Rotary Encoder Control

Aduino Stepper Motor Acceleration and Rotary Encoder Control

 

#include <AccelStepper.h>

int motorSpeed = 10000; //maximum steps per second (about 5rps / at 8 microsteps)
int motorAccel = 3600; //steps/second/second to accelerate

const unsigned char ttable[7][4] = {
{0x0, 0x2, 0x4, 0x0}, {0x3, 0x0, 0x1, 0x10},
{0x3, 0x2, 0x0, 0x0}, {0x3, 0x2, 0x1, 0x0},
{0x6, 0x0, 0x4, 0x0}, {0x6, 0x5, 0x0, 0x20},
{0x6, 0x5, 0x4, 0x0},
};

#define DT 2
#define CLK 3
#define SW 4
#define DIR_CCW 0x10
#define DIR_CW 0x20
int motorStepPin = 8; //digital pin 3
int motorDirPin = 9; //digital pin 2
const int Enable_PIN = 10;
AccelStepper stepper(1, motorStepPin, motorDirPin);
volatile unsigned char state = 0;

void setup(){
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
  pinMode(Enable_PIN, OUTPUT);
  digitalWrite(Enable_PIN, LOW);
  stepper.moveTo(8000); //move 32000 steps (should be 10 rev)
  Serial.begin(9600);
  pinMode(DT, INPUT);
  pinMode(CLK, INPUT);
  pinMode(SW, INPUT);
  digitalWrite(SW, HIGH);
}

void loop(){
 int counter;
 unsigned char result;
  /* Reset the counter */
 counter = 0;

  while(1)
  {
    /* Read the status of the dial */
    unsigned char pinstate = (digitalRead(CLK) << 1) | digitalRead(DT);
    state = ttable[state & 0xf][pinstate];
    result=state&0x30;
    if(result==DIR_CCW) counter++;
        if(result==DIR_CW) counter--;
 // put some magnification  hera about 10 times
   stepper.moveTo(counter*10);
    stepper.run();
  }
}

References:
http://www.airspayce.com/mikem/arduino/AccelStepper/index.html
http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html

Categories: Genel
  1. Carl
    25 January 2016 at 04:08

    Do you have the schematic of how everything is plugged together?

  2. aruntum nonjun
    30 April 2016 at 13:45

    I want to use the Encoder h38s600 arduino to do with me

  3. 30 April 2016 at 13:55

    I want to use the Encoder h38s600 arduino to do with me

  4. parabellium
    29 October 2017 at 16:42

    Merhaba, kod için teşekkür ederim. Ancak motor 10 tur attıktan sonra kendi kendine geriye doğru dönmeye başlıyor. Neden olabileceği konusunda bir fikriniz var mı ?

  1. 15 March 2015 at 22:58

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: