In the circuit that I made there are sensors, LCDs, keypads, Arduino, and 3-pin on/off switches. I want when the switch is on, then the circuit program that is run with the keypad can be run. However, in this program I find it difficult to make this happen. In this program, the off/on switch only affects the LCD. The keypad cannot be run in this program. Even though the keypad is still working properly. Please help me.
ketik atau #include <Wire.h> // Memanggil library wire.h
#include <LiquidCrystal_I2C.h> // Memanggil library LCD dengan I2C
#include <Keypad.h> // Memanggil library keypad.h
// Pin dan objek LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin sensor dan LED
#define Sensor A0 // Deklarasi pin sensor pada pin A0
#define ledM A1 // Deklarasi LED Merah pada pin A1
#define ledH A2 // Deklarasi LED Hijau pada pin A2
#define switchPin 12 // Pin switch
// Konfigurasi keypad
const byte ROWS = 4; // 4 baris
const byte COLS = 3; // 3 kolom
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Nilai batas sensor
const int dry = 507; // Nilai batas kering sensor
const int wet = 200; // Nilai batas basah sensor
void setup() {
Serial.begin(9600);
/*lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Multi-commodity");
lcd.setCursor(0, 1);
lcd.print("Moisture Meter");*/
pinMode(ledM, OUTPUT);
pinMode(ledH, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}
void loop() {
int sensor = analogRead(A0);
float kadarBeras = 0; // Inisialisasi kadar beras
float kadarJagung = 0; // Inisialisasi kadar jagung
char key = keypad.getKey();
// Program switch dengan switchPin pada pin 12
int switchStatus = digitalRead(switchPin);
if (switchStatus == LOW) {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Multi-commodity");
lcd.setCursor(0, 1);
lcd.print("Moisture Meter");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.noBacklight();
}
// Program keypad yang diberikan
if (key == '*') {
digitalWrite(ledM, LOW);
digitalWrite(ledH, LOW);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("POUR");
lcd.setCursor(1, 1);
lcd.print("- - - - - - -");
delay(3000);
lcd.setCursor(5, 0);
lcd.print("SELECT");
lcd.setCursor(0, 1);
lcd.print("[1]Rice [2]Corn ");
lcd.print(key);
}
if (key == '1') {
kadarBeras = (-0.1433 * sensor) + 81.497; // Perhitungan kadar beras
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("Rice");
lcd.setCursor(3, 1);
lcd.print("Measure...");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture Content:");
lcd.setCursor(0, 1);
lcd.print(kadarBeras);
lcd.print("%");
delay(1000);
}
if (key == '2') {
kadarJagung = (-0.1483 * sensor) + 83.705; // Perhitungan kadar jagung
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("Corn");
lcd.setCursor(3, 1);
lcd.print("Measure...");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture Content:");
lcd.setCursor(0, 1);
lcd.print(kadarJagung);
lcd.print("%");
delay(1000);
}
if (key == '#') {
lcd.clear();
if (kadarJagung <= 15) {
lcd.setCursor(0, 0);
lcd.print("Good Quality");
digitalWrite(ledM, LOW);
digitalWrite(ledH, HIGH);
delay(500);
} else if (kadarJagung > 15) {
lcd.setCursor(0, 0);
lcd.print("Low Quality");
digitalWrite(ledM, HIGH);
digitalWrite(ledH, LOW);
delay(500);
}
}
}