Arduino Pin 4 连接到键盘总是高电平导致键盘不工作
Arduino Pin 4 connected to keypad alsways HIGH resulting in not working keypad
我正在尝试将 keypad 集成到我的项目中,并想先在一个简单的程序中对其进行测试。
键盘像这样连接到 Arduino Uno (ATmega328P):
C1 -> 引脚 6
C2 -> 引脚 8
C3 -> 引脚 4
R1 -> 引脚 7
R2 -> 引脚 2
R3 -> 引脚 3
R4 -> 引脚 5
我下载了这个键盘的模板程序,并相应地调整了我的引脚配置。
代码如下:
#include <LCD5110_Graph.h>
/* keyPadHiduino Example Code
by: Jim Lindblom
date: January 5, 2012
license: MIT license. If you find this code useful, please
feel free to use this code however you'd like, commercially
or otherwise. Just keep this license on there whatever you do.
This code implements a 12-key USB keypad. You can type 0-9,
* is the + sign and the # key is enter. I'm using SparkFun's
12-button keypad, your pinouts may vary. Multi-touch is
not supported.
SparkFun Keypad Pinout:
Rows and columns are connected as such:
-------------
R1 | 1 | 2 | 3 | - 7 (grün)
R2 | 4 | 5 | 6 | - 2 (braun)
R3 | 7 | 8 | 9 | - 3 (schwarz)
R4 | * | 0 | # | - 5 (violett)
-------------
|C1 |C2 |C3
6(blau) 8(gelb) 4(weiß)
*/
// Pins 1-7 of the keypad connected to the Arduino respectively:
uint8_t keypadPins[7] = {2, 3, 4, 5, 6, 7, 8};
int keypadStatus; // Used to monitor which buttons are pressed.
int timeout; // timeout variable used in loop
LCD5110 LCD(9, 10, 11, 12, 13);
extern uint8_t SmallFont[]; //charactersize 6x8 pixels
extern uint8_t TinyFont[]; //charactersize 4x6 pixels
extern uint8_t MediumNumbers[]; //charactersize 12x16 pixels
void setup()
{
LCD.InitLCD();
LCD.setFont(TinyFont);
for (uint8_t i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs
digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high
}
keypadStatus = 0;
Serial.begin(9600);
}
void loop()
{
keypadStatus = getKeypadStatus(); // read which buttons are pressed
if (keypadStatus != 0) // If a button is pressed go into here
{
Serial.println("A button was pressed");
sendKeyPress(keypadStatus); // send the button over USB
timeout = 2000; // top of the repeat delay
while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down
delayMicroseconds(1);
while (getKeypadStatus() == keypadStatus) // while the same button is held down
{
sendKeyPress(keypadStatus); // continue to send the button over USB
delay(50); // 50ms repeat rate
}
}
}
/* sendKeyPress(int key): This function sends a single key over USB
It requires an int, of which the 12 LSbs are used. Each bit in
key represents a single button on the keypad.
This function will only send a key press if a single button
is being pressed */
void sendKeyPress(int key)
{
switch(key)
{
case 1: // 0x001
LCD.clrScr();
LCD.print("1",CENTER,14);
LCD.update();
Serial.print('1'); // Sends a keyboard '1'
break;
case 2: // 0x002
LCD.clrScr();
LCD.print("2",CENTER,14);
LCD.update();
Serial.print('2');
break;
case 4: // 0x004
LCD.clrScr();
LCD.print("3",CENTER,14);
LCD.update();
Serial.print('3');
break;
case 8: // 0x008
LCD.clrScr();
LCD.print("4",CENTER,14);
LCD.update();
Serial.print('4');
break;
case 16: // 0x010
LCD.clrScr();
LCD.print("5",CENTER,14);
LCD.update();
Serial.print('5');
break;
case 32: // 0x020
LCD.clrScr();
LCD.print("6",CENTER,14);
LCD.update();
Serial.print('6');
break;
case 64: // 0x040
LCD.clrScr();
LCD.print("7",CENTER,14);
LCD.update();
Serial.print('7');
break;
case 128: // 0x080
LCD.clrScr();
LCD.print("8",CENTER,14);
LCD.update();
Serial.print('8');
break;
case 256: // 0x100
LCD.clrScr();
LCD.print("9",CENTER,14);
LCD.update();
Serial.print('9');
break;
case 512: // 0x200
LCD.clrScr();
LCD.print("Star",CENTER,14);
LCD.update();
Serial.print('*');
break;
case 1024: // 0x400
LCD.clrScr();
LCD.print("0",CENTER,14);
LCD.update();
Serial.print('0'); // Sends a keyboard '0'
break;
case 2048: // 0x800
LCD.clrScr();
LCD.print("Raute",CENTER,14);
LCD.update();
Serial.print('#'); // Sends the 'ENTER' key
break;
}
}
/* getKeypadStatus(): This function returns an int that represents
the status of the 12-button keypad. Only the 12 LSb's of the return
value hold any significange. Each bit represents the status of a single
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ...,
'#' is bit 11.
This function doesn't work for multitouch.
*/
int getKeypadStatus()
{
uint8_t rowPins[6] = {keypadPins[5], keypadPins[0], keypadPins[1], keypadPins[2]}; // R1 = KeypadPin[5], R2 = ...
uint8_t columnPins[3] = {keypadPins[4], keypadPins[6], keypadPins[2]}; // C1 = KeypadPin[4], C2 = ...
int keypadStatus = 0; // this will be what's returned
/* initialize all pins, inputs w/ pull-ups */
for (uint8_t i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT);
digitalWrite(keypadPins[i], HIGH);
}
for (uint8_t row=0; row<4; row++)
{ // initial for loop to check all 4 rows
pinMode(rowPins[row], OUTPUT); // set the row pin as an output
digitalWrite(rowPins[row], LOW); // pull the row pins low
for (int col=0; col<3; col++)
{ // embedded for loop to check all 3 columns of each row
if (!digitalRead(columnPins[col]))
{
keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value
Serial.print("keypadStatus: ");Serial.println(keypadStatus);
}
}
pinMode(rowPins[row], INPUT); // reset the row pin as an input
digitalWrite(rowPins[row], HIGH); // pull the row pin high
}
return keypadStatus;
}
为了检查按下的数字是否返回正确的值,我通过串行通信将其值打印到 arduino 控制台并将其显示在我的 LCD 上。
我的问题是,当我上传代码并且 µcontroller 启动时 运行 我从我的键盘上得到了很多输入而没有触及任何东西,在 getKeypadStatus() 中计算的值是 2048",它转换为“#”按钮。我试图在计算时显示值 keypadStatus,结果我通过串行通信获得了大量的“2048”。
此外,我在 arduino 的引脚 4 处测量了 5V。我换了一些电线来检查问题是在键盘上还是在 arduino 上。当切换电线时,我仍然在 arduino 的引脚 4 上测量到 5V,但是打印的值(keypadStatus)仍然是“2048”。
键盘本身不可能有问题,因为我测量了每个按钮的电阻,它工作正常。
有人有意见吗?
最好使用 Mark Stanley 和 Alexander Brevig 的 Keypad 库。该库负责设置引脚并轮询不同的列和行。要安装 Keypad 库,请转到 Sketch > Include Library > Manage Libraries 并搜索“keypad”。单击库,然后单击安装。 3x4小键盘的代码如下
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {7, 2, 3, 5};
byte colPins[COLS] = {6, 8, 4};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}
}
我正在尝试将 keypad 集成到我的项目中,并想先在一个简单的程序中对其进行测试。 键盘像这样连接到 Arduino Uno (ATmega328P):
C1 -> 引脚 6
C2 -> 引脚 8
C3 -> 引脚 4
R1 -> 引脚 7
R2 -> 引脚 2
R3 -> 引脚 3
R4 -> 引脚 5
我下载了这个键盘的模板程序,并相应地调整了我的引脚配置。
代码如下:
#include <LCD5110_Graph.h>
/* keyPadHiduino Example Code
by: Jim Lindblom
date: January 5, 2012
license: MIT license. If you find this code useful, please
feel free to use this code however you'd like, commercially
or otherwise. Just keep this license on there whatever you do.
This code implements a 12-key USB keypad. You can type 0-9,
* is the + sign and the # key is enter. I'm using SparkFun's
12-button keypad, your pinouts may vary. Multi-touch is
not supported.
SparkFun Keypad Pinout:
Rows and columns are connected as such:
-------------
R1 | 1 | 2 | 3 | - 7 (grün)
R2 | 4 | 5 | 6 | - 2 (braun)
R3 | 7 | 8 | 9 | - 3 (schwarz)
R4 | * | 0 | # | - 5 (violett)
-------------
|C1 |C2 |C3
6(blau) 8(gelb) 4(weiß)
*/
// Pins 1-7 of the keypad connected to the Arduino respectively:
uint8_t keypadPins[7] = {2, 3, 4, 5, 6, 7, 8};
int keypadStatus; // Used to monitor which buttons are pressed.
int timeout; // timeout variable used in loop
LCD5110 LCD(9, 10, 11, 12, 13);
extern uint8_t SmallFont[]; //charactersize 6x8 pixels
extern uint8_t TinyFont[]; //charactersize 4x6 pixels
extern uint8_t MediumNumbers[]; //charactersize 12x16 pixels
void setup()
{
LCD.InitLCD();
LCD.setFont(TinyFont);
for (uint8_t i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT); // Set all keypad pins as inputs
digitalWrite(keypadPins[i], HIGH); // pull all keypad pins high
}
keypadStatus = 0;
Serial.begin(9600);
}
void loop()
{
keypadStatus = getKeypadStatus(); // read which buttons are pressed
if (keypadStatus != 0) // If a button is pressed go into here
{
Serial.println("A button was pressed");
sendKeyPress(keypadStatus); // send the button over USB
timeout = 2000; // top of the repeat delay
while ((getKeypadStatus() == keypadStatus) && (--timeout)) // Decrement timeout and check if key is being held down
delayMicroseconds(1);
while (getKeypadStatus() == keypadStatus) // while the same button is held down
{
sendKeyPress(keypadStatus); // continue to send the button over USB
delay(50); // 50ms repeat rate
}
}
}
/* sendKeyPress(int key): This function sends a single key over USB
It requires an int, of which the 12 LSbs are used. Each bit in
key represents a single button on the keypad.
This function will only send a key press if a single button
is being pressed */
void sendKeyPress(int key)
{
switch(key)
{
case 1: // 0x001
LCD.clrScr();
LCD.print("1",CENTER,14);
LCD.update();
Serial.print('1'); // Sends a keyboard '1'
break;
case 2: // 0x002
LCD.clrScr();
LCD.print("2",CENTER,14);
LCD.update();
Serial.print('2');
break;
case 4: // 0x004
LCD.clrScr();
LCD.print("3",CENTER,14);
LCD.update();
Serial.print('3');
break;
case 8: // 0x008
LCD.clrScr();
LCD.print("4",CENTER,14);
LCD.update();
Serial.print('4');
break;
case 16: // 0x010
LCD.clrScr();
LCD.print("5",CENTER,14);
LCD.update();
Serial.print('5');
break;
case 32: // 0x020
LCD.clrScr();
LCD.print("6",CENTER,14);
LCD.update();
Serial.print('6');
break;
case 64: // 0x040
LCD.clrScr();
LCD.print("7",CENTER,14);
LCD.update();
Serial.print('7');
break;
case 128: // 0x080
LCD.clrScr();
LCD.print("8",CENTER,14);
LCD.update();
Serial.print('8');
break;
case 256: // 0x100
LCD.clrScr();
LCD.print("9",CENTER,14);
LCD.update();
Serial.print('9');
break;
case 512: // 0x200
LCD.clrScr();
LCD.print("Star",CENTER,14);
LCD.update();
Serial.print('*');
break;
case 1024: // 0x400
LCD.clrScr();
LCD.print("0",CENTER,14);
LCD.update();
Serial.print('0'); // Sends a keyboard '0'
break;
case 2048: // 0x800
LCD.clrScr();
LCD.print("Raute",CENTER,14);
LCD.update();
Serial.print('#'); // Sends the 'ENTER' key
break;
}
}
/* getKeypadStatus(): This function returns an int that represents
the status of the 12-button keypad. Only the 12 LSb's of the return
value hold any significange. Each bit represents the status of a single
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ...,
'#' is bit 11.
This function doesn't work for multitouch.
*/
int getKeypadStatus()
{
uint8_t rowPins[6] = {keypadPins[5], keypadPins[0], keypadPins[1], keypadPins[2]}; // R1 = KeypadPin[5], R2 = ...
uint8_t columnPins[3] = {keypadPins[4], keypadPins[6], keypadPins[2]}; // C1 = KeypadPin[4], C2 = ...
int keypadStatus = 0; // this will be what's returned
/* initialize all pins, inputs w/ pull-ups */
for (uint8_t i=0; i<7; i++)
{
pinMode(keypadPins[i], INPUT);
digitalWrite(keypadPins[i], HIGH);
}
for (uint8_t row=0; row<4; row++)
{ // initial for loop to check all 4 rows
pinMode(rowPins[row], OUTPUT); // set the row pin as an output
digitalWrite(rowPins[row], LOW); // pull the row pins low
for (int col=0; col<3; col++)
{ // embedded for loop to check all 3 columns of each row
if (!digitalRead(columnPins[col]))
{
keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4); // set the status bit of the keypad return value
Serial.print("keypadStatus: ");Serial.println(keypadStatus);
}
}
pinMode(rowPins[row], INPUT); // reset the row pin as an input
digitalWrite(rowPins[row], HIGH); // pull the row pin high
}
return keypadStatus;
}
为了检查按下的数字是否返回正确的值,我通过串行通信将其值打印到 arduino 控制台并将其显示在我的 LCD 上。
我的问题是,当我上传代码并且 µcontroller 启动时 运行 我从我的键盘上得到了很多输入而没有触及任何东西,在 getKeypadStatus() 中计算的值是 2048",它转换为“#”按钮。我试图在计算时显示值 keypadStatus,结果我通过串行通信获得了大量的“2048”。
此外,我在 arduino 的引脚 4 处测量了 5V。我换了一些电线来检查问题是在键盘上还是在 arduino 上。当切换电线时,我仍然在 arduino 的引脚 4 上测量到 5V,但是打印的值(keypadStatus)仍然是“2048”。
键盘本身不可能有问题,因为我测量了每个按钮的电阻,它工作正常。
有人有意见吗?
最好使用 Mark Stanley 和 Alexander Brevig 的 Keypad 库。该库负责设置引脚并轮询不同的列和行。要安装 Keypad 库,请转到 Sketch > Include Library > Manage Libraries 并搜索“keypad”。单击库,然后单击安装。 3x4小键盘的代码如下
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {7, 2, 3, 5};
byte colPins[COLS] = {6, 8, 4};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}
}