Arduino - Rotary encoder not turning my Stepper motor

Just a quick thought, you set previous position equal to rotary position at one point in the code. Later you compare them and require them to be different in order to rotation.

I am looking at this on my phone so I might have missed some of the brackets.

Also, a little commenting of the code would be helpful when asking others to review your code.
 

Thread Starter

Dan Leo

Joined Dec 4, 2016
4
Code:
#include "Stepper.h"
#define STEPS 32

volatile boolean TurnDetected;
volatile boolean rotationdirection;

const int PinCLK=2;
const int PinDT=3;
const int PinSW=4;

int RotaryPosition=0;

int PrevPosition;
int StepsToTake;

Stepper small_stepper(STEPS, 8, 10, 9, 11);

void isr () {
  delay(4);
  if (digitalRead(PinCLK))
     rotationdirection= digitalRead(PinDT);
     else
     rotationdirection= !digitalRead(PinDT);
     TurnDetected = true;
}
void setup() {
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH);
attachInterrupt (0,isr,FALLING);

}

void loop() {
small_stepper.setSpeed(600);
if (!(digitalRead(PinSW))) {
  if (RotaryPosition == 0) {
  } else {
    small_stepper.step(-(RotaryPosition*50));
  }
}
if (TurnDetected) {
  PrevPosition = RotaryPosition;
  if (rotationdirection) {
    RotaryPosition=RotaryPosition-1;}
    else {
      RotaryPosition=RotaryPosition+1;}

      TurnDetected = false;
      if ((PrevPosition + 1) == RotaryPosition) {
        StepsToTake=50;
        small_stepper.step(StepsToTake);
  }
  if ((RotaryPosition + 1) == PrevPosition) {
    StepsToTake=-50;
    small_stepper.step(StepsToTake);
  }
}
}
 
Have you tested any of the individual parts of the code to verify that they do what you expect them to do with the was you have the circuits connected?

Can you rotate the stepper without the encoder?

Can the arduino see the signals from your encoder and interpret them as you like?

Does you interrupt work?
 
Top