I am displaying single digits correctly in the serial monitor. What I want to do, is be able to take a string of number entries, terminated with the #, and save into a variable. So, push 1 2 3 4 # would save 1234 into a variable. The # tells the sketch that data entry is finished, and to listen for a new entry. Advice?
Current sketch:
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the kpd
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);
}
}
then a loop that adds entry characters to the array as long as entry is not #.
then I need to concatenate the array members, and add .mp3 to the end, so I can load a file from my sd card. I'm getting ahead of myself, and I don't know how to code this without some direction.
then a loop that adds entry characters to the array as long as entry is not #.
You already have that loop(). You need an index variable, to keep track of where you last wrote.
The process of getting intermittent data is the same, regardless of where the data comes from - the serial port, the keypad, an ethernet client. Robin2 and Nick Gammon have posted plenty on how to read from serial.
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the kpd
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char entryStr[5]; // This can hold up to 4 digits
int i=0;
void setup(){
Serial.begin(9600);
}
void loop(){
char key;
int index = 0;
while ( (key = keypad.getKey()) != '\n') { // Until they press Enter...
entryStr[index++] = key;
}
entryStr[index] = '\0'; // Make it a string
Serial.println(entryStr);
}
Remember that strings in C always terminate with the null character ('\0'). If you forget this, anything using the string will march through memory until it reads a zero somewhere. This also means that your input char array must be 1 element larger than you expect the input to be.
Code finished, here is the sketch that creates a filename from the entry, and allows you to cancel and start over with a * entry. # tells the sketch that the entry is ready to be processed. Thank you all (especially Mike McRoberts) for your help!
Feel free to suggest optimizations. Those variables need to be reset to zero after use for the next entry, so I'm not sure why you think they're squandered resources.