Go Down

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

Jewel

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

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

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

Jewel

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

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

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.

alkopop79

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

alkopop79

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

PaulS

Add:
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.

alkopop79

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

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

Try sending >12345< and see what happens.

alkopop79


Go Up