my smartfarming arduino code can't run due to an unknown error. i used several sensors in my project this time, including dht22, soil moisture, photosensitive sensor for lcd i2c output. please someone help me.
#include <DHT.h> // DHT library for DHT22 sensor
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 sensor model
// Define analog pins for soil moisture and photosensitive sensor
const int soilMoisturePin = A0;
const int photoSensorPin = A1;
// Initialize LCD (replace with your specific LCD library and pin connections)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Example for I2C LCD with specific pins
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize serial communication (optional for debugging)
Serial.begin(9600);
// Set LCD display and clear screen
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
// Read temperature and humidity from DHT sensor
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to avoid NaN values)
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read analog values from soil moisture and photosensitive sensors
int soilMoisture = analogRead(soilMoisturePin);
int photoSensorValue = analogRead(photoSensorPin);
// Convert sensor readings to meaningful values (adjust based on sensor specifications)
float moisturePercentage = map(soilMoisture, 0, 1023, 0, 100); // Map analog value to moisture percentage
float lightIntensity = map(photoSensorValue, 0, 1023, 0, 100); // Map analog value to light intensity percentage
// Display readings on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("°C Humidity: ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Soil Moisture: ");
lcd.print(moisturePercentage);
lcd.print("% Light: ");
lcd.print(lightIntensity);
lcd.print("%");
// Optional: Print sensor readings to serial monitor for debugging
Serial.print("Temp: ");
Serial.print(temp);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.print("%, Soil Moisture: ");
Serial.print(moisturePercentage);
Serial.print("%, Light: ");
Serial.println(lightIntensity);
// Delay between readings (adjust as needed)
delay(2000);
}