Using a 3-pin switch button to drain and disconnect current in the circuit

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);
    }
  }
}

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest and analyze the test results.

Have a nice day and enjoy coding in C++.

This part of your code reads the switch in the digitalRead satatement and then acts on it in the if / else section. If the switch reads LOW then it runs everything from the { to the } in the if part. If it doesn't (which means it's HIGH) then it runs from the { to the } the else part.

The part about the keypad is outside of the { and } so it runs all the time. If you only want it running in one section then move all of the keypad code to that section.

Thanks, Sir. I already try your advice with this code, but the "multi-commodity moisture meter" is still show on the LCD. So, when I click the keypad it doesn't work. Any other suggestions?

#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");
    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);
    }
  }
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.noBacklight();
  }
}

Thanks for sharing. Can you help me to improve my program by what you mean?

just use the switch to affect reading the keypad

void loop() {
    int sensor = analogRead(A0);
    float kadarBeras = 0; // Inisialisasi kadar beras
    float kadarJagung = 0; // Inisialisasi kadar jagung
    char key = NO_KEY;

    if (LOW == digitalRead(switchPin))
        key = keypad.getKey();

    lcd.init();
    lcd.backlight();

Just add another if statement to your original code

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();
  }

 // ****** Add another if 
  if (switchStatus == LOW) {
  // 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);
    }
  }
 }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.