So now I have been able to toggle between the auto and manual modes. With C key pressed, LCD displays either Set Mode: Auto or Set Mode: Manual. I intend to use the D key as an enter key for my project such that when I press it the LCD displays Mode Set to Auto/ Manual, so that the user gets a confirmation that the mode of his choice has been successfully set.How should I go about it?
Set a variable (possibly an enum) in your handle*Press functions to indicate whether auto or manual option is displayed. Add the confirmation key 'D' to the switch statement. When 'D' is pressed check the value of the variable and then do whatever action you need to do to change mode.
Thanks, i shall try something & post it. ![]()
Instead of using an enum data type I tried using a boolean. Following is the code.
#include <LiquidCrystal.h>
#include <Keypad.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19); //RS,EN,D4,D5,D6,D7
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const unsigned long LongPress = 500;
boolean mode = FALSE;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2); //initializing LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Manual"); //default mode
}
void loop()
{
if (keypad.getKeys()) // check for keypad activity
{
// we won't handle multiple keypresses, just single ones, so just key index 0
const byte key = keypad.key[0].kchar;
const byte state = keypad.key[0].kstate; // IDLE, PRESSED, HOLD, RELEASED
switch (key) {
case 'C': {
static unsigned long pressedTime; // static so the value is remembered like a global
if (state == PRESSED) {
pressedTime = millis();
} else if (state == RELEASED) {
if (millis() - pressedTime > LongPress) {
handleLongPress();
} else {
handleShortPress();
}
}
} break;
case 'D': {
if (mode = FALSE)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode Set to Manual!");
}
}
else
{ lcd.setCursor(0, 0);
lcd.print("Mode Set to Auto!");
} break;
}
}
}
void handleShortPress() {
mode = TRUE;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Auto");
Serial.println("short");
}
void handleLongPress() {
mode = FALSE;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Manual");
Serial.println("long");
}
I still get the following errors:
sketch_aug29a:21: error: 'FALSE' was not declared in this scope
boolean mode = FALSE;
^
C:\Users\Umesh\Documents\Arduino\sketch_aug29a\sketch_aug29a.ino: In function 'void loop()':
sketch_aug29a:59: error: 'FALSE' was not declared in this scope
if (mode = FALSE)
^
sketch_aug29a:66: error: expected '}' before 'else'
else
^
sketch_aug29a:66: error: expected '}' before 'else'
sketch_aug29a:69: error: break statement not within loop or switch
} break;
^
C:\Users\Umesh\Documents\Arduino\sketch_aug29a\sketch_aug29a.ino: At global scope:
sketch_aug29a:72: error: expected declaration before '}' token
}
^
C++ is case sensitive, so it needs to be true and false.
if (mode = FALSE) is an assignment, you need if (mode == false), or simply if (! mode)
Your arrangement of pointy brackets {} is incorrect in case 'D':
I have figured everything out. Here is the code that works perfectly fine. Thanks for all the help. Cheers!!
#include <LiquidCrystal.h>
#include <Keypad.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19); //RS,EN,D4,D5,D6,D7
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const unsigned long LongPress = 500;
int i = 1;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2); //initializing LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Manual"); //default mode
}
void loop()
{
if (keypad.getKeys()) // check for keypad activity
{
// we won't handle multiple keypresses, just single ones, so just key index 0
const byte key = keypad.key[0].kchar;
const byte state = keypad.key[0].kstate; // IDLE, PRESSED, HOLD, RELEASED
switch (key) {
case 'C': {
static unsigned long pressedTime; // static so the value is remembered like a global
if (state == PRESSED) {
pressedTime = millis();
} else if (state == RELEASED) {
if (millis() - pressedTime > LongPress) {
handleLongPress();
} else {
handleShortPress();
}
}
} break;
case 'D': {
if(i==0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto Mode Set!");
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Manual Mode Set!");
}
} break;
}
}
}
void handleShortPress() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Auto");
i=0;
Serial.println("short");
}
void handleLongPress() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Mode:");
lcd.setCursor(10, 0);
lcd.print("Manual");
i=1;
Serial.println("long");
I have coding for selection function using keypad which can be used toinput of required temperature and humidity and number of eggs to incubate. Here is my code:
#include<Keypad.h>
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] ={
{'1','2',3,'a'}
{'4'5','6','b'}
{'7','8','9','c'}
{'*','0','#','d'}
};
byte rowPins{45,43,41,39};
byte colPins{44,42,40,38};
Keypad keypad =(makeKeymap(keys),rowPins,colPins,rows,cols);
int KPress=0,KStroke=0,KAst=0;
Int TLapse=0;
char KPrev,Knew;
void setup{
Serial.begin(9600);
}
void loop{
char key =keypad.getKey();
If(key){
KNew=key;
}
If (KPress==0){
if(KNew=='' && KStroke==1 && KAst==1){
Serial.print("A:Requires Temp and Hum");
KStroke=1;
}
If (Knew=='' && KStroke ==2 && KAst == 1){
Serial.print("B:No. Of eggs To Incubate");
KStroke=2;
}
If (KNew=='A'){
KPress=1;
KNew='/0';
key = ' ';
KAst =0;
}
If (KNew=='B'){
KPRess=2;
KNew=' ';
key=' ';
KAst=0;
}
}
(deleted)
kritischer:
Jeez... I'd rather get no help then get passive aggressive "help" like this
Why did you necro a 3 years old thread that was already necro'd and hijacked, just to say that?
(deleted)