Variable length input from keypad

Is there a way to get a number from a keypad that is variable length. For example the user could enter only 5, or multiple digit numbers like 1085, or 15281. Getting a single digit from the keypad seems simple enough but I’m not sure where to start to get multiple digits of input. If this were Python I’d add each key to a list with foo.append(key) and look at the whole list at the end, but I don’t see anything like a list or append for the Arduino. Seems like an array requires a fixed length which could be an issue.

Ideally the user pushes # on the keypad and the Arduino keeps track of what is entered, for example the user then pushes 5, 7, and 1. When they are done they push # again to end the entry. From there I’d love to have 571 in a variable I can access as a value, or 21360, or whatever value the user happens to enter.

This is for a camera shutter release I’m working on. The user will enter a delay that will be used between when a event is triggered and when the camera actually takes a picture. Any help would be greatly appreciated! If this is not possible I’m open to other suggestions too. Cheers!

but I don't see anything like a list or append for the Arduino

Why bother with a list, just add each digit into the variable as it comes. In pusdio code the idea is:- Start with a variable int num = 0; The if(newKey != '#') num = (num * 10) + newKey That sort of thing, of course you can put all the error catching stuff in and do the conversion between the newKey and the actual number.

Yes, as Mike shown you have to 'byte bang' it as the Arduino serial port is a single character at a time affair. You as the programmer have to assemble the characters, define and detect a end of entry character. You also have to keep checking the serial data port for when a new user entry might start.

Once you have the basic idea down, it is often useful to write it as a standalone function and then just call the function in you main code when you need to.

Lefty

That makes complete sense. Sometimes the most obvious solutions evade me. I'll give it a shot tonight and see how it goes. Cheers!

I tired it out and the values that were returned were very odd. Instead of returning zero, it would return 48, instead of 1 it would return 49, and so on. After a while it struck me that the int() function was turning the ASCII character number directly into an integer, rather than the integer itself. As a work around I’m using int(key-48) which returns the correct value, but is there a better way to do this? Cheers!

As a work around I'm using int(key-48) which returns the correct value, but is there a better way to do this?

The way you figured out yourself is perfectly good way to convert numbers from ascii charaters to integers.

You could make it easier on yourself (and anyone else who reads your code) by writing key - '0'

Good call! I'm used to not being able to mix strings and integers (Python), cool that its possible to do that here. Cheers!