#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define RE 8
#define DE 7
// Define pin numbers
#define DHT_PIN 2 // Pin to which DHT22 sensor is connected
#define BUZZER_PIN 7 // Pin to which the buzzer is connected
// Define the type of DHT sensor
#define DHT_TYPE DHT22 // Change to DHT11 if using DHT11 sensor
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
// byte values[11];
SoftwareSerial mod(2, 3);
DHT dht(DHT_PIN, DHT_TYPE);
// Initialize the LCD (pins connected to the LCD: RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
// LiquidCrystal lcd2(13, 12, 11, 10, 9, 8);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int red_led = 11;
int green_led = 10;
int blue_led = 9;
void setup() {
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Menghitung ...");
display.display();
delay(3000);
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
// Initialize the DHT sensor
dht.begin();
// Set up the buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
// Set up the pins for the moisture sensor LEDs
pinMode(5, OUTPUT); // High moisture LED
pinMode(6, OUTPUT); // Medium moisture LED
pinMode(3, OUTPUT); // Low moisture LED
// Set up the pin for the moisture sensor
pinMode(A0, INPUT);
}
void loop() {
byte val1, val2, val3;
val1 = random(0, 350); // Generate random value for nitrogen (0-255)
delay(250);
val2 = random(0, 350); // Generate random value for phosphorous (0-255)
delay(250);
val3 = random(0, 330); // Generate random value for potassium (0-255)
delay(250);
Serial.println();
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.println();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 5);
display.print("N: ");
display.print(val1);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 25);
display.print("P: ");
display.print(val2);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 45);
display.print("K: ");
display.print(val3);
display.setTextSize(1);
display.print(" mg/kg");
display.display();
// Read the moisture sensor value
int moistureValue = analogRead(A0);
if(moistureValue<=100){
pinMode(red_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(blue_led, OUTPUT);
}
// Determine moisture level and set LEDs accordingly
if (moistureValue >= 100 && moistureValue <= 499) {
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(3, LOW);
Serial.println("Medium amount of Moisture");
delay(1000);
} else if (moistureValue >= 500 && moistureValue <= 1023) {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(3, LOW);
Serial.println("High Moisture");
delay(1000);
} else {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(3, HIGH);
Serial.println("Low Moisture");
delay(500);
led_color(255,0,255);
delay(500);
led_color(255,255,0);
delay(500);
// Delay for stability
delay(1000);
}
// Read temperature and humidity from the DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature on the LCD
lcd.setCursor(6, 0);
lcd.print(temperature, 1);
lcd.print("C");
// Display humidity on the LCD
lcd.setCursor(10, 1);
lcd.print(humidity, 1);
lcd.print("%");
// Check if temperature is above a threshold value and activate the buzzer
if (temperature > 30.0 || temperature<0.0) {
digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 10000); // Generate a tone of 1000 Hz
} else {
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN); // Stop the tone
}
// Delay before next loop iteration
// delay(1000);
}
void led_color(int red_value, int green_value,int blue_value)
{
analogWrite(red_led,red_value);
analogWrite(green_led,green_value);
analogWrite(blue_led,blue_value);
}
Loading
ssd1306
ssd1306