CONTROL STEPPER WITH ROTARY ENCODER

STEPPER encoder uln

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/Stepper.zip

#include <AccelStepper.h>

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

#define HALFSTEP 8

// Motor pin definitions
#define motorPin1 12 // IN1 on the ULN2003 driver 1
#define motorPin2 11 // IN2 on the ULN2003 driver 1
#define motorPin3 10 // IN3 on the ULN2003 driver 1
#define motorPin4 9 // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

volatile unsigned char state = 0;

void setup(){
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(10000.0);
stepper.setSpeed(1000);
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 here about 100 times
stepper.moveTo(counter*100);
stepper.run();
}
}

2 thoughts on “CONTROL STEPPER WITH ROTARY ENCODER

  1. in the function “if (result == DIR_CW) counter–;” it should be “if (result == DIR_CW) counter–-;”. Rest ok! Best regards.

    Like

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 )

Connecting to %s