Compilation error: 'lcdRS' was not declared in this scope?

What's wrong with my code? i'm trying to use the LCD library but it keeps giving me problems


const int trigPin = 9;   // Ultralydsensor trigpin
const int echoPin = 10;  // Ultralydsensor echopin
const int ledPin = 13;   // LED-pin
const int relayPin = 8;  // Rele-pin

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
  Serial.begin(9600);  // Initialiser seriell kommunikasjon

  pinMode(trigPin, OUTPUT);  // Sett trigPin som output
  pinMode(echoPin, INPUT);   // Sett echoPin som input
  pinMode(ledPin, OUTPUT);   // Sett ledPin som output
  pinMode(relayPin, OUTPUT); // Sett relayPin som output

  // Initialiser LCD-skjerm
  pinMode(lcdRS, OUTPUT);
  pinMode(lcdE, OUTPUT);
  pinMode(lcdD4, OUTPUT);
  pinMode(lcdD5, OUTPUT);
  pinMode(lcdD6, OUTPUT);
  pinMode(lcdD7, OUTPUT);

  lcd.begin(16, 2);
  
}

void loop() {
  // Send en ultralydimpuls
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Mål avstanden til objektet
  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;  // Beregn avstanden i cm

  // Vis avstanden på seriell monitor
  Serial.print("Avstand: ");
  Serial.print(distance);
  Serial.println(" cm");

   // Skriv "HOLD AVSTAND" og avstanden på LCD-skjermen
  lcdWrite("HOLD AVSTAND");
  lcdSetCursor(0, 1);
  lcdPrint(distance);
  lcdPrint(" cm");
  

  // Hvis avstanden er 0,5 meter eller mindre, slå av releen og blink med LEDen
  if (distance <= 50) {
    digitalWrite(relayPin, LOW);
    digitalWrite(ledPin, HIGH);
    delay(250);
    digitalWrite(ledPin, LOW);
    delay(250);
  } 

Welcome to the forum

The compiler isn't lying. lcsRS is not declared and nor are several other variables

Where did you get the code ?

I'm writing it myself, i'm new to arduino. how should i procceed?

declare lcdRS as a valid variable.

or use the declared rs const int rs = 12 or change const int rs = 12 to const int lcdRS = 12.

most likely you'll need to declare

pinMode(lcdRS, OUTPUT);
  pinMode(lcdE, OUTPUT);
  pinMode(lcdD4, OUTPUT);
  pinMode(lcdD5, OUTPUT);

and others.

Setting the pinModes for the LCD pins is unnecessary. The library takes care of setting up the pins. See the begin() function definition in the LiquidCrystal.cpp file.

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