Go Down

Topic: Can't get lcd.print to work (Read 586 times) previous topic - next topic

DavidFMarks

New to all this and trying to write a sketch to measure distance with an RTF 04 and display it on the PC and an LCD. The code below produces the error message "LCD was not declared in this scope" on either of the two lines which set the cursor and write the variable distance. If I comment thes out, everything else works. Tried this the other day and it worked. I cannot for the life of me find what I have done which is different (probably staring me in the face). The code is as follows:
Code: [Select]

#define ECHOPIN 8                            // Pin to receive echo pulse
#define TRIGPIN 9                            // Pin to send trigger pulse
#include <LiquidCrystal.h>
void setup(){
  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.print("Distance = ");
}

void loop(){
 
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // Calculate distance from time of pulse
  Serial.println(distance);                     
  delay(50);
  lcd.cursor(0,12);     //Move cursor to one space after equals sign
  lcd.print(distance);  // Wait 50mS before next ranging
}


wildbill

Move the declaration of the lcd object out of the setup routine (above it). You've made it local, so the attempts to use it in loop fail - as the compiler is (cryptically) trying to tell you.

DavidFMarks

Excellent ! Thanks very much.. Hadn't understood the local issue. Gradually getting there ! Many thanks for your help.

UKHeliBob


Excellent ! Thanks very much.. Hadn't understood the local issue. Gradually getting there ! Many thanks for your help.


Shouldn't lcd.cursor be lcd.setCursor ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up