LCD not printing in loop fuction

I want to start off with I promise I have read several posts about getting the lcd to work and none of them had my problem. I know this is a real newb question but I really need this working with in the next few days and i think it something really easy that i am over looking :(. I have read at least 4 tutorials on the simple 2x16 LCD’s and I have gotten the lcd to write to the screen however, I cannot get it to display changing variables. I am reading an optical encoder and printing the value to the serial screen just fine, but when I call lcd.print it doesn’t do anything. Here is my code, igrnore all the optical encoder code. The problem, I think, is in the void loop function. Thanks to everyone in advance

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

enum PinAssignments {
  encoderPinA = 2,
  encoderPinB = 3,
  clearButton = 8
};
int encoderPos = 0;

unsigned int lastReportedPos = 1;

boolean A_set = false;
boolean B_set = false;


void setup() {
// set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
// Print a message to the LCD.s
  lcd.print("Input        Aux");

  pinMode(encoderPinA, INPUT); 
  pinMode(encoderPinB, INPUT); 
  pinMode(clearButton, INPUT);
  digitalWrite(clearButton, HIGH);

// encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoderA, CHANGE);
// encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, doEncoderB, CHANGE);

  Serial.begin(9600);
}


void loop(){ 
if (lastReportedPos != encoderPos){
  Serial.print("Index:");
  Serial.print(encoderPos);
  Serial.println();
  lastReportedPos = encoderPos;
  lcd.setCursor(0,1);
  lcd.print(encoderPos);
}
  if (digitalRead(clearButton) == LOW){
    encoderPos = 0;
  }
}

// Interrupt on A changing state
void doEncoderA(){
// Test transition
  A_set = digitalRead(encoderPinA) == HIGH;
// and adjust counter + if A leads B
  encoderPos += (A_set != B_set) ? +1 : -1;
 
  if(encoderPos > 510){
    encoderPos = 510;
  }
  else if(encoderPos < 0){
    encoderPos = 0;
  }
}

// Interrupt on B changing state
void doEncoderB(){
// Test transition
  B_set = digitalRead(encoderPinB) == HIGH;
// and adjust counter + if B follows A

  encoderPos += (A_set == B_set) ? +1 : -1;

  encoderPos += (A_set != B_set) ? +1 : -1;
  if(encoderPos >= 510){
    encoderPos = 510;
  }
  else if(encoderPos <= 0){
    encoderPos = 0;
  }
}

encoderPos is an int, does the lcd.print() func expect ints or printable chars/strings?


Rob

it should be fine with an int. This is off the arduino site for lcd.print()

print()

Description
Prints text to the LCD.

Syntax
lcd.print(data)
lcd.print(data, BASE)

Parameters
lcd: a variable of type LiquidCrystal
data: the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

I have gotten the lcd to write to the screen ...

Have you gotten it to write to the second line of the screen?

Have you tried running the stuff in loop only once by temporarily moving it to setup() and having an empty loop(). This would tell you if you have indeed written stuff to the second line and then later covered it up.

Have you tried putting a really long delay at the end of loop. This could also tell you the same thing.

Don

Yes i have. In the setup i can do something like:
lcd.begin(16, 2);
lcd.print(“Input Aux”);
lcd.setCursor(5,0);
lcd.print(“WTFFFFF”);

and it prints on the second line just fine. Also, I have uploaded my old code to the Arduino which was a capacitor meter and the
variable that stores the value of the capacitance prints on the second line just fine. I am using the lcd in the EXACT same way
and its not working.
Does it have to do with the interups I’m using for the reading of A and B of the optical encoder?

here is the code for my capacitor meter that works perfectly!

#include <LiquidCrystal.h>

#define analogPin      0          // analog pin for measuring capacitor voltage
#define chargePin      13         // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin   4         // pin to discharge the capacitor
#define resistorValue  100500.0F   // change this to whatever resistor value you are using
                                  // F formatter tells compliler it's a floating point value
                                  

unsigned long startTime;
unsigned long elapsedTime;
float microFarads;                // floating point variable to preserve precision, make calculations
float nanoFarads;
float fudgefactor;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup(){
  
    // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Like A Boss!");
  
  pinMode(chargePin, OUTPUT);     // set chargePin to output
  digitalWrite(chargePin, LOW);  

  Serial.begin(9600);             // initialize serial transmission for debugging
}

void loop(){
  digitalWrite(chargePin, HIGH);  // set chargePin HIGH and capacitor charging
  startTime = micros();

  while(analogRead(analogPin) < 648){       // 647 is 63.2% of 1023, which corresponds to full-scale voltage 
  }

  elapsedTime= micros() - startTime;
 // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (1000)  
  microFarads = ((float)elapsedTime / resistorValue) ; 
  Serial.print(elapsedTime);       // print the value to serial port
  Serial.print(" uS    ");         // print units and carriage return


  if (microFarads > 1){
    Serial.print((long)microFarads);       // print the value to serial port
    Serial.println(" microFarads");         // print units and carriage return
        // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(microFarads);
  lcd.setCursor(14, 1);
  lcd.print("uF");
  }
  else
  {
    // if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad). 
    // This is  a workaround because Serial.print will not print floats

    nanoFarads = microFarads * 1000.0;      // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
    Serial.print((long)nanoFarads);         // print the value to serial port
    Serial.println(" nanoFarads");          // print units and carriage return
        // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(nanoFarads);
  lcd.setCursor(14, 1);
  lcd.print("nF");
  }

  /* dicharge the capacitor  */
  digitalWrite(chargePin, LOW);             // set charge pin to  LOW 
  pinMode(dischargePin, OUTPUT);            // set discharge pin to output 
  digitalWrite(dischargePin, LOW);          // set discharge pin LOW 
  while(analogRead(analogPin) > 0){         // wait until capacitor is completely discharged
  }
  
  pinMode(dischargePin, INPUT);            // set discharge pin back to input

}

got it working!! A friend of mine pointed out that i was using pin 8 as an input for a button and for the lcd screen. So once i switched the button to pin 4 it works perfectly!! :slight_smile: Thanks for taking a look and trying to help!