#include <LiquidCrystal.h>
const int button_up = A0;//按键“↑”定义:A0
const int button_down = A1;//按键“↓”定义:A1
const int button_set = A2;//按键“功能”定义:A2
const int laba = 2;
const bool sleep = true;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);//显示器引脚定义
int init_time = 0;
int S_SETS[3][2] = {{1289, 21}, {25, 369}, {1256, 25}};
void setup() {
Serial.begin(9600);
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
pinMode(button_set, INPUT_PULLUP);
lcd.begin(16, 2);
}
void loop() {
welcome(); //显示初始欢迎文本
int btn_down_state = btn_down();
if (btn_down_state == 3) { //获取设置按钮状态并是否进入设置界面
for (int i = 1; i < 4; i++) { //设置距离初始值;前置检测测距个数,根据个数修改数组????
set_data(i);
}
}
show_running_data();
}
void welcome() { //显示初始欢迎文本
while (1) {
if (init_time < 3) {
lcd.setCursor(0, 0);
lcd.print(" Control System");
lcd.setCursor(0, 1);
lcd.print("By Jonathan_zfl!");
tone(laba, 2000, 600);
delay(1000);
lcd.clear();
if (init_time < 2)
{
delay(300);
}
init_time++;
return;
}
break;
}
}
int btn_down() {//检测按键 0:无按钮按下;1:按钮1按下;2:按钮2按下;3:按钮3按下
int state = 0;
if (digitalRead(button_up) == LOW) {
delay(10);
if (digitalRead(button_up) == LOW) {
tone(laba, 2500, 100);
state = 1;
while (!digitalRead(button_up));
}
}
if (digitalRead(button_down) == LOW) {
delay(10);
if (digitalRead(button_down) == LOW) {
tone(laba, 2500, 100);
state = 2;
while (!digitalRead(button_down));
}
}
if (digitalRead(button_set) == LOW) {
delay(10);
if (digitalRead(button_set) == LOW) {
tone(laba, 2500, 100);
state = 3;
while (!digitalRead(button_set));
}
}
return state;
}
String lcd_print(int i) {
}
void show_running_data() {
lcd.setCursor(0, 0);//获取设备运行状态信息并显示
lcd.print("S1:1236 S2:3653");
lcd.setCursor(0, 1);
lcd.print("S3:0028 MT:run~");
}
void set_data(int S) {
int cursor_arr[8] = {3, 4, 5, 6, 11, 12, 13, 14};
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("SET:S" + String(S));
String lo_str = String(S_SETS[S - 1][0]);
String hi_str = String(S_SETS[S - 1][1]);
int len_lo = lo_str.length();
int len_hi = hi_str.length();
if (len_lo < 4) {
for (int i = 0; i < 4 - len_lo; i++) {
lo_str = "0" + lo_str;
}
}
if (len_hi < 4) {
for (int j = 0; j < 4 - len_hi; j++) {
hi_str = "0" + hi_str;
}
}
lcd.setCursor(0, 1);
String re = "L:[" + lo_str + "]H:[" + hi_str + "]";
lcd.print(re);
lcd.cursor();
lcd.setCursor(cursor_arr[0], 1);
int i = 0;
while (1) {
int state = btn_down();
if (state == 1) {
if (i < 4) {
int s = (lo_str.substring(i, i + 1)).toInt() + 1;
if (s > 9) {
lo_str.setCharAt(i, '0');
} else {
lo_str.setCharAt(i, String(s)[0]);
}
lcd.write(lo_str[i]);
} else {
int j = i - 4;
int s = (hi_str.substring(j, j + 1)).toInt() + 1;
if (s > 9) {
hi_str.setCharAt(j, '0');
} else {
hi_str.setCharAt(j, String(s)[0]);
}
lcd.write(hi_str[j]);
}
lcd.noCursor();
lcd.setCursor(cursor_arr[i], 1);
lcd.cursor();
}
if (state == 2) {
if (i > 6) {
i = 0;
} else {
i = i + 1;
}
lcd.setCursor(cursor_arr[i], 1);
}
if (state == 3) {
S_SETS[S - 1][0] = lo_str.toInt();
S_SETS[S - 1][1] = hi_str.toInt();
Serial.println(S_SETS[S - 1][0]);
Serial.println(S_SETS[S - 1][1]);
lcd.noCursor();
break;
}
}
lcd.clear();
}