system
January 6, 2011, 3:09pm
1
i m not sure if its just me or wad but ya i m having a weird problem... well this is my code, part of it i mean...
if (ledState==HIGH)
{
char key = keypad.getKey();
powerup();
}
the problem in this loop is that only the powerup funtion is being called continuosly, the getkey function doesnt at all happen...
and i need the powerup(); function to be called only once when the ledState becomes high...
i m not sure how to handle this...
if it is of any help, the getkey function is actually on a another .cpp file which is part of the library... is it because of that?
but the thing is, when i take out the powerup(); function from the IF loop, the getKey(); function happens.
i dont understand whats the problem... please do help... thank you...
system
January 6, 2011, 7:13pm
3
and i need the powerup(); function to be called only once when the ledState becomes high...
i m not sure how to handle this...
Make a global variable store the fact that you called the function (a flag) see the code below as an example.
bool isPowered = false;
Then set / check it before calling the function.
(untested code)
if (ledState==HIGH)
{
char key = keypad.getKey();
if (!isPowered ){
isPowered = true;
powerup();
}
}
If you need it to "reset" when the led goes low, then you would do a similar check on low and set the isPowered back to false in that case.
Hope that is what you were looking for.
Good Luck
system
January 7, 2011, 12:57am
4
I'm not sure I understand what is wrong with the program behavior (too little context is given) but your code snippet does have one problem.
if (ledState==HIGH)
{
[glow]char [/glow] key = keypad.getKey();
powerup();
}[glow] //right here[/glow]
variable key is created where I've highlighted and falls out of scope where i've commented "right here". That means that the variable key gets set but is never used in your code.
I'm guessing key is defined somewhere else too. Novice programmers make this mistake a lot because the compiler won't warn you that you have the same variable declared twice (because they are in different scopes and therefor not the same variable)