I want to use the keypad.h library to ext my relay when I hold down the A key, but I can't seem to get it to work. When I use kp.getState() == HOLD, it does it when I hold any key, which is obviously not what I want. I know all the keys work because I can get it to print A when I push on it, but the loop I want doesn't occur.
Here is my code, the proceeding part is just if(key == 'x') for other keys.
while(kp.getState == HOLD && key == 'A'){ //While the extend button is pressed turn on the extend relay
switch (load_man) {//apply the coresponding load 0=Auto load(compression on extend and tension on retract) 1=no load;2=compression;3=tension
case 0:// auto load on extend is compression
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 1:// No Load
lcd.setCursor(0, 3);//
lcd.print("ext no Load ");
break;
case 2:// compression load
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 3:// Tension load
lcd.setCursor(0, 3);//
lcd.print("ext Tension ");
break;
}
key = kp.getKey();
pot = analogRead(A0);
pot = map(pot,0,1023,0,10000);
switch (lsf1) {//1=No limits///0=Limit switches///2=pot limits
case 0:
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 1:
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 2:
if ( pot < pot_ext){
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
}
else{
digitalWrite(13, LOW); // sets the LED off
digitalWrite(ext, LOW); //turn off ext relay
}
}
}
The keypad driver uses a polling technique to update itself. What that means is you will need to avoid using delay(), long for-loops, and while loops. You didn't include all your code so I can only assume that you are also calling getKey() before the while-loop otherwise you would never be able to get inside the loop.
I'm at work right now so I don't have long to answer your question but I moved kp.getKey() out of the while-loop and then changed the loop to an if-statement. As long as you hold down the 'A' key the if-statement will be true and then your switch-statements will be called. If you don't want your switch-statements to be called on every pass through the loop() then you can add a test for kp.keyStateChanged() to the if-statement.
If this isn't enough to get you going then I will try to help some more when I get home tonight.
loop()
{
key = kp.getKey();
//While the extend button is pressed turn on the extend relay
if(kp.getState == HOLD && key == 'A')
{
switch (load_man) {//apply the coresponding load 0=Auto load(compression on extend and tension on retract) 1=no load;2=compression;3=tension
case 0:// auto load on extend is compression
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 1:// No Load
lcd.setCursor(0, 3);//
lcd.print("ext no Load ");
break;
case 2:// compression load
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 3:// Tension load
lcd.setCursor(0, 3);//
lcd.print("ext Tension ");
break;
}
pot = analogRead(A0);
pot = map(pot,0,1023,0,10000);
switch (lsf1) {//1=No limits///0=Limit switches///2=pot limits
case 0:
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 1:
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 2:
if ( pot < pot_ext){
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
}
else{
digitalWrite(13, LOW); // sets the LED off
digitalWrite(ext, LOW); //turn off ext relay
}
}
}
} // end loop()
The code stanley gave me didn't work, but I figured it out before I came here. Based on one of stanley's other posts on holding down keys, I made another variable holdkey, that would save the key during the while loop where it checks if a key is held. I then reduced the holding time to give the illusion of it being a normal button that you can push or hold since I only need 1 of 2 keys being held down at any time.
char holdKey;
....
void setup()
{
kp.setHoldTime(10);
...
}
void manual()
{
key = kp.getKey();
if(key)
{
holdKey = key;
Serial.println(holdKey);
}
//More keys...
while(kp.getState() == HOLD) {
if(holdKey == 'A'){
switch (load_man) {//apply the coresponding load 0=Auto load(compression on extend and tension on retract) 1=no load;2=compression;3=tension
case 0:// auto load on extend is compression
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 1:// No Load
lcd.setCursor(0, 3);//
lcd.print("ext no Load ");
break;
case 2:// compression load
lcd.setCursor(0, 3);//
lcd.print("ext Compression ");
break;
case 3:// Tension load
lcd.setCursor(0, 3);//
lcd.print("ext Tension ");
break;
}
key = kp.getKey();
pot = analogRead(A0);
pot = map(pot,0,1023,0,10000);
switch (lsf1) {//1=No limits///0=Limit switches///2=pot limits
case 0://if limits is selected then just retract**************need to change this when limits are applied Now is set as no limits
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 1:// //if no limts is selected then just extend
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
break;
case 2:// if pot limits the compare actuator current position with the teach mode selected position(pot_ext)
if ( pot < pot_ext){
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ext, HIGH); // turn on ext relay
}
else{
digitalWrite(13, LOW); // sets the LED off
digitalWrite(ext, LOW); //turn off ext relay
}
}
}
else if (holdKey == 'B') { //While the retract button is pressed turn on the retract relay
switch (load_man) {//apply the coresponding load 0=Auto load(compression on extend and tension on retract) 1=no load;2=compression;3=tension
case 0:// auto load on extend is compression
lcd.setCursor(0, 3);//
lcd.print("ret Tension ");
break;
case 1:// No Load
lcd.setCursor(0, 3);//
lcd.print("ret no Load ");
break;
case 2:// compression load
lcd.setCursor(0, 3);//
lcd.print("ret Compression ");
break;
case 3:// Tension load
lcd.setCursor(0, 3);//
lcd.print("ret Tension ");
break;
}
key = kp.getKey();
pot = analogRead(A0);
pot = map(pot,0,1023,0,10000);
switch (lsf1) {//1=No limits///0=Limit switches///2=pot limits
case 0://if limits is selected then just retract**************need to change this when limits are applied Now is set as no limits
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ret, HIGH); // turn on ret relay
break;
case 1:// //if no limts is selected then just retract
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ret, HIGH); // turn on ret relay
break;
case 2:
if ( pot > pot_ret){
digitalWrite(13, HIGH); // sets the LED on
digitalWrite(ret, HIGH); // turn on ret relay
}
else{
digitalWrite(13, LOW); // sets the LED off
digitalWrite(ret, LOW); //turn off ret relay
}
}
}
else
{
break;
}
}
}