Arduino Keypad简单的代码不起作用

时间:2016-12-03 17:18:49

标签: arduino arduino-uno

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/

我不明白这个经过认证的简单代码并不起作用。我收到消息: 键盘:41:错误:' led_pin'在这方面没有申明     否则digitalWrite(led_pin,LOW);

#include <Keypad.h>

int led_pin=13;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  pinMode(led_pin,OUTPUT);
  Serial.begin(9600);
}

void loop(){
  char customKey = customKeypad.getKey();

  if (customKey){
    digitalWrite(led_pin,HIGH);
    Serial.println(customKey);
    delay(100);
  }
   else digitalWrite(led_pin,LOW);
}

如果我将另一个ledd_pin声明放入循环中,我会收到消息: 在函数&#39; void loop()&#39;: 键盘:35:错误:&#39; customKeypad&#39;在这方面没有申明    char customKey = customKeypad.getKey();

这首歌很奇怪,因为everythig必须在这个非常简单的程序上工作。

1 个答案:

答案 0 :(得分:-2)

我认为你应该检查一下&#39;的结构。 if循环中的语句。

void loop(){
  char customKey = customKeypad.getKey();

  if (customKey){
    digitalWrite(led_pin,HIGH);
    Serial.println(customKey);
    delay(100);
  } else {
    digitalWrite(led_pin,LOW);
  }
}
相关问题