i want you complete this code, the getKeypadValue(int minVal, int maxVal) function in…
- User
i want you complete this code, the getKeypadValue(int minVal, int maxVal) function in order to add after minVal and maxVal in input a text that have to be shown on the lcd screen in order to ask the kind of information (timer, Min_PH, heure_ph, relay number...) : #include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DS3232RTC.h> #include <Streaming.h>
// Pin configuration and libraries initiation const byte ROWS = 4; const byte COLS = 4; // Définition des broches du clavier matriciel byte rowPins[ROWS] = { 33, 32, 31, 30 }; byte colPins[COLS] = { 29, 28, 27, 26 }; // Définition des touches du clavier matriciel 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); LiquidCrystal_I2C lcd(0x27, 20, 4); DS3232RTC rtc;
const int Relai_depart = 34; const int NUM_RELAYS = 8;a int RELAY_PINS[NUM_RELAYS]; bool relayStates[NUM_RELAYS]; int timer[NUM_RELAYS]; int forcing[NUM_RELAYS]; float tension_relai[NUM_RELAYS]; float intensite_relai[NUM_RELAYS]; float puissance_relai[NUM_RELAYS];
//Paramètre programme horaire (PH) const int Quantite_PH = 5; int Statut_PH[Quantite_PH] = {0}; int Heure_PH[Quantite_PH] = {0}; int Min_PH[Quantite_PH] = {0}; int Relai_PH[Quantite_PH] = {0}; int Duree_PH[Quantite_PH] = {0}; int Repetition_PH[Quantite_PH] = {0};
// Variables int relayPins[12] = {34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45}; int buttonPin = 18; float temp1, temp2, temp3, temp4; float hum1, hum2, hum3, hum4;
void setup() { Wire.begin(); rtc.begin();
for (int i = 0; i < 12; i++) { pinMode(relayPins[i], OUTPUT); digitalWrite(relayPins[i], LOW); }
pinMode(buttonPin, INPUT_PULLUP); lcd.init(); lcd.backlight(); }
void loop() { if (digitalRead(buttonPin) == LOW) { mainMenu(); } }
void mainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Relays B-Info"); lcd.setCursor(0, 1); lcd.print("D-Quit");
char key = keypad.waitForKey(); switch (key) { case 'A': relaysMenu(); break; case 'B': infoMenu(); break; case 'D': lcd.clear(); lcd.noBacklight(); break; } }
void relaysMenu() { int relay = 0; lcd.clear(); lcd.setCursor(0, 0); lcd.print("Enter relay #");
// Collect relay number (1-12) do { char key = keypad.waitForKey(); if (key >= '1' && key <= '9' && relay * 10 + (key - '0') <= 12) { relay = relay * 10 + (key - '0'); lcd.print(key); } } while (relay == 0);
relay = relay - 1; // Adjust the relay index (0-11)
lcd.setCursor(0, 1); lcd.print("A:ON/OFF B:TIMER "); lcd.setCursor(0, 2); lcd.print("C:Program D:Exit ");
bool exit = false; while (!exit) { char key = keypad.waitForKey(); int value;
switch (key) { case 'A': relayStates[relay] = !relayStates[relay]; break; case 'B': value = getKeypadValue(0, 999); if (value > -1) { relayStates[relay] = 1; timer[relay] = value; } break; case 'C': value = getKeypadValue(0, 24); if (value > -1) { Heure_PH[relay] = value; value = getKeypadValue(0, 60); if (value > -1) { Min_PH[relay] = value; timer[relay] = getKeypadValue(1, 999); } } break; case 'D': exit = true; lcd.clear(); lcd.noBacklight(); break; }} }
int getKeypadValue(int minVal, int maxVal) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Enter value ("); lcd.print(minVal); lcd.print("-"); lcd.print(maxVal); lcd.print("):");
int value = 0; bool exit = false; while (!exit) { char key = keypad.waitForKey();
if (key >= '0' && key <= '9') { int newVal = value * 10 + (key - '0'); if (newVal >= minVal && newVal <= maxVal) { value = newVal; lcd.print(key); } } else if (key == '*') { return -1; // Cancel entry } else if (key == '#') { exit = true; }}
return value; }
void infoMenu() { // Handle displaying and interacting with the info menu lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Temp B-Hum"); lcd.setCursor(0, 1); lcd.print("D-Quit");
bool quit = false; while (!quit) { char key = keypad.waitForKey(); switch (key) { case 'A': showTemps(); break; case 'B': showHumidity(); break; case 'D': quit = true; break; } } }
void showTemps() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("T1:"); lcd.print(temp1, 1); lcd.setCursor(0, 1); lcd.print("T2:"); lcd.print(temp2, 1); lcd.setCursor(0, 2); lcd.print("T3:"); lcd.print(temp3, 1); lcd.setCursor(0, 3); lcd.print("T4:"); lcd.print(temp4, 1);
keypad.waitForKey(); }
void showHumidity() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("H1:"); lcd.print(hum1, 1); lcd.setCursor(0, 1); lcd.print("H2:"); lcd.print(hum2, 1); lcd.setCursor(0, 2); lcd.print("H3:"); lcd.print(hum3, 1); lcd.setCursor(0, 3); lcd.print("H4:"); lcd.print(hum4, 1);
keypad.waitForKey(); }
- Assistant
Here's the modified code with the
getKeypadValue()function modified to show custom information type during input:#include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DS3232RTC.h> #include <Streaming.h> // Pin configuration and libraries initiation const byte ROWS = 4; const byte COLS = 4; // Définition des broches du clavier matriciel byte rowPins[ROWS] = { 33, 32, 31, 30 }; byte colPins[COLS] = { 29, 28, 27, 26 }; // Définition des touches du clavier matriciel 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); LiquidCrystal_I2C lcd(0x27, 20, 4); DS3232RTC rtc; const int Relai_depart = 34; const int NUM_RELAYS = 8; int RELAY_PINS[NUM_RELAYS]; bool relayStates[NUM_RELAYS]; int timer[NUM_RELAYS]; int forcing[NUM_RELAYS]; float tension_relai[NUM_RELAYS]; float intensite_relai[NUM_RELAYS]; float puissance_relai[NUM_RELAYS]; // Paramètre programme horaire (PH) const int Quantite_PH = 5; int Statut_PH[Quantite_PH] = {0}; int Heure_PH[Quantite_PH] = {0}; int Min_PH[Quantite_PH] = {0}; int Relai_PH[Quantite_PH] = {0}; int Duree_PH[Quantite_PH] = {0}; int Repetition_PH[Quantite_PH] = {0}; // Variables int relayPins[12] = { 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 }; int buttonPin = 18; float temp1, temp2, temp3, temp4; float hum1, hum2, hum3, hum4; void setup() { Wire.begin(); rtc.begin(); for (int i = 0; i < 12; i++) { pinMode(relayPins[i], OUTPUT); digitalWrite(relayPins[i], LOW); } pinMode(buttonPin, INPUT_PULLUP); lcd.init(); lcd.backlight(); } void loop() { if (digitalRead(buttonPin) == LOW) { mainMenu(); } } void mainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Relays B-Info"); lcd.setCursor(0, 1); lcd.print("D-Quit"); char key = keypad.waitForKey(); switch (key) { case 'A': relaysMenu(); break; case 'B': infoMenu(); break; case 'D': lcd.clear(); lcd.noBacklight(); break; } } void relaysMenu() { int relay = 0; lcd.clear(); lcd.setCursor(0, 0); lcd.print("Enter relay #"); // Collect relay number (1-12) do { char key = keypad.waitForKey(); if (key >= '1' && key <= '9' && relay * 10 + (key - '0') <= 12) { relay = relay * 10 + (key - '0'); lcd.print(key); } } while (relay == 0); relay = relay - 1; // Adjust the relay index (0-11) lcd.setCursor(0, 1); lcd.print("A:ON/OFF B:TIMER "); lcd.setCursor(0, 2); lcd.print("C:Program D:Exit "); bool exit = false; while (!exit) { char key = keypad.waitForKey(); int value; switch (key) { case 'A': relayStates[relay] = !relayStates[relay]; break; case 'B': value = getKeypadValue(0, 999, "Enter Timer:"); if (value > -1) { relayStates[relay] = 1; timer[relay] = value; } break; case 'C': value = getKeypadValue(0, 24, "Enter Heure PH:"); if (value > -1) { Heure_PH[relay] = value; value = getKeypadValue(0, 60, "Enter Min PH:"); if (value > -1) { Min_PH[relay] = value; timer[relay] = getKeypadValue(1, 999, "Enter Duration:"); } } break; case 'D': exit = true; lcd.clear(); lcd.noBacklight(); break; } } } int getKeypadValue(int minVal, int maxVal, String infoText) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(infoText); lcd.setCursor(0, 1); lcd.print("Enter value ("); lcd.print(minVal); lcd.print("-"); lcd.print(maxVal); lcd.print("):"); int value = 0; bool exit = false; while (!exit) { char key = keypad.waitForKey(); if (key >= '0' && key <= '9') { int newVal = value * 10 + (key - '0'); if (newVal >= minVal && newVal <= maxVal) { value = newVal; lcd.print(key); } } else if (key == '*') { return -1; // Cancel entry } else if (key == '#') { exit = true; } } return value; } void infoMenu() { // Handle displaying and interacting with the info menu lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Temp B-Hum"); lcd.setCursor(0, 1); lcd.print("D-Quit"); bool quit = false; while (!quit) { char key = keypad.waitForKey(); switch (key) { case 'A': showTemps(); break; case 'B': showHumidity(); break; case 'D': quit = true; break; } } } void showTemps() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("T1:"); lcd.print(temp1, 1); lcd.setCursor(0, 1); lcd.print("T2:"); lcd.print(temp2, 1); lcd.setCursor(0, 2); lcd.print("T3:"); lcd.print(temp3, 1); lcd.setCursor(0, 3); lcd.print("T4:"); lcd.print(temp4, 1); keypad.waitForKey(); } void showHumidity() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("H1:"); lcd.print(hum1, 1); lcd.setCursor(0, 1); lcd.print("H2:"); lcd.print(hum2, 1); lcd.setCursor(0, 2); lcd.print("H3:"); lcd.print(hum3, 1); lcd.setCursor(0, 3); lcd.print("H4:"); lcd.print(hum4, 1); keypad.waitForKey(); }I've added a new
String infoTextparameter to thegetKeypadValue()function to take the custom text to display on the LCD screen as input. I've also updated the instances wheregetKeypadValue()is called to include the custom text you wanted to display (timer, Min_PH, heure_ph, etc.) - User
Complete this code by adding the D key to quit the menu in showTemps(), showHumidity(), Also, use the function getKeypadValue to collect the relay number in relaysMenu()
#include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DS3232RTC.h> #include <Streaming.h>
// Pin configuration and libraries initiation const byte ROWS = 4; const byte COLS = 4; // Définition des broches du clavier matriciel byte rowPins[ROWS] = { 33, 32, 31, 30 }; byte colPins[COLS] = { 29, 28, 27, 26 }; // Définition des touches du clavier matriciel 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); LiquidCrystal_I2C lcd(0x27, 20, 4); DS3232RTC rtc;
const int Relai_depart = 34; const int NUM_RELAYS = 8; int RELAY_PINS[NUM_RELAYS]; bool relayStates[NUM_RELAYS]; int timer[NUM_RELAYS]; int forcing[NUM_RELAYS]; float tension_relai[NUM_RELAYS]; float intensite_relai[NUM_RELAYS]; float puissance_relai[NUM_RELAYS];
// Paramètre programme horaire (PH) const int Quantite_PH = 5; int Statut_PH[Quantite_PH] = {0}; int Heure_PH[Quantite_PH] = {0}; int Min_PH[Quantite_PH] = {0}; int Relai_PH[Quantite_PH] = {0}; int Duree_PH[Quantite_PH] = {0}; int Repetition_PH[Quantite_PH] = {0};
// Variables int relayPins[12] = { 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 }; int buttonPin = 18; float temp1, temp2, temp3, temp4; float hum1, hum2, hum3, hum4;
void setup() { Wire.begin(); rtc.begin();
for (int i = 0; i < 12; i++) { pinMode(relayPins[i], OUTPUT); digitalWrite(relayPins[i], LOW); }
pinMode(buttonPin, INPUT_PULLUP); lcd.init(); lcd.backlight(); }
void loop() { if (digitalRead(buttonPin) == LOW) { mainMenu(); } }
void mainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Relays B-Info"); lcd.setCursor(0, 1); lcd.print("D-Quit");
char key = keypad.waitForKey(); switch (key) { case 'A': relaysMenu(); break; case 'B': infoMenu(); break; case 'D': lcd.clear(); lcd.noBacklight(); break; } }
void relaysMenu() { int relay = 0; lcd.clear(); lcd.setCursor(0, 0); lcd.print("Enter relay #");
// Collect relay number (1-12) do { char key = keypad.waitForKey(); if (key >= '1' && key <= '9' && relay * 10 + (key - '0') <= 12) { relay = relay * 10 + (key - '0'); lcd.print(key); } } while (relay == 0);
relay = relay - 1; // Adjust the relay index (0-11)
lcd.setCursor(0, 1); lcd.print("A:ON/OFF B:TIMER "); lcd.setCursor(0, 2); lcd.print("C:Program D:Exit ");
bool exit = false; while (!exit) { char key = keypad.waitForKey(); int value;
switch (key) { case 'A': relayStates[relay] = !relayStates[relay]; break;
case 'B': value = getKeypadValue(0, 999, "Enter Timer:"); if (value > -1) { relayStates[relay] = 1; timer[relay] = value; } break; case 'C': value = getKeypadValue(0, 24, "Enter Heure PH:"); if (value > -1) { Heure_PH[relay] = value; value = getKeypadValue(0, 60, "Enter Min PH:"); if (value > -1) { Min_PH[relay] = value; timer[relay] = getKeypadValue(1, 999, "Enter Duration:"); } } break; case 'D': exit = true; lcd.clear(); lcd.noBacklight(); break;} } }
int getKeypadValue(int minVal, int maxVal, char* infoText) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(infoText); lcd.setCursor(0, 1); lcd.print("Enter value ("); lcd.print(minVal); lcd.print("-"); lcd.print(maxVal); lcd.print("):"); lcd.setCursor(0, 2); lcd.print("A-VALIDER D-QUITTER");
int value = 0; bool exit = false; while (!exit) { char key = keypad.waitForKey();
if (key >= '0' && key <= '9') { int newVal = value * 10 + (key - '0'); if (newVal >= minVal && newVal <= maxVal) { value = newVal; lcd.print(key); } } else if (key == 'D') { return -1; // Cancel entry } else if (key == 'A') { exit = true; }}
return value; }
void infoMenu() { // Handle displaying and interacting with the info menu lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Temp B-Hum"); lcd.setCursor(0, 1); lcd.print("D-Quit");
bool quit = false; while (!quit) { char key = keypad.waitForKey(); switch (key) { case 'A': showTemps(); break; case 'B': showHumidity(); break; case 'D': quit = true; break; } } }
void showTemps() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("T1:"); lcd.print(temp1, 1); lcd.setCursor(0, 1); lcd.print("T2:"); lcd.print(temp2, 1); lcd.setCursor(0, 2); lcd.print("T3:"); lcd.print(temp3, 1); lcd.setCursor(0, 3); lcd.print("T4:"); lcd.print(temp4, 1);
keypad.waitForKey(); }
void showHumidity() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("H1:"); lcd.print(hum1, 1); lcd.setCursor(0, 1); lcd.print("H2:"); lcd.print(hum2, 1); lcd.setCursor(0, 2); lcd.print("H3:"); lcd.print(hum3, 1); lcd.setCursor(0, 3); lcd.print("H4:"); lcd.print(hum4, 1);
keypad.waitForKey(); }
- Assistant
Here's the updated version of the code with the 'D' key to quit the menu in
showTemps()andshowHumidity()functions, and thegetKeypadValue()function used to collect the relay number inrelaysMenu().#include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DS3232RTC.h> #include <Streaming.h> // Pin configuration and libraries initiation const byte ROWS = 4; const byte COLS = 4; // Définition des broches du clavier matriciel byte rowPins[ROWS] = { 33, 32, 31, 30 }; byte colPins[COLS] = { 29, 28, 27, 26 }; // Définition des touches du clavier matriciel 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); LiquidCrystal_I2C lcd(0x27, 20, 4); DS3232RTC rtc; const int Relai_depart = 34; const int NUM_RELAYS = 8; int RELAY_PINS[NUM_RELAYS]; bool relayStates[NUM_RELAYS]; int timer[NUM_RELAYS]; int forcing[NUM_RELAYS]; float tension_relai[NUM_RELAYS]; float intensite_relai[NUM_RELAYS]; float puissance_relai[NUM_RELAYS]; // Paramètre programme horaire (PH) const int Quantite_PH = 5; int Statut_PH[Quantite_PH] = {0}; int Heure_PH[Quantite_PH] = {0}; int Min_PH[Quantite_PH] = {0}; int Relai_PH[Quantite_PH] = {0}; int Duree_PH[Quantite_PH] = {0}; int Repetition_PH[Quantite_PH] = {0}; // Variables int relayPins[12] = { 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 }; int buttonPin = 18; float temp1, temp2, temp3, temp4; float hum1, hum2, hum3, hum4; void setup() { Wire.begin(); rtc.begin(); for (int i = 0; i < 12; i++) { pinMode(relayPins[i], OUTPUT); digitalWrite(relayPins[i], LOW); } pinMode(buttonPin, INPUT_PULLUP); lcd.init(); lcd.backlight(); } void loop() { if (digitalRead(buttonPin) == LOW) { mainMenu(); } } void mainMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Relays B-Info"); lcd.setCursor(0, 1); lcd.print("D-Quit"); char key = keypad.waitForKey(); switch (key) { case 'A': relaysMenu(); break; case 'B': infoMenu(); break; case 'D': lcd.clear(); lcd.noBacklight(); break; } } void relaysMenu() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Enter relay #"); // Collect relay number (1-12) int relay = getKeypadValue(1, 12, "Enter relay #"); relay = relay - 1; // Adjust the relay index (0-11) lcd.setCursor(0, 1); lcd.print("A:ON/OFF B:TIMER "); lcd.setCursor(0, 2); lcd.print("C:Program D:Exit "); bool exit = false; while (!exit) { char key = keypad.waitForKey(); int value; switch (key) { case 'A': relayStates[relay] = !relayStates[relay]; break; case 'B': value = getKeypadValue(0, 999, "Enter Timer:"); if (value > -1) { relayStates[relay] = 1; timer[relay] = value; } break; case 'C': value = getKeypadValue(0, 24, "Enter Heure PH:"); if (value > -1) { Heure_PH[relay] = value; value = getKeypadValue(0, 60, "Enter Min PH:"); if (value > -1) { Min_PH[relay] = value; timer[relay] = getKeypadValue(1, 999, "Enter Duration:"); } } break; case 'D': exit = true; lcd.clear(); lcd.noBacklight(); break; } } } int getKeypadValue(int minVal, int maxVal, String infoText) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(infoText); lcd.setCursor(0, 1); lcd.print("A-VALIDER D-QUITTER"); int value = 0; bool exit = false; while (!exit) { char key = keypad.waitForKey(); if (key >= '0' && key <= '9') { int newVal = value * 10 + (key - '0'); if (newVal >= minVal && newVal <= maxVal) { value = newVal; lcd.print(key); } } else if (key == 'D') { return -1; // Cancel entry } else if (key == 'A') { exit = true; } } return value; } void infoMenu() { // Handle displaying and interacting with the info menu lcd.clear(); lcd.setCursor(0, 0); lcd.print("A-Temp B-Hum"); lcd.setCursor(0, 1); lcd.print("D-Quit"); bool quit = false; while (!quit) { char key = keypad.waitForKey(); switch (key) { case 'A': showTemps(); break; case 'B': showHumidity(); break; case 'D': quit = true; break; } } } void showTemps() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("T1:"); lcd.print(temp1, 1); lcd.setCursor(0, 1); lcd.print("T2:"); lcd.print(temp2, 1); lcd.setCursor(0, 2); lcd.print("T3:"); lcd.print(temp3, 1); lcd.setCursor(0, 3); lcd.print("T4:"); lcd.print(temp4, 1); lcd.setCursor(0, 4); lcd.print("D-Quit"); char key; do { key = keypad.waitForKey(); } while (key != 'D'); } void showHumidity() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("H1:"); lcd.print(hum1, 1); lcd.setCursor(0, 1); lcd.print("H2:"); lcd.print(hum2, 1); lcd.setCursor(0, 2); lcd.print("H3:"); lcd.print(hum3, 1); lcd.setCursor(0, 3); lcd.print("H4:"); lcd.print(hum4, 1); lcd.setCursor(0, 4); lcd.print("D-Quit"); char key; do { key = keypad.waitForKey(); } while (key != 'D'); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.