reset the encoders values of a DRO

Hi!

So I'm building a Digital Read Out for my lathe using two 1024 p/r rotary encoders. The values are sent to 2 LCDs. There's a switch thas switch the values from inches to mm. So far so good the peices of code I've put together works well but I want more! I'm trying to get the 3 readouts per LCD to be zeroed by pressing their respective button. I've searched for a couple days now but couldn't find any hint of a way to do that. As I'm fairly new to coding I don't know exactly what everything is in this code and what everything does but it does work well so far.

  #include <Wire.h>

  #include <LiquidCrystal_I2C.h>
  LiquidCrystal_I2C lcd1(0x27, 20, 4); // << Address 1
  LiquidCrystal_I2C lcd2(0x26, 20, 4); // << Address 2

  #include <Encoder.h>
  Encoder  myEnc1(1, 2); 
  Encoder  myEnc2(3, 4);

  const int unitSwitch = 5;
  const int zeroSwitch1A = 6;
  const int zeroSwitch1B = 7;
  const int zeroSwitch1C = 8;
  const int zeroSwitch2A = 9;
  const int zeroSwitch2B = 10;
  const int zeroSwitch2C = 11;
   
  
void setup()  {

  pinMode( unitSwitch, INPUT_PULLUP );
  pinMode( zeroSwitch1A, INPUT_PULLUP );
  pinMode( zeroSwitch1B, INPUT_PULLUP );
  pinMode( zeroSwitch1C, INPUT_PULLUP );
  pinMode( zeroSwitch2A, INPUT_PULLUP );
  pinMode( zeroSwitch2B, INPUT_PULLUP );
  pinMode( zeroSwitch2C, INPUT_PULLUP );

  
  Serial.begin( 9600 );
    
  lcd1.init();
  lcd2.init();

  lcd1.backlight();
  lcd2.backlight();
}

  long oldPosition = -999;
  
void loop() {
    
  const float Wv = (.1 * PI) / 1024; // Math formula for .37" diameter pulley
  //and 1024 pulse/revolution encoder.

  int unitSwitch1;
  int zeroSwitch1A;
  int zeroSwitch1B;
  int zeroSwitch1C;
  int zeroSwitch2A;
  int zeroSwitch2B;
  int zeroSwitch2C;

  Serial.println (zeroSwitch1A);
  
    unitSwitch1 = digitalRead( unitSwitch );
   // Serial.println( unitSwitch1 );

   
      long newPosition1 = myEnc1.read();
      if (newPosition1 != oldPosition)  

  if (unitSwitch1== HIGH) { 
 
  lcd1.setCursor(0, 0); // (Column 7,ROW 0) (Column #0-19,Row #0-3)
  lcd1.print("X AXIS");
  lcd1.setCursor(9, 0);
  lcd1.print("-INCH-");
  lcd1.setCursor(16, 0);
  lcd1.setCursor(0,1);
  lcd1.print("A"); 
  lcd1.setCursor(9, 1);
  lcd1.print(Wv * newPosition1, 3);//Math formula * Encoder reading ,3 decimals
  lcd1.setCursor(0, 2);
  lcd1.print("B");
  lcd1.setCursor(9, 2);
  lcd1.print(Wv * newPosition1, 3);
  lcd1.setCursor(0, 3);
  lcd1.print("C");
  lcd1.setCursor(9, 3);
  lcd1.print(Wv * newPosition1, 3);
    
    long newPosition2 = myEnc2.read();
    if (newPosition2 != oldPosition)

  lcd2.setCursor(0, 0);
  lcd2.print("Y AXIS   -INCH-");
  lcd2.setCursor(0,1);
  lcd2.print("A"); 
  lcd2.setCursor(9, 1);
  lcd2.print(Wv * newPosition2, 3);
  lcd2.setCursor(0, 2);
  lcd2.print("B");
  lcd2.setCursor(9, 2);
  lcd2.print(Wv * newPosition2, 3);
  lcd2.setCursor(0, 3);
  lcd2.print("C");
  lcd2.setCursor(9, 3);
  lcd2.print(Wv * newPosition2, 3);
  
 } 

  else {

    long newPosition1 = myEnc1.read();
    if (newPosition1 != oldPosition)  
      
  lcd1.setCursor(0, 0); // (Column 7,ROW 0) (Column #0-19,Row #0-3)
  lcd1.print("X AXIS");
  lcd1.setCursor(9, 0);
  lcd1.print("--MM--");
  lcd1.setCursor(16, 0);
  lcd1.setCursor(0,1);
  lcd1.print("A"); 
  lcd1.setCursor(9, 1);
  lcd1.print(Wv * newPosition1 * 25.4, 3);//Math formula * Encoder reading ,3 decimals
  lcd1.setCursor(0, 2);
  lcd1.print("B");
  lcd1.setCursor(9, 2);
  lcd1.print(Wv * newPosition1 * 25.4, 3);
  lcd1.setCursor(0, 3);
  lcd1.print("C");
  lcd1.setCursor(9, 3);
  lcd1.print(Wv * newPosition1 * 25.4, 3);
 

  long newPosition2 = myEnc2.read();
  if (newPosition2 != oldPosition)

  lcd2.setCursor(0, 0);
  lcd2.print("Y AXIS   --MM--");
  lcd2.setCursor(0,1);
  lcd2.print("A"); 
  lcd2.setCursor(9, 1);
  lcd2.print(Wv * newPosition2 * 25.4, 3);
  lcd2.setCursor(0, 2);
  lcd2.print("B");
  lcd2.setCursor(9, 2);
  lcd2.print(Wv * newPosition2 * 25.4, 3);
  lcd2.setCursor(0, 3);
  lcd2.print("C");
  lcd2.setCursor(9, 3);
  lcd2.print(Wv * newPosition2 * 25.4, 3);
  }
 }

As I'm fairly new to coding I don't know exactly what everything is in this code

It would be a great idea to go through the code line by line, and figure out what each line does.

You will need to study the library documentation in order to learn how to reset the encoder position. IF you happen to be using the encoder library from Paul Stoffregen, then start reading here.

You have a lot of redundant code there. Also, I'm not sure why you displaying the same value for A, B, and C? A more typical DRO would give you X,Y,Z on the three lines.

#include <Wire.h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 20, 4); // << Address 1
LiquidCrystal_I2C lcd2(0x26, 20, 4); // << Address 2

#include <Encoder.h>
Encoder  myEnc1(1, 2);
Encoder  myEnc2(3, 4);

const int unitSwitch = 5;
const int zeroSwitch1A = 6;
const int zeroSwitch1B = 7;
const int zeroSwitch1C = 8;
const int zeroSwitch2A = 9;
const int zeroSwitch2B = 10;
const int zeroSwitch2C = 11;


void setup()  {

  pinMode( unitSwitch, INPUT_PULLUP );
  pinMode( zeroSwitch1A, INPUT_PULLUP );
  pinMode( zeroSwitch1B, INPUT_PULLUP );
  pinMode( zeroSwitch1C, INPUT_PULLUP );
  pinMode( zeroSwitch2A, INPUT_PULLUP );
  pinMode( zeroSwitch2B, INPUT_PULLUP );
  pinMode( zeroSwitch2C, INPUT_PULLUP );


  Serial.begin( 9600 );

  lcd1.init();
  lcd2.init();

  lcd1.backlight();
  lcd2.backlight();
}

long oldPosition = -999;

void loop() {

  const float Wv = (.1 * PI) / 1024; // Math formula for .37" diameter pulley
  //and 1024 pulse/revolution encoder.

  float conversionFactor;

  int unitSwitch1;
  int zeroSwitch1A;
  int zeroSwitch1B;
  int zeroSwitch1C;
  int zeroSwitch2A;
  int zeroSwitch2B;
  int zeroSwitch2C;

  Serial.println (zeroSwitch1A);

  unitSwitch1 = digitalRead( unitSwitch );
  // Serial.println( unitSwitch1 );

  char *units;
  if (unitSwitch1 == HIGH ) {
    units = "-INCH-";
    conversionFactor = 1.0;
  } else {
    units = "--MM--";
    conversionFactor = 25.4
  }

  long newPosition1 = myEnc1.read();
  if (newPosition1 != oldPosition) {
    lcd1.setCursor(0, 0); // (Column 7,ROW 0) (Column #0-19,Row #0-3)
    lcd1.print("X AXIS   ");
    lcd1.print(units);
    lcd1.setCursor(0, 1);
    lcd1.print("A");
    lcd1.setCursor(9, 1);
    lcd1.print(Wv * newPosition1 * conversionFactor, 3);//Math formula * Encoder reading ,3 decimals
    lcd1.setCursor(0, 2);
    lcd1.print("B");
    lcd1.setCursor(9, 2);
    lcd1.print(Wv * newPosition1 * conversionFactor, 3);
    lcd1.setCursor(0, 3);
    lcd1.print("C");
    lcd1.setCursor(9, 3);
    lcd1.print(Wv * newPosition1 * conversionFactor, 3);
  }

  long newPosition2 = myEnc2.read();
  if (newPosition2 != oldPosition) {
    lcd2.setCursor(0, 0);
    lcd2.print("Y AXIS   ");
    lcd1.print(units);
    lcd2.setCursor(0, 1);
    lcd2.print("A");
    lcd2.setCursor(9, 1);
    lcd2.print(Wv * newPosition2 * conversionFactor, 3);
    lcd2.setCursor(0, 2);
    lcd2.print("B");
    lcd2.setCursor(9, 2);
    lcd2.print(Wv * newPosition2 * conversionFactor, 3);
    lcd2.setCursor(0, 3);
    lcd2.print("C");
    lcd2.setCursor(9, 3);
    lcd2.print(Wv * newPosition2 * conversionFactor, 3);
  }
}

Encoder myEnc1(1, 2);

Which Arduino?

I'm using an arduino Uno. And I actually don't want the same values for A, B and C, I actually want to be able to zero each of them independently, each with a button. I've put the same value because it was depressing seeing nothing on those lines, at least for now there's numbers moving... Someday they'll be bent to my will and get zero'ed on command !!!

Rather than have 2 displays that you are dealing with, you should have 1 display with the X axis info on line 1 and the Y axis info on line 2. wire up 2 buttons, one button zeros the X axis, the other button zeros the Y axis.

Here's a hint.

encoder stoffregen commands usage.png

encoder stoffregen commands usage.png

Pin 1 is the serial TX pin.

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