Sketch Improvement Advice. Basic Encoder LCD Counter

Hi,

I would like to ask for some advice on possibly improving my sketch.

I’ve been playing around with a Keys Rotary Encoder Module/Breakout Board, a 20,4 LCD and the PJRC Encoder Library.

I wanted to achieve 1 count for every detent/click of the Encoder.

I managed to get the sketch working on the LCD too my liking, but kept thinking that it could be improved. So I’ve tried to improve the sketch. It still works, but I now get +0 instead of 00 for the first reading. Anti Clockwise goes to -1,-2 and Clockwise goes +1,+2 etc.

I’ve left “commented-out” what the unimproved sketch was, and the last 8 lines of the sketch are my ‘so-called’ improvements.

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/
#include <LiquidCrystal.h>
#include <Encoder.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(20, 21);
//   avoid using pins with LEDs attached

void setup()
{
  lcd.begin(20, 4);
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  lcd.print("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition / 4);
    lcd.setCursor(0, 1);
    lcd.print("Test = ");
    /*lcd.setCursor(7, 1);
    if (newPosition >= 1) {
      lcd.print("+");
    }
    if (newPosition == 0) {
      lcd.print("0");
    }
    lcd.print(newPosition / 4);
    delay(50);
    lcd.print("  ");
    }
    }*/
    if (newPosition >= 1 || newPosition ==0)
    {
      lcd.print("+") || ("0");
    }
    lcd.print (newPosition / 4);
    lcd.print("  ");
  }
}

I’d be grateful for some pointers.

Dizzwold.

You haven't really specified the desired output format completely.

Hi,

I would like it to start with 00 then Clockwise to increment by 1 for every detent/click to +1, +2, +3 etc and the same for Anti Clockwise from 00 to -1, -2, -3 etc.

Dizzwold.

      lcd.print("+") || ("0");

What do you expect this line to do? Why not use the code from the commented-out section which explicitly prints an extra zero when the position is zero? Follow that pattern. Explicitly print a plus when the position is greater than zero.