1

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

} 

지금은 .... "LCD"의 "직렬 ...."변화하고있어 내 LCD 디스플레이와 잘 작동합니다. 이것은 기본 코드이므로 이제는 미세 조정해야합니다. -cl

+0

실제 가치가 아닌 x에 대한 기대 가치를 알려주십시오. 코드에서 x = (keyPressed * 3)에 대한 X의 실제 값은 무엇입니까? – Craig

답변

0

우선이 문제를 단계적으로 접근하십시오. 즉, 먼저 키패드를 읽고 PC에서 직렬 회선을 통해 출력을 표시합니다. 이 코드가 작동하면 엔트리를 정수로 변환하는 코드를 작성하십시오.

키패드 항목을 단순히 표시 한 다음 해당 항목을 정수로 변환하는 예제를 제공하십시오.

+0

"이 코드가 작동하면 엔트리를 정수로 변환하는 코드를 작성하십시오." – user2635777

+0

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); // Serial Monitor : 2 (OK) Serial.println (customKeyINT * 3); } // Serial Monitor : 6 (대단원 !!) Serial.println (customKeyINT); // Serial Monitor : 0 (글로벌 변수 인 경우 왜?) Serial.println (customKeyINT * 3); }} // 시리얼 모니터 : 0 (디도) 고마워! – user2635777

+0

형식을 지정하여 코드를 제출할 수있는 방법이 있습니까? – user2635777

0

귀하의 의견에 따라, 여기에 귀하의 코드와 추가 코멘트가 하나 있습니다. 중첩 된 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! – 
    } 
} 
+0

작동하며 포인터를 사용할 필요가 없었습니다 ... 감사합니다! – user2635777

+0

고맙습니다 - 문제가 해결되었습니다. char2int() 함수를 만들었고 ir가 정상적으로 작동합니다. 이제 해결할 수없는 다음 단계는 키보드 입력의 두 번째 시리즈를 얻는 방법입니다. 다음은 간단한 코드입니다. – user2635777

+0

void loop() { char customKey = customKeypad.getKey(); (customKey == 'A') { Serial.println ("Press a key :"); // 시리얼 모니터 : pritns ok char key = customKeypad.getKey(); if (key) { Serial.print ("You entered :"); // 직렬 모니터 : 아무 것도 ... Serial.println (key); } } } } – user2635777