Inputting A String of 2 Numbers Anchored by #

I have a Velleman 12 keys common output pin keypad. I have written the code to detect the numbers coming in and it works fine, printing the right number on the serial monitor.

The problem I'm having is that I need it to recognize a string of numbers. I need to input 52# and then output an led sequence. I have not been able to find anything using the non-matrix keypad.

Please help! Below is the code I'm currently using to read the input from the keypad.


void setup(){

Serial.begin(9600);

pinMode(2, INPUT_PULLUP); pinMode(3, INPUT_PULLUP); pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP); pinMode(6, INPUT_PULLUP); pinMode(7, INPUT_PULLUP); pinMode(8, INPUT_PULLUP); pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); pinMode(13, INPUT_PULLUP);

}

void loop(){

int three = digitalRead(2); // keypad pin 2 int seven = digitalRead(3); // keypad pin 3 int four = digitalRead(4); // keypad pin 4 int one = digitalRead(5); // keypad pin 5 int zero = digitalRead(6); // keypad pin 6 int eight = digitalRead(7); // keypad pin 7 int five = digitalRead(8); // keypad pin 8 int two = digitalRead(9); // keypad pin 9 int pound = digitalRead(10); // keypad pin 10 int nine = digitalRead(11); // keypad pin 11 int six = digitalRead(12); // keypad pin 12

if(seven==LOW){Serial.println(7);} if(four==LOW){ Serial.println(4); } if(one==LOW){ Serial.println(1); } if(zero==LOW){ Serial.println(0); } if(eight==LOW){ Serial.println(8); } if(five==LOW){ Serial.println(5); } if(two==LOW){ Serial.println(2); } if (pound==LOW){ Serial.println("#"); } if(nine==LOW){ Serial.println(9); } if(six==LOW){ Serial.println(6); } if (three==LOW) {Serial.println(3);}

}

Perhaps instead of just printing the digit, you should store in an an array, except when the digit is a #. When it is, NULL terminate the array and use atoi() to convert it to an int.

Or, create the number on the fly. Start with an int initialized to 0. Each time a digit is entered, multiply the int by 10 and add the new digit.

I'm still a little confused. I think I understand how to use the array, but wouldn't it still limit me to 9 inputs and outputs? I need to be able to have 50 unique inputs and outputs without any overlaps.

kchajin: I'm still a little confused. I think I understand how to use the array, but wouldn't it still limit me to 9 inputs and outputs? I need to be able to have 50 unique inputs and outputs without any overlaps.

From the question, I don't think you do understand how to use the array.

Suppose that the collection of characters from the keys pressed is '2', '3', '1', '5', '#', '2'.

You have an array that can hold 10 characters, called stuff:

char stuff[10];

You have an index into the array:

byte index = 0;

A key is pressed - '2'. You store it in the array, and add a NULL after it.

stuff[index++] = key;
stuff[index] = '\0';

After this, the array contains "2".

A key is pressed - '3'. You store it in the array, and add a NULL after it. After this, the array contains "23".

A key is pressed - '1'. You store it in the array, and add a NULL after it. After this, the array contains "231".

A key is pressed - '5'. You store it in the array, and add a NULL after it. After this, the array contains "2315".

A key is pressed - '#'. You do not store it in the array. Instead, you convert the value in the array to an int and reset the index and array:

   int val = atoi(stuff);
   index = 0;
   stuff[index] = '\0';

After this, the array contains "", and val == 2315.