What code would I need for Arduino UNO to keep track of rotary encoder pulses?

What I am looking to accomplish is to have our machine pull metal straps under a blade to be cut at different lengths (24", 32", 48",and 60"). Obviously I’m making a different program for each length by tweaking the code. I do have some code written but I’m looking for help(I will place what I have at the bottom). I’m not sure how to code the Arduino to keep track of the number of pulses the encoder is sending. Right now I have the code for counting which I think is part of what I need. I do have an lcd shield as well and would also like if it could keep track of how many cuts are made, but that’s not my main priority right now.

The base of the process I’m going for contains the following…

  1. Rotary Encoder is attached to a rotating shaft
  2. Arduino counts the pulses from the encoder till it receives X amount of pulses to get 24" of material
  3. X amount of pulses is recognized then Arduino sends signal to a relay
  4. Relay sends power to solenoids for a 1/4 of a second
  5. Solenoids pull a blade down and cut the material
  6. Repeat

Parts I’m working with are as follow…

  1. Arduino UNO
  2. 1602 V2 LCD
  3. Beefcake Relay
  4. YUMO 1024 P/R Incremental Rotary Encoder
#include <LiquidCrystal.h>

// Program to cut 24 inch strips
// Lcd will read company name then program name and clear
// Process will start
// Rotary encoder will rotate on shaft sending pulses to Arduino
// Ardunio will recognize 7,800 pulses then send signal to relay
// Relay will send stronger signal to solenoids to engage them for .25 seconds
// Solenoids will pull the blade down
// Repeat

 * Power LCD and Rotary encoder from the +5V pin of Arduino
 * LCD RS -> pin 4
 * LCD EN -> pin 6
 * LCD D4 -> pin 11
 * LCD D5 -> pin 12
 * LCD D6 -> pin 13
 * LCD D7 -> pin 14
 * Encoder OutputA -> pin 2
 * Encoder OutputB -> pin 3

int Encoder_OutputA= 2;
int Encoder_OutputB= 3;

int Previous_Output;
int Encoder_Count;

const int RELAY_PIN = 7;

// Pins being used by lcd
LiquidCrystal lcd(4,6,11,12,13,14);

void setup() {

lcd.print(" G-O-Metric "); //Intro Message line 1
lcd.setCursor(0, 1);
lcd.print("  24 Inch Strips  "); //Intro Message line 2


//Encoder pin Mode declaration
  pinMode (Encoder_OutputA, INPUT);
  pinMode (Encoder_OutputB, INPUT);

Previous_Output = digitalRead(Encoder_OutputA); //Read the inital value of Output A

//Relay pin Mode declaration

void loop() {
     if (digitalRead(Encoder_OutputA) != Previous_Output)
     if (digitalRead(Encoder_OutputB) != Previous_Output)
       Encoder_Count ++;
       lcd.setCursor(0, 1); 
       lcd.setCursor(0, 1); 
       lcd.print("Anti - Clockwise");
    Previous_Output = digitalRead(Encoder_OutputA);

     // Turn the relay on for 1/4 of a second 
  digitalWrite(RELAY_PIN, HIGH);
  digitalWrite(RELAY_PIN, LOW);


I'm not sure how to code the Arduino to keep track of the number of pulses the encoder is sending.

What do you see when you print the value of Encoder_Count ?

Have a look at this

Quadrature encoders derive their name from the four possible states that they can assume, if only momentarily. They have two outputs, typically named A and B each can be logic high or low thus giving us the four combinations (11, 10, 01, 00). Internally, these encoders are likely to have two disks (or one disk with two sensors), one for each A and B, that are slightly out of phase. Using mechanical or optical sensors, as the encoder is rotated so the signals vary on each output with a delay that represents the phase difference. In one direction, A change prior to B and the reverse in the opposite direction. The only stable state, which is commonly referred to as detente, is when the encoder is at one of its rest positions and both signals are at their respective rest values. For this discussion we'll assume signals at rest to be at a logic 1, hence the detente value would be 0b11 or 3. As the encoder leaves detente, first one signal will drop to zero, giving us 0b10 or 0b01 (2 or 1). Next we'll pass through a phase where both signals are zero and then through the reverse state from the start before returning to detente.

The two sequences can therefore be described (and tested for) as starting at detente 3, passing through 102 or 201 depending on direction and returning to detente 3. Your four interrupt hits are then 1-0-2-3 or 2-0-1-3.

Each time the interrupt fires, it will reflect a state change on one of the two pins. If you read the port input pins, masking those that map to your A and B signals, you'll get 0, 1, 2 or 3, Keeping track of the sequence will tell you which direction the encoder is being rotated.

I just Googled "Arduino rotary encoder sketches" and got 153,000 hits. I'll bet some of those would help answer your questions.

What do you see when you print the value of Encoder_Count ?

I'm not seeing anything but a blank screen with the code I have.

Have you tried a simple "Hello World" program to test whether the LCD is working ?