Pages: [1]   Go Down
Author Topic: Function for setting the time  (Read 225 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am attempting to use a keypad (finally got it working) to set the time.  The time and keypad libraries are invoked at the top.  digit1 and digit2 are both static ints.  The keypad takes in a variable assigned to it (0-9) and should then enter the switch statement where it takes key (a char data type) and give the function a number (the variable is 'conversion and is an int).

This is in the void loop.
Code:
  Serial.print("What hour is it?");
  digit1 =HOUR1digit();
  digit2 =HOUR2digit(); // Set the new hour
  NEWHOUR = setHOUR();
  delay(200);

These are three functions for setting the hour (outside of void loop).  The first two store a digit, and then the final function combines them.  I got the idea from a different thread in this forum.
Code:
int HOUR1digit(){ // USED FOR SETTING HOUR
key = KEYPAD.waitForKey();
delay(200);
switch (key){
case '1':{conversion=1;}
case '2':{conversion=2;}
case '3':{conversion=3;}
case '4':{conversion=4;}
case '5':{conversion=5;}
case '6':{conversion=6;}
case '7':{conversion=7;}
case '8':{conversion=8;}
case '9':{conversion=9;}
case '0':{conversion=0;}}
     digit1=conversion;
     Serial.println(key);
     Serial.println(digit1);
    return digit1;
}
int HOUR2digit(){ // USED FOR SETTING HOUR
    key =KEYPAD.waitForKey();
switch (key){
case '1':{conversion=1;}
case '2':{conversion=2;
case '0':{conversion=0;}}       
        Serial.println(key);
        digit2=conversion;
        Serial.println(digit2);
        return digit2;}}
       

int setHOUR(){ // USED FOR SETTING HOUR
int newHOUR;
if (digit1>2)
   {newHOUR=digit1;
   return newHOUR;}
newHOUR = (10*digit1)+digit2;
if (newHOUR>24){
    newHOUR=24;
    return newHOUR;}
return newHOUR;}

I am printing to the serial monitor to figure out what is happening.  It prints the correct key which I select, but in the switch statement, the output of 'conversion' is zero.  Any thoughts?
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to put some break statements in there to stop the case from falling thru.
Logged

Experience, it's what you get when you were expecting something else.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.
Logged

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1008
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could also replace the whole switch by a single line, as simple as:
Code:
digit1 = key - '0';
Logged

Pages: [1]   Go Up
Jump to: