This noobie needs help I just wanna know how it works

So i got to explore the world of arduino all because of this new subject of our school’s curriculum. Seeing people talk about their projects here and help each other out, hehe, I decided to ask some help from here :sweat_smile:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

#include <DHT.h>
const int DHTPIN = 8;
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int averageHumidity = 50;
const float humidityFactor = 2.0;

int sensor1 = A0;
int sensor2 = A1;
int sensor3 = A2;
int sensor4 = A3;

const int delayTime = 500;

const int WP_pin1 = 12;
const int WP_pin2 = 11;
const int WP_pin3 = 10;
const int WP_pin4 = 9;

const int baselineThresh = 600;

const int trigPin = 5;
const int echoPin = 6;

const int buzzerPin = 7;

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);

  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);

  dht.begin();

  pinMode(WP_pin1, OUTPUT);
  pinMode(WP_pin2, OUTPUT);
  pinMode(WP_pin3, OUTPUT);
  pinMode(WP_pin4, OUTPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  digitalWrite(WP_pin1, HIGH);
  digitalWrite(WP_pin2, HIGH);
  digitalWrite(WP_pin3, HIGH);
  digitalWrite(WP_pin4, HIGH);

  digitalWrite(buzzerPin, LOW);

  delay(delayTime);
}

void loop() {

    if (Serial.available()) {
  // wait a bit for the entire message to arrive
  delay(100);
  // clear the screen
  lcd.clear();
  // read all the available characters
  while (Serial.available() > 0) {
    // display each character to the LCD
    lcd.write(Serial.read());
  }
  float h = dht.readHumidity();
  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float adjustedThresh = baselineThresh + (humidityFactor * (averageHumidity - h));

  Serial.print("Current Humidity: ");
  Serial.print(h);
  Serial.print("%\t");
  Serial.print("Threshold: ");
  Serial.println(adjustedThresh);

  Serial.print("1stPlant");
  sensor1 = analogRead(A0);
  Serial.println(sensor1);

  if (sensor1 > adjustedThresh) {
    digitalWrite(WP_pin1, LOW);
  } else {
    digitalWrite(WP_pin1, HIGH);
  }

  Serial.print("2ndPlant");
  sensor2 = analogRead(A1);
  Serial.println(sensor2);

  if (sensor2 > adjustedThresh) {
    digitalWrite(WP_pin2, LOW);
  } else {
    digitalWrite(WP_pin2, HIGH);
  }

  Serial.print("3rdPlant");
  sensor3 = analogRead(A2);
  Serial.println(sensor3);

  if (sensor3 > adjustedThresh) {
    digitalWrite(WP_pin3, LOW);
  } else {
    digitalWrite(WP_pin3, HIGH);
  }

  Serial.print("4thPlant");
  sensor4 = analogRead(A3);
  Serial.println(sensor4);

  if (sensor4 > adjustedThresh) {
    digitalWrite(WP_pin4, LOW);
  } else {
    digitalWrite(WP_pin4, HIGH);
  }

  delay(delayTime);

  long duration, inches, cm;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  if (distance > 50) {
    digitalWrite(buzzerPin, HIGH);
    Serial.println("Insufficient Water Storage");
  } else {
    digitalWrite(buzzerPin, LOW);
  }
  delay(delayTime);
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}

I pasted the code for our project (I didn’t expect it to be this long) and the story is: it worked well. It was verified and uploaded and functioned.. until I added the code for the lcd screen. I got the lcd’s code from one of the examples given in the library we used, which was supposed to show the things that pop on the serial monitor.

What i found weird is that after pressing “verify”, the lines in red highlight were

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
long microsecondsToInches(long microseconds) {

and

long microsecondsToCentimeters(long microseconds) {

I don’’t know how that’s possible, and perhaps you experienced guys know. THanks for letting me share:)

Missing a closing ‘}’ before


long microsecondsToInches(long microseconds) {

Compilers are rather dumb, when it comes to analyzing syntax errors: where it comes across some "thing" (character, word, etc) that is unexpected. It knows that something is wrong, but it can do a spectacularly bad job of figuring out WHERE the actual error is.
In this case, it sees a closing brace, which indicates the 'end' of a code block of some kind. But it doesn't think such a block had ever started, and it can't do a good job of guessing - was it a couple lines ago, or way at the beginning of the sketch?
So you get an error message that points at a mysterious place.

This is why a regular formatting scheme, and the auto-format command, are so useful. While the compiler doesn't understand white-space width, it can be really obvious to a human looking at it when things "don't line up properly."

(It's also why, when you have "many" compiler errors, it's important to look at the first-occurring errors first. A relatively minor ("non-fatal") early error can lead to a cascade of subsequent errors, before the compiler finally gives up.)

What is the question? If it's a compile error click the 'COPY ERROR MESSAGES' button lower right and paste them in a block.

you should have used the <adafruit_crystal.h> for it

you put digitalwrite so are you trying to control cd or led

correcting my self lcd or led what are you trying to control

km_arduino,

You might not get a reply from spilled_tea.
The last time that they were here was back in September 2025.

(Note however that the last time I made a similar claim the OP appeared 10 minutes later.)

og you are using lcd with soil moisture sensor to show how moist it the soil but use the <adafuit_crystal.h> library @spilled_tea

@JohnLincoln

he's using lcd I think. And yeah, you should adafruit_crystal.h for it.

This is not the file name, even for Adafruit... regardless, LiquidCrystal_I2C.h works just fine.

Not necessary. The original library works just fine.

digitalWrite() is used to put a logic level on a pin. Not exclusively for an LCD or LED. For this sketch, digitalWrite() is being used to turn the water pumps on and off. The LCD in this project is insignificant. It takes the Serial Input data and places the data on the LCD.

A watering system. Referring to one example, digitalWrite(WP_pin1, HIGH);... the sketch seems to be checking for humidity (DHT) and soil dampness (soil probes... high water means low resistance) of four "plants" and watering the plants if the soil is dry. Since "WP" is set "HIGH" at the start of the sketch, I would assume that to be the logic level to urn the Water Pumps OFF... so HIGH means OFF.

You can see in this simulation that "HIGH" levels of a potentiometer turns the LED (Water Pump) OFF. "LOW" potentiometer turns WP ON.

You can also see the "distance" measurements are checking the water tank level... if that "rises" above 50 (more than 50cm between HCSR04 and water), the system will warn of low water level.

i mean <adafruit_liquidcrystal.h>

No, that library does not exist.

Maybe you are referring to Adafruit_LiquidCrystal.h, which is totally different than what you have written...

Still, the original library in the original code works, so there is no need for Adafruit_LiquidCrystal.h

You said the exact same thing

Right, I had to correct you again, because you keep trying to use inaccurate information.