I’m working on a way to map a 4x4 keypad array to eventually connect the buttons to my HUE lighting system using IFTTT. Taking baby steps here, what I have is the keypad connected to all of the digitial inputs on my particle photon (D0 to D7). I’m just trying to see first if I can get the key pressed to show up in the particle console using the Particle.Publish() function. I keep running into an error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] when I try to using Particle.Publish(key) in the following code:
// This #include statement was automatically added by the Particle IDE.
#include <Keypad_Particle.h>
const byte ROWS = 4; //4 rows
const byte COLS = 4; //4 columns
//define symbols on buttons
char keys [ROWS] [COLS] =
{
{'1' , '2' , '3', 'A'},
{'4' , '5' , '6', 'B'},
{'7' , '8' , '9', 'C'},
{'*' , '0' , '#', 'D'}
};
byte rowPins[ROWS] = {D7, D6, D5, D4}; //connection from row
byte colPins[COLS] = {D3, D2, D1, D0}; //connection from column
//initialize an instance of class NewKeypad
Keypad keypad = Keypad (makeKeymap(keys) , rowPins , colPins , ROWS , COLS);
void setup ()
{
Serial.begin ( 9600 );
}
void loop ()
{
char key = keypad.getKey ();
if (key)
{
Serial.println (key);
Particle.publish(key);
}
}
I know the keypad is working however because if I change the Particle.publish(key); to Particle.publish(“button pressed”); the console displays this string for each button pressed. Can anyone help me understand what I have to change to get the specific key to display in the console?
You cannot use a single char
in place where the function expects a character array (aka string).
Try this instead
char key[2];
key[0] = keypad.getKey();
key[1] = '\0';
Particle.publish("pressed", key, PRIVATE);
Well the code works, I’ve replaced the void loop () section with:
void loop ()
{
char key = keypad.getKey ();
if (key)
{
Serial.println (key);
char key[2];
key[0] = keypad.getKey();
key[1] = '\0';
Particle.publish("pressed", key, PRIVATE);
}
but instead of the key pressed it returns null for every key, which I know has something to do with the \0 is null, correct? what is the PRIVATE do in that function as well?
I rather meant something along this line
char key[2];
key[0] = keypad.getKey();
key[1] = '\0';
if (key[0]) {
Serial.println(key);
Particle.publish("pressed", key, PRIVATE);
}
or even shorter
char key[2] = " ";
if (key[0] = keypad.getKey()) {
Serial.println(key);
Particle.publish("pressed", key, PRIVATE);
}
It limits the scope of the event so that only you see it - noone else is really interested to get told about which key you pressed on your keypad. Without the PRIVATE keyword the event will be published publicly.
Ok, so that worked great, I had my rows and columns mixed up so reversing those the code I used to get this working looks like this:
// This #include statement was automatically added by the Particle IDE.
#include <Keypad_Particle.h>
const byte ROWS = 4; //4 rows
const byte COLS = 4; //4 columns
//define symbols on buttons
char keys [ROWS] [COLS] =
{
{'1' , '2' , '3', 'A'},
{'4' , '5' , '6', 'B'},
{'7' , '8' , '9', 'C'},
{'*' , '0' , '#', 'D'}
};
byte rowPins[ROWS] = {D3, D2, D1, D0}; //connection from row
byte colPins[COLS] = {D7, D6, D5, D4}; //connection from column
//initialize an instance of class NewKeypad
Keypad keypad = Keypad (makeKeymap(keys) , rowPins , colPins , ROWS , COLS);
void setup ()
{
Serial.begin ( 9600 );
}
void loop ()
{
char key[2];
key[0] = keypad.getKey();
key[1] = '\0';
if (key[0])
{
Serial.println (key);
Particle.publish("pressed", key, PRIVATE);
}
}
for my own knowledge, can you explain how you chose the numbers in the square brackets after key?
C arrays are always zero based, so the first element in an array is referenced via [0]
and the second would be [1]
.
Also in C strings need to be terminated with a null character ('\0'
).
Since you want to assign the pressed key into the first character of the string and want that string to end after that, you terminate the string at the second element which is referenced as [1]
.
But these are some of the most fundamental C/C++ concepts you need to understand before you can hope to tackle anything actually useful