Using a Keypad with Spark Core

I want to use a keypad with a Spark Core to make an electronic door entry system. Rather like these guys did: http://www.visionect.com/blog/epaper-door-keypad-spark-core-internet-of-things/ but using a standard 7 wire keypad with the code running on the Spark, a bit more like this http://playground.arduino.cc/Main/KeypadTutorial.

It seems like things headers like keypad.h etc. and I guess whatever libraries the compiler would link with aren’t available for the Spark. So, I wonder if anyone has any insight that could help?

Thanks!

2 Likes

I don’t have much experience with the keypad and the Spark headers, but why don’t you use this


add the touch pads to it and create your own touch keypad?
I was trying to compile the code and the great team of Spark helped me figure it out. You can find the discussion here

1 Like

Have a look at this library, its an improved version of the arduino keypad library and pretty well commented to make it easy to use.

As for using libraries that the spark doesnt have standard… you can look in the web IDE libraries section… there may be user submitted ones in there… otherwise look for the little + in the top right corner and add your own. Libraries usually have 2 files, .h and .cpp just cut and paste the contents of each into the new tabs and use the #include “whatever_you_named_it.h”

3 Likes

Thanks, I’ll look at your suggestions and report back.

1 Like

Thanks for your help so far Chris, I’ve got the .h and .cpp for keypad but it seems I need some others as well (WProgram.h and key.h) in fact they get included in keypad.h

Basically, I’m trying to end up using something like the code fragment below:

char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

I am to a degree handicapped by the fact that my last programming was in C and was a very long time ago. Having said that I think I ought to be able to get my head around it!

Anyway, hope you might be able to help me and thanks again.

1 Like

I don’t think you need WProgram.h. Would you mind posting all of your code in a Github gist so I can mirror your setup over here?

@jmcaddy, @harrisonhjones, I really need to finish my arduino porting notes!! Typically the front end of most arduino programs have code that does not apply to the Spark. In keypad.h, this code:

// Arduino versioning.
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"	// for digitalRead, digitalWrite, etc
#else
#include "WProgram.h"
#endif

is not required and can either be deleted BUT needs to be replaced with #include “application.h”. The easiest way is to use conditional compilation like this:

#if defined (SPARK)
#include "application.h"
#else
// Arduino versioning.
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"	// for digitalRead, digitalWrite, etc
#else
#include "WProgram.h"
#endif
#endif  //Spark

Similarly, in keypad.cpp, the compiler may have problems with #include <Keypad.h> so you can use the following:

#if defined (SPARK)
#include "Keypad.h"
else
#include <Keypad.h>
#endif

The rest of the code is generic and should work well with the Spark EXCEPT for the I/O pins you allocate for the keyboard and led. In the following, the two arrays must use Spark pin designations like D0, D1, A0, A1, etc.

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

As well, the onboard LED is on pin D7 so #define ledpin 13 should be:

#define ledpin D7

Enjoy! :smile:

3 Likes

@peekay123 beat me to it! The reason I suggested that library is because it’s pretty much ready to go without too many changes.

Being handicapped with a background in C isn’t a bad thing… we really just use c++ with some nice functions already made

When you put the code in and try and compile and it says there is an error in your code… I find the easiest way is start at the bottom and work my way up… looking at the line numbers mostly and trying to work out what the error means. If it says something like uint8_t is not defined or a pin doesn’t work like D0 then it’s normally the missing #include “application.h”

I actually have something that successfully compiles in the Web IDE! But, it’s bedtime here and so I’ll try flashing it tomorrow and hopefully it’ll work. Thanks for the help.

Hi

For a variety of reasons, I’ve only just back around to this project.

I have something that works but it’s behaviour is strange. Pressing a given key on the keypad seems not always to return the same digit (I’ve put some debugging in to report what was pressed each time). Initially, I suspected the keypad and disconnected it but even manually making the contact gives odd results (i.e. different numbers, for instance I might make the contact that should give a 7 and a lot of the time it will but then sometimes it will give a 3).

I tried setting pinMode function to PULL_DOWN on the relevant pins in case it was some sort of electrical issue whereby the pins were kind of floating (in voltage terms).

Anyway, any thoughts would be most appreciated.

Thanks.

James

@jmcaddy, which keyboard library are you using? Can you post your code?

Looking at the library, setting PULL_DOWN on the pins will not work as all the correct pin states are set by the library. The loop() timing and where and how often you call getKey() will affect the key scanning time and debounce and possibly the detected key value.

I also noticed some little things in the library (suggested by new Champion @Hootie81) which could cause problems. Tonight, I’ll do my version of the port and post it on my github. :smile:

Hi

Thanks for your reply.

The code is below - I commented a lot out just so it just reports back the key that was pressed.

Sorry forgot code.

@jmcaddy, you have two loo() functions!! Is that on purpose?

One thing I noticed is that you only call getKey() in Locked state. However, the keyboard library debounces by using millis() timers. I suggest you move the getKey() call outside the IF statement so it is called on every loop:

char key = kpd.getKey();

// Locked State - Monitor keypad for valid Password code entry
if (key != NO_KEY && LockState == LOCKED)
{
...

Also, remove all your INPUT_PULLDOWN pinMode() statements as they will interfere with the pinMode() settings of the keyboard library. You may want to replace your IF statements with a switch/case to clarify the key actions but that’s just my opinion. :smile:

1 Like

2 loops was an error.

Made your suggested changes and behaviour is still strange. A fragment of the serial output looks like this:

The reported 9 and 6 did correspond to the pressing of those keys but it seems to be whirring round the loop even when a key is not pressed. If I unplug the keyboard, there are sporadic bursts of output and if I plug a lead into one of the pins it seems to trigger a many more reported key presses.

I bet I’m doing something stupid but would really appreciate your thoughts.

Thanks again.

@jmcaddy, can you go back to one of the simple examples that came with the library so we can establish that the basics work? The HelloKeypad.ino example is ideal.

1 Like

I made a new project with the “HelloKeypad code”. I added in the Keypad.cpp and Keypad.h that I was using previously (that you talked about earlier in this thread).

The only changes I made to the code were the pin assignments (from Arduino to Spark) and replace the #include Keypad.h with a quoted one.

The behaviour remains similar. Output is being produced even when no key is pressed and intermittently when the keypad is disconnected.

@jmcaddy, can you try the keypad.cpp and keypad.h files from here. I did not like the declaration of some timers as unsigned int instead of unsigned long.

I believe, however, I found the real problem. In initializePins(), the Arduino code enables the INPUT pull-ups by writing a HIGH to the input pins. This does NOT work on the Core. Instead, I changed the pinMode() from INPUT to INPUT_PULLUP. This explains why you are getting weird results with no keys pushed or even with the keyboard out! :stuck_out_tongue_winking_eye:

Let me know how it goes :smiley:

It didn’t like your changed declarations I’m afraid!

So I changed them back to int and… it works perfectly.

Thank you so much. I’m on the road to never having to worry about teenagers forgetting keys again!

Ideally the debounceTime and holdTime should be unsigned longs given that’s what millis() returns. Is this not what you used?