Unable to print data in both line of 16X2 display

I am working with a quadrature encoder connected to DC motor. I am trying to display speed and time for which motor is on. I got a 16X2 Display board of which I want to use line 1 for Speed display and line 2 for time. Unfortunately I am unable to do so. Can anyone help me in getting where I am wrong.
The code is below:

#define encoder0PinA  2
#define encoder0PinB  4

volatile long encoder0Pos=0;
long newposition;
long oldposition = 0;
unsigned long newtime;
unsigned long oldtime = 0;
long vel;

void setup()
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor
  attachInterrupt(0, doEncoder, RISING);  // encoDER ON PIN 2
  Serial.begin (9600);


void loop()
newposition = encoder0Pos;
newtime = millis();
vel = (newposition-oldposition) * 1000 /(newtime-oldtime);
oldposition = newposition;
oldtime = newtime;

Serial.print("speed = ");


void doEncoder()
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
  } else {

void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.write(0xFE);   //command flag
   Serial.write(128);    //position
void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.write(0xFE);   //command flag
   Serial.write(192);    //position
void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
if (position<16){ Serial.write(0xFE);   //command flag
              Serial.write((position+128));    //position
}else if (position<32){Serial.write(0xFE);   //command flag
              Serial.write((position+128+64-20));    //position 
} else { goTo(0); }

void clearLCD(){
   Serial.write(0xFE);   //command flag
   Serial.write(0x01);   //clear command.
void backlightOn(){  //turns on the backlight
    Serial.write(0x7C);   //command flag for backlight stuff
    Serial.write(157);    //light level.
void backlightOff(){  //turns off the backlight
    Serial.write(0x7C);   //command flag for backlight stuff
    Serial.write(128);     //light level for off.
void serCommand(){   //a general function to call the command flag for issuing all other commands   

The sketch indicates you have a serial LCD display, in that case you have probably wired it to the serial port. Depending on the Arduino you are using, that serial port is also used for serial upload of the sketch and perhaps that is upsetting the display?

More info will help in diagnosing your problem, otherwise folk will have to make a lot of assumptions:

  • Which Arduino board
  • Which version of the IDE
  • A link to the vendors we page for the LCD
  • Details of exactly how you have wired everything up, a photo also helps

Where are you getting the magic numbers for the cursor position?

There are 80 valid addresses (positions) but only 32 of them are visible on a 16x2 display. Perhaps those magic numbers are putting the characters where they aren't visible.


The magic numbers work with a SparkFun display as it appears the code is copied from here, but we don't know if optim_biku has one of those...

That still doesn't explain the derivation of the magic numbers which I have figured out. It looks like they are correct if not a bit obscure.

What they have done is take the hex address specified in the datasheet, set the high bit which converts the address into an instruction, and then change the radix to decimal for some unexplained reason, perhaps because the author has ten fingers.