1

I want to get numbers entered from my Arduino keypad (which are of type 'char') converted to type 'int' so I can use them in variables and use math. For instance, if I press key "5", how do i put it into a variable "keyPressed" and do "X = (keyPressed * 3)" to get "int X = 15". I tired atol, atoi, reinterpret_cast, static_cast, etc. without success. I'm using a 'switch case' to get the entries from my keypad but any other method is ok. I have all that's needed to press a key and get an output like Serial.println(), etc. but no as an int value I can use in further calculations. Please help. Thank you.

This code finally works!: Thank you for all the help.

// Keypad***********************************************************************
#include <Keypad.h>
#include <Streaming.h>

#include <AFMotor.h>
#include <LiquidCrystal.h>

AF_Stepper motor1(200, 1);  // steps for 360 degrees = 1.8 degree/step
AF_Stepper motor2(200, 2);

const byte ROWS = 4;
const byte COLS = 4;
const int debounceTime = 20;

char keys[ROWS] [COLS] = { 
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'} };

byte rowPins[ROWS] = {9, 8, 7, 6};   // Arduino pins -red/yellow/green/blue
byte colPins[COLS] = {10, 11, 12, 13};   // Arduino pins- brown/gray/white/black

Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

//**************************************************************************************
char customKey;
int customKeyINT;
int keyINT;
char mtID;
char mtDir;
int mtIDINT;
int mtDirINT;
boolean mtDirBLN;
char mtSteps;
char mtSteps1;
char mtSteps2;
char mtSteps3;
int mtSteps1INT;
int mtSteps2INT;
int mtSteps3INT;
int steps;

// function entry() *****************************************************************
void entry()
{
     for (int i = 1; i > 0; i--)
        {
        Serial << "Custom FM move" << endl;
        Serial << "Enter: ";
        Serial <<  "1: Right/Left or " << endl;
        Serial << "2: Front/Back:" << endl;  
        while(Serial.available() == 0)
          {
          customKey = customKeypad.getKey();
          if (customKey)
             {
             mtID = customKey;
             Serial.print("You entered Motor: ");   
             Serial << mtID << endl;
             Serial.println();
             break;
             }
           }
         }
 // ------------------------------------------------------------------------       
     for (int i = 1; i > 0; i--)
        {
          Serial << "Enter: " << endl;
          Serial << "1: Front - 2: Back" << endl;  
          while(Serial.available() == 0)
             {
             customKey = customKeypad.getKey();
             if (customKey)
               {
               mtDir = customKey;
               if (mtDir == '1')
                  {
                  Serial.print("You entered: ");   
                  Serial << "Front" << endl;
                  Serial.println();
                  break;
                  }
               if (mtDir == '2')
                  {
                  Serial.print("You entered: ");   
                  Serial << "Back" << endl;
                  Serial.println();
                  break;
                  }
               }
             }
        }
// ---------------------------------------------------------------------  
     for (int i = 1; i > 0; i--)
        {
          Serial << "Enter # of steps" << endl;
          Serial << "i.e., 025 :" << endl;  
          while(Serial.available() == 0)
             {
             customKey = customKeypad.getKey();
             if (customKey)
               {
               mtSteps1 = customKey;
                  Serial.print("You entered: ");   
                  Serial << "First digit: " << endl;
                  Serial << mtSteps1 << endl;
                  Serial.println();
                  break;
               }
             }
        }
// ---------------------------------------------------------------------  
     for (int i = 1; i > 0; i--)
        {
          Serial << "Enter 2nd digit:" << endl;
          while(Serial.available() == 0)
             {
             customKey = customKeypad.getKey();
             if (customKey)
               {
               mtSteps2 = customKey;
                  Serial.print("You entered: ");   
                  Serial << mtSteps1 << mtSteps2 << endl;
                  Serial.println();
                  break;
               }
             }
        }
// ---------------------------------------------------------------------
     for (int i = 1; i > 0; i--)
        {
          Serial << "Enter last digit:" << endl;
          while(Serial.available() == 0)
             {
             customKey = customKeypad.getKey();
             if (customKey)
               {
               mtSteps3 = customKey;
                  Serial.print("You entered: ");   
                  Serial << mtSteps1 << mtSteps2 << mtSteps3 << endl;
                  Serial.println();
                  break;
               }
             }
        }
// 'steps' conversion from char to int -----------------------------------

      mtSteps1INT = char2int(mtSteps1);
      if (mtSteps1INT == 48)
      { mtSteps1INT = 0; }
      delay(20);

      mtSteps2INT = char2int(mtSteps2);
      if (mtSteps2INT == 48)
      { mtSteps2INT = 0; }
      delay(20);

      mtSteps3INT = char2int(mtSteps3);
      if (mtSteps3INT == 48)
      { mtSteps3INT = 0; }
      delay(20);

      steps = (mtSteps1INT * 100)+(mtSteps2INT * 10)+mtSteps3INT;
      Serial << steps << " steps" <<  endl;

// 'motor ID' and 'direction' conversion from char to int ----------------------
      mtIDINT = char2int(mtID);

      mtDirINT = char2int(mtDir);
      if (mtDirINT == 1)
      { mtDirBLN = HIGH; }

      delay(20);

      if (mtDirINT == 2)
      { mtDirBLN = LOW; }
      delay(20);



      if (mtIDINT == 1)
      { 
      motor1.step(steps, mtDirBLN, DOUBLE);   // this will run the motor
      Serial << "motor1.step(" << steps << ", " << mtDirBLN  << ", " << "DOUBLE)" << endl;
      }
      else 

      if (mtIDINT == 2)
      {
      motor2.step(steps, mtDirBLN, DOUBLE); // this will run the motor
      Serial << "motor2.step(" << steps << ", " << mtDirBLN  << ", " << "DOUBLE)" << endl;
      }
      delay(20);

// ---------------------------------------------------------------------
}  // end of function


// *****************************************************************************
void setup()
{
     Serial.begin(9600);
}  // end of SETUP *************************************************************


// *****************************************************************************
void loop()
{
   customKey = customKeypad.getKey();
   if (customKey)
   {
    if (customKey == 'A')
        { 
        entry();  
        }
   }

}

I'm now changing the "Serial...." for "lcd...." and it works fine with my lcd displays. This is the basic code now I have to fine tune it of course. -cl

  • You tell us what you expected value for x but not the actual value. What is the actual value of X for x=(keyPressed * 3) in your code? – Craig Jul 31 '13 at 16:28

2 Answers2

0

First of all, approach this problem in increments. That is, first simply read the keypad and display its output via the serial line back at the PC. After you get this to work then write code to convert the entry into an integer.

Please provide code examples of simply displaying the keypad entry and then an example of converting that entry into an integer.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
  • "After you get this to work then write code to convert the entry into an integer." – user2635777 Jul 31 '13 at 13:24
  • int customKeyINT; // global variable char customKey; // global variable void setup() { Serial.begin(9600); } void loop() { char customKey = customKeypad.getKey(); if (customKey) { if (customKey == '2') { int customKeyINT = 2; Serial.println(customKeyINT); // Serial Monitor: 2 (OK) Serial.println(customKeyINT * 3); } // Serial Monitor: 6 (GREAT!!) Serial.println(customKeyINT); // Serial Monitor: 0 (why if it's a global var.?) Serial.println(customKeyINT * 3); } } // Serial Monitor: 0 (dido) Thanks! – user2635777 Jul 31 '13 at 16:57
  • direct cuestion is atoi() function for convert a char to int – ealcober Mar 02 '21 at 22:57
0

Per your comment, here is your code, with one additional comment line. Deep within the nested ifs is the statement int customKeyINT = 2; the int declares a block variable as an integer which "masks" the global variable of the same name.

Remove the int portion of the line so that it reads: customKeyINT = 2;. The last two serial.println statements will also display 2 and 6 but only if you press 2 on the keypad. So, try using the atoi statement as the next incremental step towards a more generic solution.

    // global variable
    int customKeyINT; 

    // global variable
    char customKey;

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

    void loop() 
    { 
      char customKey = customKeypad.getKey();
      if (customKey) 
      { 
        if (customKey == '2') 
        { 
          // the following line of code is the culprit:
          int customKeyINT = 2;

          Serial.println(customKeyINT);  // Serial Monitor: 2 (OK)
          Serial.println(customKeyINT * 3); // Serial Monitor: 6 (GREAT!!)
       } 

       Serial.println(customKeyINT); 
       // Serial Monitor: 0 (why if it's a global var.?)     

       Serial.println(customKeyINT * 3);
       // Serial Monitor: 0 (ditto) Thanks! –
     }
 } 
JackCColeman
  • 3,777
  • 1
  • 15
  • 21
  • Thank you - problem solved, I made a function char2int() and ir works fine. Now the next step I can't solve is how to get a second series of keyboard entries. this is a simplified code: – user2635777 Aug 08 '13 at 15:03
  • void loop() { char customKey = customKeypad.getKey(); if (customKey) { if (customKey == 'A') { Serial.println("Press a key: "); // Serial monitor: pritns ok char key = customKeypad.getKey(); if (key) { Serial.print("You entered: "); // Serial monitor: nothing... Serial.println(key); } } } } – user2635777 Aug 08 '13 at 15:04
  • @user2635777 if you post a new question, and send me a comment i will look at your new code. as is it's pretty unreadable. However, as a general rule try to issue only one "customKeypad.getKey()` in `loop()` because loop really does just that and sometimes you will get bugs that only occur on a second or third iteration of a loop. So, having clean logic with only one input and/or one output statement avoids a lot of problems. – JackCColeman Aug 09 '13 at 02:08
  • thanks - how do you submit formatted code? when I paste it it loses all formatting... – user2635777 Aug 09 '13 at 16:24
  • @user2635777, when you are in edit mode there is a mini-menu bar above the edit area. Paste your code and select it, then press the {} button. Also, there needs to be a blank line between the text and the code. – JackCColeman Aug 09 '13 at 16:29
  • how do you get to 'edit mode' Also when I press ENTER it sends the comment right away instead of a new line. How do I enter a carriage return? should I make the comment in html? – user2635777 Aug 09 '13 at 21:22
  • I was referring to editing your code into your question or adding another answer. Not sure about comments.
    There is the help field just to the right.
    – JackCColeman Aug 09 '13 at 21:47
  • @JackColeman finally I got a working code to interact the keypad with the display (Serial monitor and lcd) and I guess I figure how to enter code in this site. Thanks again! – user2635777 Aug 12 '13 at 16:21