【问题标题】:Keypad type conversion键盘类型转换
【发布时间】:2013-07-31 04:57:54
【问题描述】:

我想将从我的 Arduino 键盘(类型为“char”)输入的数字转换为类型“int”,以便我可以在变量中使用它们并使用数学。 例如,如果我按下键“5”,我如何将它放入变量“keyPressed”并执行“X =(keyPressed * 3)”以获得“int X = 15”。 我厌倦了 atol、atoi、reinterpret_cast、static_cast 等,但没有成功。 我正在使用“开关盒”从我的键盘获取条目,但任何其他方法都可以。 我拥有按下一个键并获得像 Serial.println() 等输出所需的一切,但没有我可以在进一步计算中使用的 int 值。 请帮忙。 谢谢。

这段代码终于可以工作了!:感谢您的所有帮助。

// 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();  
        }
   }

}

我现在正在将“Serial....”更改为“lcd....”,它适用于我的液晶显示器。这是基本代码,当然我必须对其进行微调。 -cl

【问题讨论】:

  • 您告诉我们您对 x 的期望值,而不是实际值。您的代码中 x=(keyPressed * 3) 的 X 的实际值是多少?

标签: casting arduino type-conversion integer-arithmetic numeric-keypad


【解决方案1】:

首先,逐步解决这个问题。也就是说,首先简单地读取键盘并通过串行线路将其输出显示在 PC 上。完成此操作后,编写代码将条目转换为整数。

请提供简单显示键盘条目的代码示例,然后提供将该条目转换为整数的示例。

【讨论】:

  • “在你让它工作之后,然后编写代码将条目转换为整数。”
  • int customKeyINT; // 全局变量 char customKey; // 全局变量 void setup() { Serial.begin(9600); } void loop() { char customKey = customKeypad.getKey(); if (customKey) { if (customKey == '2') { int customKeyINT = 2; Serial.println(customKeyINT); // 串行监视器:2 (OK) Serial.println(customKeyINT * 3); } // 串行监视器:6 (GREAT!!) Serial.println(customKeyINT); // 串行监视器:0(为什么是全局变量?) Serial.println(customKeyINT * 3); } } // 串行监视器:0(dido)谢谢!
  • 直接提示是 atoi() 函数,用于将 char 转换为 int
【解决方案2】:

根据您的评论,这是您的代码,还有一个额外的评论行。在嵌套 ifs 的深处是语句 int customKeyINT = 2; int 将块变量声明为整数,它“屏蔽”了同名的全局变量。

删除该行的int 部分,使其显示为:customKeyINT = 2;。最后两个serial.println 语句也将显示 2 和 6,但前提是您在键盘上按 2。因此,请尝试使用 atoi 语句作为迈向更通用解决方案的下一步。

    // 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! –
     }
 } 

【讨论】:

  • 谢谢 - 问题解决了,我做了一个函数 char2int() 并且 ir 工作正常。现在我无法解决的下一步是如何获得第二系列键盘条目。这是一个简化的代码:
  • void loop() { char customKey = customKeypad.getKey(); if (customKey) { if (customKey == 'A') { Serial.println("Press a key: "); // 串口监视器: pritns ok char key = customKeypad.getKey(); if (key) { Serial.print("你输入了:"); // 串口监视器:无... Serial.println(key); } } } }
  • @user2635777 如果您发布新问题并给我发评论,我会查看您的新代码。因为它很不可读。但是,作为一般规则,请尝试在loop() 中仅发出一个“customKeypad.getKey()”,因为循环确实就是这样做的,有时您会遇到仅在循环的第二次或第三次迭代时出现的错误。所以,有只有一个输入和/或一个输出语句的干净逻辑可以避免很多问题。
  • 谢谢 - 你如何提交格式化代码?当我粘贴它时,它会丢失所有格式...
  • @user2635777,当您处于编辑模式时,编辑区域上方有一个迷你菜单栏。粘贴您的代码并选择它,然后按 {} 按钮。此外,文本和代码之间需要有一个空行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多