Go Down

### Topic: Store Multiple Digit Input from Keypad (Read 7931 times)previous topic - next topic

#### Jewel

##### Mar 17, 2012, 04:46 am
Hi!
I have a keypad interfaced with a Arduino Mega.
This is what I want to do:
The user inputs a frequency value (say 100). The number is stored in memory as a variable and then used to output a wave at that frequency (this uses a separate section of codes - I have this part running).
However, I am having issues capturing the input and storing it. As in, if I punch in 1,0,0 it should record as 100; 5,0 as 50 etc. I read through the tutorials and the forum. http://arduino.cc/forum/index.php?topic=57627.0 was helpful in that matter. But I can have a single, double or triple digit input and cannot think of how to code for this.
I hope I was able to explain that well.
I need help with the logic! Please guide me!
Thanks!

#### dxw00d

#1
##### Mar 17, 2012, 09:43 am
You need to have either a fixed terminating character, so you enter 5,0,# or 1,0,0,#, or you have to implement a timeout based routine. The fixed terminator is easiest to implement.

#### Jewel

#2
##### Mar 19, 2012, 03:57 am
Oh that seems a good idea! Will try coding and seeing if ti works! Thanx!

#### Jewel

#3
##### Mar 19, 2012, 05:00 am
Hi!

Does this make sense?

/Voltage - Functions to capture input, print on screen and store value//
//Voltage - Digit 1//
int GetVoltage1()
{
int key = keypad.getKey();
switch (key)
{
case NO_KEY: break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
lcd.print(key);
key = digit1;
break;
}
case '*':
{
voltage2();
break;
}
}
return digit1;
}

//Voltage - Digit 2//
int GetVoltage2()
{
int key = keypad.getKey();
switch (key)
{
case NO_KEY: break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
lcd.print(key);
key = digit2;
break;
}
case '*':
{
voltage2();
break;
}
}
}
return digit2;
}

//Voltage - Digit 3//
int GetVoltage3()
{
int key = keypad.getKey();
switch (key)
{
case NO_KEY: break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
lcd.print(key);
key = digit3;
break;
}
case '*':
{
voltage2();
break;
}
}
}
return digit3;
}

//Voltage - Calculate Input//
int DutyCycle1 ()
{
int input;
input = (100*digit1)+(10*digit2)+(1*digit3);
input = DutyCycle1;
return DutyCycle1;
}

#### 156sab

#4
##### Oct 10, 2012, 12:57 pm
Hi,

I am new to the arduino world and would like to implyment a similar
method of storing multiple digits from a keypad on a Arduino UNO.

Is the posted code easily adjusted to run it on the UNO and display via
the serial monitor function.

kind regards

shaun

#### PaulS

#5
##### Oct 10, 2012, 01:05 pm
Quote
Is the posted code easily adjusted to run it on the UNO and display via
the serial monitor function.

There really is no excuse for not just trying it.
The art of getting good answers lies in asking good questions.

#### alkopop79

#6
##### Dec 10, 2012, 10:03 am
I've been looking for a solution for being able to enter multiple digit numbers and convert them into integer. I found a piece of code form PaulS but it does not work properly. Whatever number I enter with the delimiters I get '0' back on the serial monitor. The code:

Code: [Select]
char inData[10];
int index;
boolean started = false;
boolean ended = false;

void setup(){
Serial.begin(9600);
}

void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '>')
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(aChar == '<')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0';
}
}

if(started && ended)
{
// Convert the string to an integer
int inInt = atoi(inData);

Serial.println(inInt);

// Get ready for the next time
started = false;
ended = false;

index = 0;
inData[index] = '\0';
}
}

#### alkopop79

#7
##### Dec 10, 2012, 10:04 am
For instance if I enter <12345> I get zero back. What am I doing wrong?

#### PaulS

#8
##### Dec 10, 2012, 11:37 am
Code: [Select]
Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");

after:
Code: [Select]
if(started && ended)
{

and show a screen shot of your serial monitor.
The art of getting good answers lies in asking good questions.

#### alkopop79

#9
##### Dec 10, 2012, 11:50 am
This is what I get on the monitor if I enter an 8-digit number starting and finishing with the characters < and >:

inData: []
inData: []

#### PaulS

#10
##### Dec 10, 2012, 11:52 am
You are using > for the start of packet marker, and < for the end of packet marker.

Try sending >12345< and see what happens.
The art of getting good answers lies in asking good questions.

#### alkopop79

#11
##### Dec 10, 2012, 11:57 am
Brilliant, thanks! It's working.

#### hanshinthant

#12
##### Sep 01, 2016, 03:45 pm
HI, I'm from myanmar starting using arduino.
i make a project with,HMC5883L ,20x4 LCD ,a 4x4 keypad and a BUZZER.These are to make a compuss  to know  heading degree and i input two degree from keypad (eg.34 ,90).when the compuss is exceed 34degree and 90degree,the buzzer alarm .what am i goon do?please teach me.(zinmeshinthant@gmail.com)

#### PaulS

#13
##### Sep 01, 2016, 03:59 pm
Quote
what am i goon do?
Write some code.

Quote