Rotary Encoder Moving Stepper Motor

Hi, I am new to these forums and also a bit of a beginner to Arduino. I have been using the Arduino library as a means to piece bits of coding together to create my concept prototype. I have got it all working, however it doesn’t perform exactly how I want it to.

I am using rotary encoders to turn stepper motors left and right. The main issue I am having is the rotary encoders are giving a constant movement in the motors in between steps of the 12 step encoder and stopping on the complete step. Instead I want the rotary encoder to move the motor a set angle on the step of the encoder then stop. Every click moves the motor a set distance.

This is the encoder I am using:

I assume it would be this section that needs adapted:
void loop(){
{rotEnc();
if (turnCount > 0)
{stepperx.step(2);
delay(10);}
if (turnCount < 0)
{stepperx.step(-2);
delay(10);}

Could anyone point me in the right direction? Greatly appreciate the help.

Here is the code I am using:
//2xStepper
#include <Stepper.h>
#define STEPS 200
Stepper steppery(STEPS, 4, 5, 6, 7);
Stepper stepperx(STEPS, 8, 9, 10, 11);

//Vibrating Motors with Counter
const int buttonPin = 3;
const int ledPin = 12;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

//rotary encoders
int inputPin1 = A0; // A
int inputPin2 = A1; // B
int inputPin3 = A2; // A
int inputPin4 = A3; // B
int val1 = 0, val2 = 0;
int oldVal1 = 0, oldVal2 = 0;
int pos = 0, oldPos = 0;
int turn = 0, oldTurn = 0, turnCount=0;
int val1B = 0, val2B = 0;
int oldVal1B = 0, oldVal2B = 0;
int posB = 0, oldPosB = 0;
int turnB = 0, oldTurnB = 0, turnCountB=0;

void rotEnc ()
{ val1 = digitalRead(inputPin1);
val2 = digitalRead(inputPin2);
// Detect changes
if ( val1 != oldVal1 || val2 != oldVal2) {
//for each pair there’s a position out of four
if ( val1 == 1 && val2 == 1 ) // stationary position
pos = 0;
else if ( val1 == 0 && val2 == 1 )
pos = 1; else if ( val1 == 0 && val2 == 0 )
pos = 2; else if ( val1 == 1 && val2 == 0 )
pos = 3;

turn = pos-oldPos;

if (abs(turn) != 2) // impossible to understand where it’s turning otherwise.
if (turn == -1 || turn == 3)
turnCount++;
else if (turn == 1 || turn == -3)
turnCount–;

if (pos == 0){ // only assume a complete step on stationary position
if (turnCount > 0)
Serial.print("<");
else if (turnCount < 0)
Serial.print(">");
turnCount=0;
}

oldVal1 = val1;
oldVal2 = val2;
oldPos = pos;
oldTurn = turn;
}
}

void rotEncB ()
{ val1B = digitalRead(inputPin3);
val2B = digitalRead(inputPin4);
// Detect changes
if ( val1B != oldVal1B || val2B != oldVal2B) {
//for each pair there’s a position out of four
if ( val1B == 1 && val2B == 1 ) // stationary position
posB = 0;
else if ( val1B == 0 && val2B == 1 )
posB = 1; else if ( val1B == 0 && val2B == 0 )
posB = 2; else if ( val1B == 1 && val2B == 0 )
posB = 3;
turnB = posB-oldPosB;
if (abs(turnB) != 2) // impossible to understand where it’s turning otherwise.
if (turnB == -1 || turnB == 3)
turnCountB++;
else if (turnB == 1 || turnB == -3)
turnCountB–;
if (posB == 0){ // only assume a complete step on stationary position
if (turnCountB > 0)
Serial.print("<");
else if (turnCountB < 0)
Serial.print(">");
turnCountB=0; }
oldVal1B = val1B;
oldVal2B = val2B;
oldPosB = posB;
oldTurnB = turnB;
}
}

void setup() {

stepperx.setSpeed(10);
steppery.setSpeed(10);

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

pinMode(inputPin1, INPUT);
digitalWrite(inputPin1, HIGH);
pinMode(inputPin2, INPUT);
digitalWrite(inputPin2, HIGH);
pinMode(inputPin3, INPUT);
digitalWrite(inputPin3, HIGH);
pinMode(inputPin4, INPUT);
digitalWrite(inputPin4, HIGH);
Serial.begin(9600);}

void loop(){
{rotEnc();
if (turnCount > 0)
{stepperx.step(2);
delay(10);}
if (turnCount < 0)
{stepperx.step(-2);
delay(10);}

}

{rotEncB();
if (turnCountB > 0)
{steppery.step(2);
delay(10);}
if (turnCountB < 0)
{steppery.step(-2);
delay(10);}
}

{
//button loop
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

if(lastButtonState != buttonState){
if(buttonState == LOW){
Serial.println(“Button is OFF”);
//print button is off if button is not pushed and a LOW is returned

}
else{
delay(500);
Serial.println(“Button was turned ON”);
buttonPushCounter++;
delay(1000);
Serial.print(“Times button was pushed “);
Serial.print(buttonPushCounter);
Serial.println(” times”);

}
}
lastButtonState = buttonState;

if(buttonPushCounter%2 == 0 && buttonPushCounter != 0){
digitalWrite(ledPin, HIGH);

}else{
digitalWrite(ledPin, LOW);
}
}}

Put each { on a separate line. Put each } on a separate line. Properly indent all the code in between.

Put each statement on it’s own line.

Your code is difficult to read. The compiler doesn’t care about white space, but we do.

Use { only where they are needed, but use them everywhere they are needed.

{rotEncB();
if (turnCountB > 0)
{steppery.step(2);
delay(10);}
if (turnCountB < 0)
{steppery.step(-2);
delay(10);}
}

  {
//button loop

The { before the rotEncB() call is not needed, nor is the one before the comment.

if (abs(turnB) != 2) // impossible to understand where it's turning otherwise.     
if (turnB == -1 || turnB == 3)       
turnCountB++;     
else if (turnB == 1 || turnB == -3)       
turnCountB--;

This code, on the other hand would be a lot more readable WITH curly braces.

void rotEnc () 
void rotEncB ()

You got a problem with A? If one function gets a suffix, the other should too.