#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include "DHT.h"
//keypad 4x4
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
byte rowPins[ROWS] = {18, 5, 4, 2}; //กำหนดขา Pin ปุ่มแนวแนว
byte colPins[COLS] = {33, 25, 26, 27}; //กำหนดขา Pin ปุ่มแนวนอน
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char inputKey; //กำหนดตัวแปรสำหรับรับค่าจาก Keypad
//DHT22
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//LCD 20x4 I2C
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//จำลองเซนเซอร์ความชื้นในดิน
const int AnalogSensorPin = 32; //กำหนดขา input เซนเซอร์
const int RelaySoil = 15; //กำหนดขา input รีเลย์
int sensorvalue = 0;
int outputvalue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Init LCD 20x4 I2C
lcd.init();
lcd.backlight();
//DHT22
dht.begin();
pinMode(RelaySoil, OUTPUT); //กำหนดขา output รีเลย์
}
void loop() {
// put your main code here, to run repeatedly:
//รับค่าจาก Keypad ขึ้นแสดงบนจอ LCD
if ((inputKey = keypad.getKey()) != NO_KEY) {
Serial.println(inputKey);
}
/*
//เช็ค Error ของ DHT22
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//แสดงอุณหภูมิ จากเซนเซอร์ DHT22
lcd.setCursor(0, 0);
lcd.print("T :");
lcd.setCursor(3, 0);
lcd.print(t, 2);
lcd.setCursor(8, 0);
lcd.print("C");
//แสดงความชื้นสัมพันธ์ จากเซนเซอร์ DHT22
lcd.setCursor(11, 0);
lcd.print("RH:");
lcd.setCursor(14, 0);
lcd.print(h, 2);
lcd.setCursor(19, 0);
lcd.print("%");
//จำลองแสดงค่าความชื้นในดิน จากเซนเซอร์จำรอง LDR
sensorvalue = analogRead(AnalogSensorPin);
outputvalue = map(sensorvalue, 0, 4094, 100, 0);
lcd.setCursor(0, 1);
lcd.print("SH:");
lcd.setCursor(3, 1);
lcd.print(outputvalue);
lcd.setCursor(6, 1);
lcd.print("%");
if (outputvalue <= 80) { //การตั้งค่า % การรดน้ำต้นไม้
digitalWrite(RelaySoil, HIGH); //เมื่อความชื้นน้อยกว่า 80% สั่งให้ปั๊มทำงาน
}
else {
digitalWrite(RelaySoil, LOW); //เมื่อความชื้นมากกว่า 80% สั่งให้ปั๊มหยุดทำงาน
}
//หน่วงเวลา
delay(1000);
//ล้างจอ LCD
lcd.clear();
*/
}