首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过键盘将文本写到液晶屏幕上,然后记住文本以供比较

如何通过键盘将文本写到液晶屏幕上,然后记住文本以供比较
EN

Stack Overflow用户
提问于 2022-10-09 10:53:41
回答 1查看 86关注 0票数 -1

我在使用16x2 I2C液晶显示屏时遇到了问题。

下面是我需要做的步骤:1.将一个数字值通过键盘输入到屏幕的顶部。此数值不应超过3位数。2.当*键被按一次时,键盘应该是活动的(和屏幕可编辑的)。3.当按一次#键时,键盘应再次处于不活动状态。4.无论在屏幕上放置了什么值,都应该记住,因此,当传感器运行时,它应该能够将其值与屏幕值进行比较,从而实现核心进程停止的平等。

下面是我的代码,这不是我的工作。有人能提出解决办法吗?谢谢。

代码语言:javascript
复制
#include <Keypad.h>
#include <LiquidCrystal_I2C.h> 
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 4); 

#include<stdio.h>
const int ROW_NUM = 4; 
const int COLUMN_NUM = 3; 
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'}, 
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5}; 
byte pin_column[COLUMN_NUM] = {4,3,2}; 
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

int counter = 0;

void setup() {
  // put your setup code here, to run once:
lcd.init(); 
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(0, 0); 
  lcd.print("Volume.        ml");
   lcd.setCursor(0, 1); 
   lcd.print("Filled        ml");
  

}


void loop() {
  // put your main code here, to run repeatedly:
  keypadfunction();

}

void keypadfunction()
{
  char key = keypad.getKey();

  if (key)
  {
    
  }
  if (key == '*')
  {
    while (key != '#' || counter <=3)
    {
      lcd.setCursor(8, 1);
      lcd.setCursor(8+counter, 1); 
      lcd.print(key);
      counter = counter+1;
    }
  }
}

@C1sc0建议后的更改:

代码语言:javascript
复制
#include <Keypad.h>
#include <LiquidCrystal_I2C.h> 
#include <Wire.h>
 
LiquidCrystal_I2C lcd(0x27, 16, 4); 

#include<stdio.h>
const int ROW_NUM = 4; 
const int COLUMN_NUM = 3; 
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte pin_rows[ROW_NUM] = {8,7,6,5}; 
byte pin_column[COLUMN_NUM] = {4,3,2}; 
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, 
ROW_NUM, COLUMN_NUM );

char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;


void setup() 
{
  // put your setup code here, to run once:
lcd.init(); 
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(0, 0); 
  lcd.print("Volume        ml");
   lcd.setCursor(0, 1); 
   lcd.print("Filled        ml");


}


void loop() 
{
char key = keypad.getKey();

  if(key) // check if any key was pressed
  {
    if(key == '*') // if * was pressed switch to edit mode
    {
        lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
        lcd.clear();
        lcd.print("Enter volume: ");
        edit = true;
        lcd.setCursor(0,1); // set your cursor to second row 
        volumeCount = 0;
        volume[0] = '0';
        volume[1] = '0';
        volume[2] = '0';
    }

    if(edit && volumeCount < 3) // enter edit mode
    {
        volume[volumeCount] = key; // save key to a char array
        lcd.setCursor(volumeCount,1);  // set your cursor to the 
next position
        lcd.print(volume[volumeCount]); // print the pressed button to lcd
        volumeCount++; // increment the array index (cursor position)
    }

    if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
    {
        edit = false; // disable edit mode
        volumeCount = 0; // reset your counter
    lcd.setCursor(0,0);
    lcd.clear();//new
    lcd.print(volume); 
    finalValue = atoi(volume); // save your entered value for further usage
    //volume[0] = '0';
    //volume[1] = '0';
    //volume[2] = '0';
    }
  }

  buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 == HIGH)
    {
     //do process basis 'finalValue'
    }


}

屏幕输出

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-09 19:24:57

创建bool (或字节)变量,以发出“编辑”状态的信号

if (key == '*') { edit= true; }

在循环中(这将是您的第一个if条件)。

创建另一个if,在这里检查编辑标志(如果为真,则等待这3个值。您可以读取char数组的按下键。

char values[3]; //在安装程序()之前创建此变量

在这里,对于索引,您可以使用一个counter (正如我看到的,它已经存在于您的代码中)。因此,将所有这些行放在第二个if条件中,并在这里将计数器增加一个。您也可以在这里显示输入的字符:

代码语言:javascript
复制
lcd.setCursor(8+counter, 1);
lcd.print(key);

创建第三个if块,并检查计数器是# key or counter == 3。如果满足这个条件,您必须做三件事,将计数器重置为零,将编辑状态重置为false,最后将值转换为int (带有atoi函数)。

工作之后,您可以重构代码,使其更有效/更易读。

你需要这样的东西:

代码语言:javascript
复制
char volume[3];
byte volumeCount = 0;
bool edit = 0;
int finalValue = 0;
void loop() 
{

  
  
  char key = keypad.getKey();
  
  if(key) // check if any key was pressed
  {
    if(key == '*') // if * was pressed switch to edit mode
    {
        lcd.setCursor(0,0); // set your cursor at columnt 0, row 0
        lcd.print("Enter volume: ");
        edit = true;
        lcd.setCursor(0,1); // set your cursor to second row 
        volumeCount = 0;
        volume[0] = '0'; 
        volume[1] = '0'; 
        volume[2] = '0';
    }
    
    if(edit && volumeCount < 3 && key != '*' && key != '#')
    {
        volume[volumeCount] = key; // save key to a char array
        lcd.setCursor(volumeCount,1);  // set your cursor to the next position
        lcd.print(volume[volumeCount]); // print the pressed button to lcd
        volumeCount++; // increment the array index (cursor position)
    }
    
    if(volumeCount == 3 || key == '#') // array.length == 3 OR you pressed #
    {
        edit = false; // disable edit mode
        volumeCount = 0; // reset your counter
        finalValue = atoi(volume); // save your entered value for further usage
        volume[0] = '0'; 
        volume[1] = '0'; 
        volume[2] = '0'; # ty for the fix!!!
    }
  }
  
  
  
  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74004109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档