help with Green house code

I’m building a green house where the user can control or set the desired levels of various parameters.
the user interacts with the program via a 4x4 keypad.the program should display the status of the current values but when a key is pressed the user should be able to edit the values desired.
i know how the sensors works but the main problem is that the program is not detecting my keypresses.

code:

#include <LiquidCrystal.h> // include the library code

include <Password.h>

#include <Keypad.h>

const int numRows = 2;//constants for the number of rows and columns in the LCD
const int numCols = 16;
int count = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins

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’}
};
float humidity, temprature, moisture; // variables globally declared
boolean light;

byte rowPins[ROWS] = {22, 23, 24, 25}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {26, 27, 28, 29}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
lcd.begin(numCols, numRows);
keypad.addEventListener(keypadEvent);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“LOADING”);
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 1);
lcd.write(255);
delay(170);
}
lcd.clear();

}

void loop()
{ // THE PROGRAM SHOULD DISPLAY THE STATUS OF SENSORS UNTILL A KEY IS PRESSED
keypad.getKey();

while (NO_KEY)//THIS DOES NOT WORK
{
lcd.print(“hello”);// EXAMPLE STATUS
}
}

void keypadEvent(KeypadEvent eKey)
{ switch (keypad.getState()) {
case PRESSED:
Serial.println(eKey);
delay(10);
switch (eKey)
{

// though i am able to get in this part

case ‘A’: lcd.clear(); // WANT USER TO ENTER A 4 DIGIT NUMBER WHICH CAN BE MADE AS SETPOINT OR UPDATES THE CURRENT VALUE
lcd.setCursor(0, 0);
lcd.print(“Enter Desired”);
lcd.setCursor(0, 1);
lcd.print(“HUMIDITY:”);// THE USER SHOLD BE ABLE TO ENTER THE VALUE HERE WHICH WOULD BE UPDATED
break;

case ‘B’: lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Enter Desired”);
lcd.setCursor(0, 1);
lcd.print(“TEMPRATURE:”);
break;

case ‘C’: lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Enter Desired”);
lcd.setCursor(0, 1);
lcd.print(“MOISTURE:”);
break;

case ‘D’: lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“LIGHT”);
lcd.setCursor(0, 1);
lcd.print(“ON/OFF”); //BOOLEAN TRUE/FALSE
break;
}
}

}

pease help

NEW_FILE.ino (2.36 KB)

  keypad.getKey();

  while (NO_KEY)//THIS DOES NOT WORK
  {
    lcd.print("hello");// EXAMPLE STATUS
  }

I am not familiar with the keypad library but should you not test the value returned by keypad.getKey();

you have to test it else your lcd would display some unknown characters. :)

vikram18: you have to test it else your lcd would display some unknown characters. :)

Where in your code are you testing the value returned by keypad.getKey() ? In any case, once the while loop is running how would the loop ever exit ?

vikram18:
you have to test it else your lcd would display some unknown characters. :slight_smile:

So why don’t you test it? The library page is very clear about it…

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}

i,ve change the code.the key part works for now used waitForKey() :) to stop the execution untill user enters a key.

now i'm unable to convert the character entered to integer

code:

case 'D': lcd.clear(); lcd.setCursor(0, 0); lcd.print("LIGHT sett."); for (int i = 0; i < 4; i++) { char see = keypad.waitForKey(); if (see != NO_KEY && isdigit(see))

{ number=atoi(see);//error comes here number is declared globally as int

lcd.setCursor(i, 1); lcd.print(number);

Serial.println(number); arr*=number;*

  • }*
  • }*
  • delay(500);*
  • lcd.clear();*
  • break;* errors:
  • ^* exit status 1 invalid conversion from 'char' to 'const char*' [-fpermissive]

Post whole within code tags

#include <LiquidCrystal.h> // include the library code
#include <Keypad.h>
const int numRows = 2;//constants for the number of rows and columns in the LCD
const int numCols = 16;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
int number;
int arr[4];
char input[4]; //buffer for input characters
String totalarray = "";
char keys[ROWS][COLS] =
{ {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
float humidity, temprature, moisture; // variables globally declared
boolean light;

byte rowPins[ROWS] = {22, 23, 24, 25}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {26, 27, 28, 29}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
  Serial.begin(9600);
  lcd.begin(numCols, numRows);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("INITIALIZING");
  for (int i = 0; i < 16; i++)
  {
    lcd.setCursor(i, 1);
    lcd.write(255);
    delay(100);
  }
  lcd.clear();
}

void loop()
{ lcd.setCursor(0, 0);
  lcd.print("Press key!");
  lcd.setCursor(0, 1);
  lcd.print("A::B::C::D::1");
  char selection = keypad.getKey();
  if (selection != NO_KEY)
  {
    switch (selection)
{
  case 'D': lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("LIGHT sett.");
        for (int i = 0; i < 4; i++)
        { char see = keypad.waitForKey();
          if (see != NO_KEY && isdigit(see))
          { number=atoi(see);
            lcd.setCursor(i, 1); lcd.print(number);
            
        
            Serial.println(number);
            arr[i]=number;
             
          }}}[code]

[/code]

i want to concatenate the 4 numbers entered so that they can be used to update the variable

If something is new, reading it back is a good idea ;)

And your indentation is a mess! See:

#include  // include the library code
#include 
const int numRows = 2;//constants for the number of rows and columns in the LCD
const int numCols = 16;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
int number;
int arr[4];
char input[4]; //buffer for input characters
String totalarray = "";
char keys[ROWS][COLS] =
{ {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
float humidity, temprature, moisture; // variables globally declared
boolean light;

byte rowPins[ROWS] = {22, 23, 24, 25}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {26, 27, 28, 29}; //connect to the column pinouts of the kpd

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
  Serial.begin(9600);
  lcd.begin(numCols, numRows);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("INITIALIZING");
  for (int i = 0; i < 16; i++)
  {
    lcd.setCursor(i, 1);
    lcd.write(255);
    delay(100);
  }
  lcd.clear();
}

void loop(){ 
  lcd.setCursor(0, 0);
  lcd.print("Press key!");
  lcd.setCursor(0, 1);
  lcd.print("A::B::C::D::1");
  char selection = keypad.getKey();
  if (selection != NO_KEY){
    switch (selection){
      case 'D': 
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("LIGHT sett.");
        for (int i = 0; i < 4; i++){
          char see = keypad.waitForKey();
          if (see != NO_KEY && isdigit(see)){ 
            number=atoi(see);
            lcd.setCursor(i, 1); lcd.print(number);
           
            Serial.println(number);
            arr[i]=number;
          }
        }
    }
  }
}

Wayyyy more readable. I prefer to put the { on the same line as the if/else/for/etc but putting it on a new line is fine as well. But then it should be the only thing on that line, so not:

          { number=atoi(see);

Together with

          }}}

Makes very hard to read code.

But back tot he problem.

First of all, you never try to combine all the keys... See is just 1 char. And atoi expects a string. But I would do something like:

if (selection != NO_KEY){
    switch (selection){
      case 'D': 
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("LIGHT sett.");
        number = 0;
        for (int i = 0; i < 4; i++){
          char see = keypad.waitForKey();
          if (see != NO_KEY && isdigit(see)){ 
            number *= 10;
            number += see - '0';
            lcd.setCursor(i, 1); lcd.print(number);
           
            Serial.println(number);
          }
        }
    }
  }