Store Multiple Digit Input from Keypad

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. Arduino Forum 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! :slight_smile: Please guide me!
Thanks!

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.

Oh that seems a good idea! Will try coding and seeing if ti works! Thanx!

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 = (100digit1)+(10digit2)+(1*digit3);
input = DutyCycle1;
return DutyCycle1;
}

arduino.txt (2.39 KB)

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

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.

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:

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

void setup(){
  Serial.begin(9600);
  Serial.println("ready");
}

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';
  }
}

For instance if I enter <12345> I get zero back. What am I doing wrong?

Add:

Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");

after:

  if(started && ended)
  {

and show a screen shot of your serial monitor.

This is what I get on the monitor if I enter an 8-digit number starting and finishing with the characters < and >:

inData: []
inData: []

You are using > for the start of packet marker, and < for the end of packet marker.

Try sending >12345< and see what happens.

Brilliant, thanks! It's working.

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)

what am i goon do?

Write some code.

please teach me

That isn't the role of this forum. The role here is to help you with programs you write. If you need to take a basic C or C++ class first, do that.