Go Down

Topic: declaration ++; (Read 123 times) previous topic - next topic

i think i have a mistake in my code. can someone help me with this.. i'm trying to input two or more numbers in the 'Sec' after pressing the '*' key to send it to Serial but its function was not that good. the '#' continues to appear in serial and it does not read my inputted value in the keypad. here is an example..
when this function is called first it will wait 5 sec before it returns to the loop section but when i enter any number (for example 25) from the keypad, it will store the numbers to 'Sec' so that when i pressed the '*' key. it will be moved to Alarm.timerRepeat(Sec, OnlyOnce); so that 'Sec' will be replaced as 25. am i clear?? or do i am in the correct path?

Code: [Select]

void setAlarm()
{
//    Serial.println("Format should be: SS");
    Serial.println("Set Second Timer:");
while ( (unsigned long)(millis() - timeRef) < 5000 )
  {
  if (key != KEY_NOT_PRESSED)
  {
//      Serial.println(key);  } 
    if (key == '*'){
      Alarm.timerRepeat(Sec, OnlyOnce);
      Serial.print(Sec);
      Serial.print("Working n ata!!");
      Sec = 0;
    }
    else if (key == '#'){delay(1000);}
    else
    {
     key = Sec;
     Sec ++;
    }
  }
}



thank you.!

As  i understand it, you want to add any number you type in the keypad to the Sec variable, until you hit the * button . Am i right? 

What data type have you used for Sec and key variables?

johnwasser

I think what you want is:
Code: [Select]
    {
     // key = Sec;
     // Sec ++;
    Sec = Sec * 10 + key - '0';
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#3
Jan 31, 2015, 02:12 am Last Edit: Jan 31, 2015, 02:17 am by ellinnaj04
@connduino
i used char for key and byte for Sec..

@johnwasser
sir can you explain how do it comes up with that equation?? wny do i need to muliply it by 10?

PaulS

Quote
sir can you explain how do it comes up with that equation?? wny do i need to muliply it by 10?
Suppose that the first key you press results in '4'. Suppose that the second key you press results in '8'.

What value do you expect? Do the math yourself to understand why the equation MUST be that way.

ok. thanks.. my fault. i misunderstand the formula.
but, do i don't need to declare the
key = Sec and Sec ++;

PaulS

Quote
but, do i don't need to declare the
key = Sec and Sec ++;
Why would you? Isn't key the variable that is valued when a key is pressed? Assigning it another value before using the pressed key value doesn't make sense. The value of the pressed key is used to define the new value TO BE USED for Sec (when the appropriate "I'm done" event occurs).

thanks alot. its working already. next is i want to enter the whole time to be set. (e.g. Alarm.timerRepeat(wholeTime, OnlyOnce);)
and wholeTime should be equal to HH,MM,SS. any suggestion on how do i enter these digits from keypad in simple codes? i need to minimise the codes because theres a lot more to be added but there is almost 32kb only the arduino can handle..
is my explanation is clear?thanks..

PaulS

Quote
any suggestion on how do i enter these digits from keypad in simple codes?
Create a function to get a 2 digit value. Call that function to get the hours. Call it again to get the minutes. Call it a third time to get the seconds.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy