Using rotary Encoder to measure distance

Hey!

I really hope I’m posting this in the right section, I apologize if I have it wrong.

I’m working on a project to measure the length of the ends of my 3D printer filament spools so I can splice them together using my Palette+ from Mosaic Manufacturing.

The Palette+ comes with a rotary encoder so I’m hoping to use that, but I’m not entirely sure how to go about programming it properly. I’ve got it counting when the pulses turn on and off but it’s not very accurate unless it goes at the same speed every time. From what I’ve read I need to deal with interrupts but I am not super familiar with Arduino programming, I’m still learning.

Is what I’m trying to do even possible with just the encoder, should I handle this a different way, or do I need to set it up to feed at the same speed consistently?

Thanks for any insight!

Here is my code:

#include <Wire.h>  // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>

int Encoder_OutputA= 5;
int Encoder_OutputB= 6;

int Previous_Output;
int Encoder_Count;
int aState;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {

lcd.begin();
lcd.print("Filament Length"); //Intro Message line 1
lcd.setCursor(0, 1);
lcd.print("Counter"); //Intro Message line 2

delay(3000);
lcd.clear();


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

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

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

It depends on what type of encoder it is (please give us brand name and type number, or a link to the product), how many pulses it gives per rotation, and how fast it will be rotating.

And does it have built-in pullup resistors? If not, you should replace

  pinMode (Encoder_OutputA, INPUT);
  pinMode (Encoder_OutputB, INPUT);

with

  pinMode (Encoder_OutputA, INPUT_PULLUP);
  pinMode (Encoder_OutputB, INPUT_PULLUP);

What Arduino are you using?

Blackfin:
What Arduino are you using?

Sorry, I'm currently using an Uno but have a Mega 2560 handy if it will work better.

Erik_Baas:
It depends on what type of encoder it is (please give us brand name and type number, or a link to the product), how many pulses it gives per rotation, and how fast it will be rotating.

And does it have built-in pullup resistors? If not, you should replace

  pinMode (Encoder_OutputA, INPUT);

pinMode (Encoder_OutputB, INPUT);



with


pinMode (Encoder_OutputA, INPUT_PULLUP);
 pinMode (Encoder_OutputB, INPUT_PULLUP);

That is a very good question, it's all enclosed and I'm still trying to get it open so I can find out. I will see what I can figure out today.

Thanks!

From what I've read I need to deal with interrupts

No, you don't, especially if you are just beginning with programming. Interrupts usually create more problems than they solve.

Fix the immediate problem first by helping us understand what it is. Please read and follow the directions in the how to use the forum posts.

But, first: DON'T DO THIS. Clearing a display and printing is slow and causes you to miss encoder steps.

       Encoder_Count ++;
       lcd.clear();
       lcd.print(Encoder_Count);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.