Interfacing of matrix keypad

Hello guys, I am working on a project which is based on Arduino mega 2560. I am interfacing the 4*4 matrix keypad with arduino board and taking inputs from keypad. I want to increment a counter when the key is pressed and do nothing when the key is released, i.e. as long as I hold the key in pressed mode it should increment the counter. However, I am not able to do so. I have used keypad library functions and tried many things but it didn't work. I am attaching my code. Please help me.

samplecode.ino (689 Bytes)

it didn't work

More details please.

What do you see when you print the values of the variables being tested by the program ? Are they what you expect ?

I have tried to check the response on a serial monitor. It shows nothing. Logically program seems correct as state and key are the two inputs we are getting from the keypad. I have also tried one condition at a time like as follows:

if (key=='1'){
counter=counter+1;
Serial.println(counter);
}

In this case it incremented the counter, but every time i had to press and release the key to increment it by 1.

Another code:

if(state==HOLD){
counter=counter+1;
Serial.println(counter);
}

In this code it incremented the counter whenever the key is pressed continuously which is exactly the result i expected. But it is doing the same for all the keys, i.e. even if i press '2' continuously it kept the counter incrementing.

Once you check that state is HOLD, you should check that the desired key is the one pressed.

     if(state == HOLD)
     {
        if(key == '1')
        {
           // increment the counter
        }
     }

PaulS:
Once you check that state is HOLD, you should check that the desired key is the one pressed.

     if(state == HOLD)

{
        if(key == '1')
        {
          // increment the counter
        }
    }

checked already. This didn't work either.

This didn't work either.

That lame statement doesn't either.

Some code you didn't post does something you didn't explain. You expected the code to do something. All that we know at this point is that what the code did is not what you expected.

But, that is ALL that we know.

Therefore, we can't help you.