Go Down

Topic: Reading from LCD display [for a timer.] (Read 141 times) previous topic - next topic

zedhp

Apr 20, 2019, 07:25 pm Last Edit: Apr 20, 2019, 07:29 pm by zedhp
Hi


I'm fairly new to arduino and c/c++ programming, and my project requires me to make a security device that randomly flickers the house lights. The timer has on and off limits set by the user, e.g: turns on from 6PM to 7AM.

What I'm trying to do is use the LCD as an interface for the user to set their on and off time limits. But I can't figure out how to read those time limits set by the user from the LCD.

This is my current code:

Edit: I'm using a membrane board to input the key values into the lcd.

Code: [Select]
#include <DS3231.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <ctype.h>


Time t;

DS3231  rtc(SDA, SCL);

int led1 = 37;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {

{'1','2','3', 'A'},

{'4','5','6', 'B'},

{'7','8','9'},

{' ','0'}

};

int curLoc = 4;

//hh:mm
//HxHy:MxMy
String Hx;
String Hy;
String Mx;
String My;

String Hr;
String Mn;

int iHr;

int ontime;
signed short onHour;
signed short offHour;

const int onHr;
const int onMn;


byte rowPins[ROWS] = {46,47,48,49}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {50,51,52,53}; //connect to the column pinouts of the keypad

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char timeset[16];

void setup(){
Serial.begin(115200);

pinMode(led1, OUTPUT);

digitalWrite(led1, LOW);

rtc.begin();

lcd.begin(16,2);

lcd.setCursor(4,0);
lcd.print("Set Time");
lcd.setCursor(0, 1);
sprintf(timeset,"On  %0.2d : %0.2d  Off", onHour, offHour);
lcd.print(timeset);
lcd.setCursor(4,1);
}

int minCur = 4;
int maxCur = 10;

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

  t = rtc.getTime();
  int Hour = t.hour;
  int Min = t.min;
  int Sec = t.sec;
  /*
   * cursor locations
   * On from: 8 9   14 15
   *       On 4 5 : 8 9 Off
   */
if(key == 'A'){
  if(curLoc >= minCur && curLoc <= maxCur){
    if(curLoc == 5){
          curLoc +=3;
    }
        curLoc += 1;
     if(curLoc >= maxCur+1){
      curLoc = 4;
    }
    lcd.setCursor(curLoc, 1);
  }
}

if(key == 'B'){
  if(curLoc >= minCur && curLoc <= maxCur){
    if(curLoc == 9){
      curLoc-=3;
    }
    curLoc -=1;
    if(curLoc <= minCur-1){
      curLoc = 10;
    }
    lcd.setCursor(curLoc, 1);
  }
}
if(key && key != 'A' && key != 'B'){
  if(curLoc >= minCur && curLoc <= maxCur){
//    if(curLoc == 4){
//      Hx = key;
//    }
//    if(curLoc == 5){
//      Hy = key;
//    }
//    if(curLoc == 14){
//      Mx = key;
//    }
//    if(curLoc == 15){
//      My = key;
//    }

    lcd.print(key);
    lcd.setCursor(curLoc, 1);
   
       }
   }
   if(Hour >= onHour && Hour <= offHour){
    int randomtimer = random(100, 1000);
    for(int i = 0; i <= randomtimer; i+=random(100,1000)){
      digitalWrite(led1, HIGH);
      Serial.println("I'm on " + (String) Hour + ":" + (String)onHour + ":" + (String)offHour);
    }
   }else{
    digitalWrite(led1, LOW);
    Serial.println("I'm off " + (String) Hour + ":" + (String)onHour + ":" + (String)offHour);
   }

}

wvmarle

So what does this code do and how is it different from what you want it to do?
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

jremington

It would make more sense to read the values as input from the keypad, don't you think?

zedhp

So what does this code do and how is it different from what you want it to do?
It just prints the Set Time message in void setup();
"On  %0.2d : %0.2d  Off" is to set the on and off time limits. When someone presses the A button on the membrane keypad, it moves the cursor to the next position to write the second number. for example: "Press 1, A 2" that's gonna set the On Time hour to 12. B button does just the same, it just moves the cursor backwards.

It would make more sense to read the values as input from the keypad, don't you think?
I tried doing that, but the issue is that I'm trying to compare t.hour or Hour, which is a 1 double digit number to 2 single digit numbers. When I enter the on and off limits, those numbers are separate numbers. For example, the 1 is its sole integer and 2 is its sole integer as well. I have no idea how to read that as 12 instead of 1 and 2.

Hope that makes sense.

jremington

#4
Apr 21, 2019, 02:56 am Last Edit: Apr 21, 2019, 02:58 am by jremington
12 = 10*1 + 2.

You are really doing this the hard way. It is much easier to use the keypad to enter genuine numbers (plenty of examples on the web) and use those numbers for display and to set the clock and the On/Off times.

As for comparing times, use the Arduino Time library and internally, you can compare all times in terms of minutes past midnight. If On/Off times span midnight, there is a simple trick for that comparison.

wvmarle

Why ask the user to enter 1 A 2 instead of 1 2 for a number? Use the A or B button as correction. Just advance the cursor the moment a number is entered.

Also react only to the numbers that are valid. So for the first digit of the hour that'd be 0, 1 or 2. For the second digit 0-9 if the first is 0 or 1, 0-4 if the first is 2.

Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up