为什么函数的输出不遵循lcd.setCursor位置?键入时,输出会弹出不同的位置而不是确定的setcursor。我需要多次使用此函数用于为不同目的保存不同数字的不同变量。但是,我需要根据设定的位置在液晶显示屏上显示数字。
以下是代码:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the
LCD I2C address
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
lcd.begin(20,4);
}
void loop()
{
lcd.setCursor(2,0);
int stage1speed = getnumber();
lcd.setCursor(5,0);
lcd.print("sv");
lcd.setCursor(2,1);
int stage1time = getnumber();
lcd.setCursor(5,1);
lcd.print("sec");
lcd.setCursor(2,2);
int stage2speed = getnumber();
lcd.setCursor(5,2);
lcd.print("sec");
lcd.setCursor(2,3);
int stage2time = getnumber();
lcd.setCursor(5,3);
lcd.print("sec");
}
int getnumber()
{
static char buffer[4];
static byte i = 0;
char key = keypad.getKey();
// i < 3: prevent buffer overflow
if ('0' <= key && key <= '9' && i < 3)
{
buffer[i] = key;
++i;
} else if (key == '#' && i > 0)
{
buffer[i] = '\0'; // null-terminate buffer
int value = atoi(buffer);
lcd.print(buffer);
i = 0;
}
}