AndrewMascolo / OnewireKeypad

One Wire Keypad
MIT License
31 stars 14 forks source link

Can't read key value, but can read key state #10

Open johannesmols opened 5 years ago

johannesmols commented 5 years ago

Hi, I'm trying to make this work, but it doesn't give me any key as an output, but weirdly does recognize the key states. I triple checked my wiring, even tried to use different resistors, but to no avail. I also tried using different code versions from your example, but it's all the same result.

This is my code:

#include <OnewireKeypad.h>

const char KEYS[] = {
  '1', '2', '3', 'A',
  '4', '5', '6', 'B',
  '7', '8', '9', 'C',
  '*', '0', '#', 'D'
};

OnewireKeypad<Print, 16> keypad(Serial, KEYS, 4, 4, A0, 4700, 1000);

void setup() {
  Serial.begin(115200);
  keypad.SetKeypadVoltage(5.0); // Set keypad voltage
  keypad.SetHoldTime(100); // Key held time in ms
  keypad.SetDebounceTime(50); // Key debounce time in ms
}

void loop() {
  if (keypad.Key_State() == HELD)
  {
    char key = keypad.Getkey();
    Serial << "Pressed key: " << key << "\n";
  }
}

I wired it exactly like this guy showed in this video: https://youtu.be/W8ZSeLD90KA?t=6s What effect does the BitBool.h file have? I saw a few code examples with it, but most without and I didn't see any change whether I had it included or not.

Thank you! :)

AndrewMascolo commented 5 years ago

Does it say “Pressed Key:” with a blank space after it?

What happens if you change == HELD to PRESSED?

What does it show if you take out this, from your IF Statement? It’s should spam Pressed Key, but you should also see the key you pressed too.

char key = keypad.Getkey(); Serial << "Pressed key: " << key << "\n";

Sent from my iPhone

On Sep 25, 2018, at 10:27 AM, Johannes Mols notifications@github.com wrote:

Hi, I'm trying to make this work, but it doesn't give me any key as an output, but weirdly does recognize the key states. I triple checked my wiring, even tried to use different resistors, but to no avail. I also tried using different code versions from your example, but it's all the same result.

This is my code:

include

const char KEYS[] = { '1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D' };

OnewireKeypad<Print, 16> keypad(Serial, KEYS, 4, 4, A0, 4700, 1000);

void setup() { Serial.begin(115200); keypad.SetKeypadVoltage(5.0); // Set keypad voltage keypad.SetHoldTime(100); // Key held time in ms keypad.SetDebounceTime(50); // Key debounce time in ms }

void loop() { if (keypad.Key_State() == HELD) { char key = keypad.Getkey(); Serial << "Pressed key: " << key << "\n"; } } I wired it exactly like this guy showed in this video: https://youtu.be/W8ZSeLD90KA?t=6s What effect does the BitBool.h file have? I saw a few code examples with it, but most without and I didn't see any change whether I had it included or not.

Thank you! :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

johannesmols commented 5 years ago

Yes exactly, so the char is simply empty.

When I change that, it just stops spamming the output when holding down, but rather only prints it out once.

When I leave out the if-statement it spams out the exact same thing too, so it does recognize the key states but somehow fails to find the key. It's weird that it doesn't even put out a wrong key, it just doesn't return anything.

AndrewMascolo commented 5 years ago

Do your resistor values match the values set in the constructor? 4700 and 1000? If not, that may be the issue.

Sent from my iPhone

On Sep 25, 2018, at 11:47 AM, Johannes Mols notifications@github.com wrote:

Yes exactly, so the char is simply empty.

When I change that, it just stops spamming the output when holding down, but rather only prints it out once.

When I leave out the if-statement it spams out the exact same thing too, so it does recognize the key states but somehow fails to find the key. It's weird that it doesn't even put out a wrong key, it just doesn't return anything.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

johannesmols commented 5 years ago

Yes I am using 1K and 4.7K resistors. I also tried swapping the 4.7K out for a 5.1K resistor, but without change. I'm honestly out of ideas why that is happening as I checked the wiring about 5 times by now.

AndrewMascolo commented 5 years ago

Try this, make a new IF statement and see if the value from geyKey() is equal to '\0' (NO_KEY). If it does then it is not going into the code before it that outputs the corresponding key.

Sent from my iPhone

On Sep 25, 2018, at 12:26 PM, Johannes Mols notifications@github.com wrote:

Yes I am using 1K and 4.7K resistors. I also tried swapping the 4.7K out for a 5.1K resistor, but without change. I'm honestly out of ideas why that is happening as I checked the wiring about 5 times by now.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

johannesmols commented 5 years ago

Hi, I tried what you said and it does come up as NO_KEY. What does that imply?

I used this code to try it out:

void loop() {
  byte state = keypad.Key_State();
  switch (state)
  {
    case PRESSED:
      Serial << "Pressed..." << "\n";
      break;
    case RELEASED:
      Serial << "Released..." << "\n";
      break;
    case HELD:
      Serial << "Holding down..." << "\n";
      if (char key = keypad.Getkey() == NO_KEY) {
        Serial << "Couldn't find key\n";
      } else {
        Serial << "Key: " << key << "\n";
      }
      break;
    case WAITING:
      Serial << "Waiting..." << "\n";
      break;
  }

  delay(100);
}

The result looks like this, when pressing a button for a few seconds:

Waiting...
Pressed...
Holding down...
19:23:16.454 -> Couldn't find key
Holding down...
19:23:17.051 -> Couldn't find key
Holding down...
19:23:17.648 -> Couldn't find key
Released...
Waiting...

I'm not sure what to make of this. Thank you for your help by the way, really appreciate it!

AndrewMascolo commented 5 years ago

What board are you using? I’m assuming it’s the UNO, can you check if you are getting 5 volts. The only reason to get NO_KEY is if the analog pin is not getting enough voltage.

There is also a parameter in the .h file for KP_Tolerance, what value is it set to. It should be 20.

For a quick debug, simply do an analogRead(A0) and print it’s value. What do you get?

Sent from my iPhone

On Sep 25, 2018, at 1:27 PM, Johannes Mols notifications@github.com wrote:

Hi, I tried what you said and it does come up as NO_KEY. What does that imply?

I used this code to try it out:

void loop() { byte state = keypad.Key_State(); switch (state) { case PRESSED: Serial << "Pressed..." << "\n"; break; case RELEASED: Serial << "Released..." << "\n"; break; case HELD: Serial << "Holding down..." << "\n"; if (char key = keypad.Getkey() == NO_KEY) { Serial << "Couldn't find key\n"; } else { Serial << "Key: " << key << "\n"; } break; case WAITING: Serial << "Waiting..." << "\n"; break; }

delay(100); } The result looks like this, when pressing a button for a few seconds:

Waiting... Pressed... Holding down... 19:23:16.454 -> Couldn't find key Holding down... 19:23:17.051 -> Couldn't find key Holding down... 19:23:17.648 -> Couldn't find key Released... Waiting... I'm not sure what to make of this. Thank you for your help by the way, really appreciate it!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

AndrewMascolo commented 5 years ago

Also I apologize if I’m taking too long to reply back, I’m still at work and don’t get home until 5pm EST

Sent from my iPhone

On Sep 25, 2018, at 1:27 PM, Johannes Mols notifications@github.com wrote:

Hi, I tried what you said and it does come up as NO_KEY. What does that imply?

I used this code to try it out:

void loop() { byte state = keypad.Key_State(); switch (state) { case PRESSED: Serial << "Pressed..." << "\n"; break; case RELEASED: Serial << "Released..." << "\n"; break; case HELD: Serial << "Holding down..." << "\n"; if (char key = keypad.Getkey() == NO_KEY) { Serial << "Couldn't find key\n"; } else { Serial << "Key: " << key << "\n"; } break; case WAITING: Serial << "Waiting..." << "\n"; break; }

delay(100); } The result looks like this, when pressing a button for a few seconds:

Waiting... Pressed... Holding down... 19:23:16.454 -> Couldn't find key Holding down... 19:23:17.051 -> Couldn't find key Holding down... 19:23:17.648 -> Couldn't find key Released... Waiting... I'm not sure what to make of this. Thank you for your help by the way, really appreciate it!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

AndrewMascolo commented 5 years ago

This is my skype: andrew_mascolo

Sent from my iPhone

On Sep 25, 2018, at 1:27 PM, Johannes Mols notifications@github.com wrote:

Hi, I tried what you said and it does come up as NO_KEY. What does that imply?

I used this code to try it out:

void loop() { byte state = keypad.Key_State(); switch (state) { case PRESSED: Serial << "Pressed..." << "\n"; break; case RELEASED: Serial << "Released..." << "\n"; break; case HELD: Serial << "Holding down..." << "\n"; if (char key = keypad.Getkey() == NO_KEY) { Serial << "Couldn't find key\n"; } else { Serial << "Key: " << key << "\n"; } break; case WAITING: Serial << "Waiting..." << "\n"; break; }

delay(100); } The result looks like this, when pressing a button for a few seconds:

Waiting... Pressed... Holding down... 19:23:16.454 -> Couldn't find key Holding down... 19:23:17.051 -> Couldn't find key Holding down... 19:23:17.648 -> Couldn't find key Released... Waiting... I'm not sure what to make of this. Thank you for your help by the way, really appreciate it!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

johannesmols commented 5 years ago

Yes I'm using the UNO. That is interesting, I do have some other stuff connected to that might be a problem. The KP_Tolerance is set to 20, yes.

When reading the analog value from the pin, the output is around 60. Converting this to voltage doing this Serial << analogRead(A0) * (5.0 / 1023.0) << "\n"; gives me an input of around 0.2V, so that's definitely not enough! If the math is correct. I got it from here: https://www.arduino.cc/en/Tutorial/ReadAnalogVoltage

I will try and unplug all the other stuff and see if that works, or plug it in via a wall plug and not USB, and then output it to a LCD. Let's hope this works.

And don't worry about taking too long, I'm very happy that you're replying at all :) Especially if you're still at work.

johannesmols commented 5 years ago

I just realized that I'm an idiot and the voltage of course changes when I press a button. So it varies between almost 0 and 5V depending on what I press. I rearranged the circuit now though, and now I get the message that I'm holding down a button all the time, so I'll try to fix that now. This is really strange.

AndrewMascolo commented 5 years ago

Any luck?

Sent from my iPhone

On Sep 25, 2018, at 2:04 PM, Johannes Mols notifications@github.com wrote:

I just realized that I'm an idiot and the voltage of course changes when I press a button. So it varies between almost 0 and 5V depending on what I press. I rearranged the circuit now though, and now I get the message that I'm holding down a button all the time, so I'll try to fix that now. This is really strange.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

johannesmols commented 5 years ago

Hey, I gave up on the idea of making it work and will just use an IR remote + sensor for my project now. Maybe I will try it again one day, and will update you then. But thank you for your help!

AndrewMascolo commented 5 years ago

My sincerest apologies, there was a problem with the debounce code, it is fixed now.

johannesmols commented 5 years ago

Awesome, thank you! I will try it out some day and let you know if it works for me.

pwrsoft commented 5 years ago

I have made a DIY keyboard. Updated the library to the latest sources. When tun a sketch – getting an empty values from Getkey().

AndrewMascolo commented 5 years ago

It was working when I posted it but I’ll take another look.

dennissivia commented 5 years ago

I have the same issue as mentioned above. After adding the switch statement for debugging i got the following output from showRange

V:5.00, AR: 0.00 | V:2.50, AR: 0.00 | V:1.67, AR: 0.00 | V:1.25, AR: 0.00 | V:0.82, AR: 0.00 | V:0.70, AR: 0.00 | V:0.62, AR: 0.00 | V:0.55, AR: 0.00 | V:0.45, AR: 0.00 | V:0.41, AR: 0.00 | V:0.38, AR: 0.00 | V:0.35, AR: 0.00 | V:0.31, AR: 0.00 | V:0.29, AR: 0.00 | V:0.27, AR: 0.00 | V:0.26, AR: 0.00 |

My values for analogRead(A0) * (5.0 / 1023.0) are roughly similar to the V entries above. However they are off by 0.2 at the beginning. My biggest concern are the AR values. I am not sure why they are all 0.00 Is there issue with mixing unsigned int and floats in the code base ?

AndrewMascolo commented 5 years ago

Either your pin range is zero or keypad voltage is zero. You may wanna look at the set range and set keypad voltage functions. You are seeing AR as zero because the Analog_factor which is the correlation between the voltage and analog range.

dennissivia commented 5 years ago

Hi, thanks for the quick response. I just debugged it and you are right. pinRange is 0. Is this expected behaviour? I will try to just call setAnalogPinRange in my code now. If that works I wonder if it might make sense to also initialise pinRange since ANALOG_FACTOR is also initialised as if pinRange was 1023. An alternative would be pointing it out in the Readme.

dennissivia commented 5 years ago

setting the pinRange did the job. I added this to my setup function:

  KP.SetAnalogPinRange(1023);
  KP.SetKeypadVoltage(5.0);

The order is also important because SetKeypadVoltage is using pinRange internally to compute ANALOG_FACTOR. So changing the order of the two lines will also lead to a pinRange and thus an AR of 0.0. However adding these lines fixed it for me. Thanks 👍

AndrewMascolo commented 5 years ago

Did you just now add both functions or did you have only one of them before. I say this because the analog_factor is set when you initialize the the constructor. It is changed when you use those two functions.

I will also add a Read Me with this info in it.

Sent from my iPhone

On Feb 26, 2019, at 5:34 PM, Dennis Sivia notifications@github.com wrote:

setting the pinRange did the job. I added this to my setup function:

KP.SetAnalogPinRange(1023); KP.SetKeypadVoltage(5.0); The order is also important because SetKeypadVoltage is using pinRange internally to compute ANALOG_FACTOR. So changing the order of the two lines will also lead to a pinRange and thus an AR of 0.0. However adding these lines fixed it for me. Thanks 👍

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

dennissivia commented 5 years ago

I had KP.SetKeypadVoltage(5.0); all the time. but without setting the pinRange before. Thus overwriting the automatically set value with 0, because of pinRange being 0. I tested now with having neither of them and it works as expected with the values initialised by the constructor. I just added the line initially, because all examples here and externally always mention to set SetKeypadVoltage but don't mention SetAnalogPinRange.

So it's important to have either none of them or both at them in the right order, then everything works as expected. Maybe this was also the issue that @johannesmols and @pwrsoft had in their examples.

AndrewMascolo commented 5 years ago

Probably, with that said I will try to get around cleaning up the code. I’ve been meaning to do it, I just haven’t really had the time.

Sent from my iPhone

On Feb 26, 2019, at 5:48 PM, Dennis Sivia notifications@github.com wrote:

I had KP.SetKeypadVoltage(5.0); all the time. but without setting the pinRange before. Thus overwriting the automatically set value with 0, because of pinRange being 0. I tested now with having neither of them and it works as expected with the values initialised by the constructor. I just added the line initially, because all examples here and externally always mention to set SetKeypadVoltage but don't mention SetAnalogPinRange.

So it's important to have either none of them or both at them in the right order, then everything works as expected. Maybe this was also the issue that @johannesmols and @pwrsoft had in their examples.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

dennissivia commented 5 years ago

Thanks, I appreciate your support. If there is anything I can assist you with regarding the implementation, do not hesitate letting me know, even though I am aware that this might not be the case for rewrites and clean-ups, but anyway.

Cheers