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 ![]()
#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:)

