Arduino键盘破坏整个代码(闹钟)
我目前正在用4x4键盘、i2c 20x4 Lcd、DS3231和蜂鸣器为闹钟编程。 一切正常,但我在执行菜单以更改报警时间和其他设置时遇到问题 可悲的是,我有时间压力,所以这对我来说非常重要。任何帮助都会得到认可 菜单显示,当我按下一个键时,它返回到显示时钟的正常屏幕。我尝试使用串行调试,但在此处中断(示意图): 这是全部代码(没有EEPROM的东西)Arduino键盘破坏整个代码(闹钟),arduino,i2c,Arduino,I2c,我目前正在用4x4键盘、i2c 20x4 Lcd、DS3231和蜂鸣器为闹钟编程。 一切正常,但我在执行菜单以更改报警时间和其他设置时遇到问题 可悲的是,我有时间压力,所以这对我来说非常重要。任何帮助都会得到认可 菜单显示,当我按下一个键时,它返回到显示时钟的正常屏幕。我尝试使用串行调试,但在此处中断(示意图): 这是全部代码(没有EEPROM的东西) #包括 #包括//I2C地址 #包括//用于I2C LCD显示器的库 #包括//键盘库 #包括DS3231 RTC的“RTClib.h”//Li
#包括
#包括//I2C地址
#包括//用于I2C LCD显示器的库
#包括//键盘库
#包括DS3231 RTC的“RTClib.h”//Library
//键盘的定义是什么
常量字节COLS=4//4垃圾
常量字节行=4//4泽伦
//小键盘的Ziffern和Zeichen werden eingegeben:
字符六键[行][COLS]={
{'1', '4', '7', '*'},
{'2', '5', '8', '0'},
{'3', '6', '9', '#'},
{'A','B','C','D'}
};
RTC_DS3231 RTC;//申报RTC
液晶I2C液晶显示器(0x27,2,1,0,4,5,6,7,3,正极);//声明LCD模块(I2C地址、RS、E、D4、D5、D6、D7、背光管脚、极性)
现在是约会时间;
字节colPins[COLS]={2,3,4,5}//引脚für模具4 Spalten的定义
字节rowPins[行]={6,7,8,9}//4号泽伦管脚的定义
字符键//味觉是一种可变的味觉。
char-keypressx//味觉警报abzubrechen
字符按键//梅努基
小键盘Tastenfeld=小键盘(makeyMap(六键)、行pin、列pin、行、列)//这是一个绝对的数字,来自于“Tastenfeld”angesprochen werden
小键盘Tastenfeld2=小键盘(makeyMap(六键)、行pin、列pin、行、列)//这是一个绝对的数字键盘,它的名字叫“Tastenfeld2”
int menupos=-1;
布尔currentStateBacklight=true;
布尔scheduledBacklight=false;
int bs__________________________;;
国际业余时间;
常数int报警引脚=10;
布尔endedAlarm=false;
国际农业时间;
布尔报警1_active=false;
布尔报警2_active=false;
字符串报警1_小时;
字符串报警1分钟;
字符串报警1_秒;
字符串alarm1Days[]={};
字符串报警2_小时;
字符串报警2_分钟;
字符串报警2_秒;
字符串alarm2Days[]={};
char daysOfTheWeek[7][12]={“Sonntag”、“Montag”、“Dienstag”、“Mittwoch”、“Donnerstag”、“Freitag”、“Samstag”};
无效设置(){
序列号:开始(250000);
延迟(3000);//等待控制台打开
如果(!rtc.begin()){
Serial.println(“找不到RTC”);
而(1),;
}
if(rtc.lostPower()){
println(“RTC断电,让我们设置时间!”);
//设置日期和时间后,请在下面的行中进行注释。
//以下行将RTC设置为编译此草图的日期和时间
rtc.调整(日期时间(F(uu日期),F(u时间));
}
lcd.立根灯(高);
begin(20,4);//设置lcd的列数和行数
Wire.begin();
}
void循环(){
键=无键;
按键X=无按键;
按键M=无按键;
while(key==NO_key){
key=Tastenfeld.getKey();
DateTime now=rtc.now();
如果(endedAlarm){
如果((毫秒()-endedAlarmTime)>60000){
endedAlarm=假;
endedAlarmTime=0;
}
}
字符串nowhour=correctedValue(now.hour());
字符串nowmin=correctedValue(now.minute());
字符串nowsec=correctedValue(now.second());
字符串nowday=daysOfTheWeek[now.daysofweek()];
如果(alarm1_active&&nowhour==alarm1_hour&&nowmin==alarm1_minute&&endedAlarm&&arrayIncludeElement(alarm1Days,nowday)){/&correctedValue(now.second())==alarm1_second){
lcd.clear();
lcd.setCursor(1,10);
lcd.print(“起床!!!”;//闹钟响起时显示的消息
int z=0;
int x=1000;
int y=430;
while(按键X==无按键){
音调(报警针,2870);
keypressx=Tastenfeld.getKey();
延迟(y);
noTone(报警针);
keypressx=Tastenfeld.getKey();
延迟(x);
x=x*0.95999;
y=y*0.97999;
keypressx=Tastenfeld.getKey();
}
endedAlarm=真;
endedAlarmTime=millis();
lcd.clear();
}
如果(alarm2_active&&nowhour==alarm2_hour&&nowmin==alarm2_minute&&endedAlarm&&arrayIncludeElement(alarm2Days,nowday)){
lcd.clear();
lcd.setCursor(1,10);
lcd.print(“起床!!!”;//闹钟响起时显示的消息
int z=0;
int x=1000;
int y=430;
while(按键X==无按键){
音调(报警针,2870);
keypressx=Tastenfeld.getKey();
延迟(y);
noTone(报警针);
keypressx=Tastenfeld.getKey();
延迟(x);
x=x*0.95999;
y=y*0.97999;
keypressx=Tastenfeld.getKey();
}
endedAlarm=真;
endedAlarmTime=millis();
lcd.clear();
}
按键X=无按键;
noTone(报警针);
now=rtc.now();
lcd.setCursor(0,0);//文本soll beim ersten Zeichen在ersten Reihe beginnen中。。
lcd.print(“”;//在文本“测试文件1”中显示
lcd.setCursor(18,0);
如果(报警1_活动==真){
lcd.print(“A
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
loop();
}
lcd.clear();
}
#include <TimedAction.h>
#include <Wire.h> // I2C Adress
#include <LiquidCrystal_I2C.h> // Library for I2C LCD Displays
#include <Keypad.h> // Keypad Libary
#include "RTClib.h" // Library for DS3231 RTC
//Hier wird die größe des Keypads definiert
const byte COLS = 4; //4 Spalten
const byte ROWS = 4; //4 Zeilen
//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS] = {
{'1', '4', '7', '*'},
{'2', '5', '8', '0'},
{'3', '6', '9', '#'},
{'A', 'B', 'C', 'D'}
};
RTC_DS3231 rtc; // declare RTC
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // declare LCD module (I2C adress, RS, E, D4, D5, D6, D7, backlightpin, polarity)
DateTime now;
byte colPins[COLS] = {2, 3, 4, 5}; //Definition der Pins für die 4 Spalten
byte rowPins[ROWS] = {6, 7, 8, 9}; //Definition der Pins für die 4 Zeilen
char key; //Taste ist die Variable für die jeweils gedrückte Taste.
char keypressx; //Taste um alarm abzubrechen
char keypressm; //menukeyw
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden
Keypad Tastenfeld2 = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld2" angesprochen werden
int menupos = -1;
boolean currentStateBacklight = true;
boolean scheduledBacklight = false;
int bs_ontime;
int bs_offtime;
const int alarm_pin = 10;
boolean endedAlarm = false;
int endedAlarmTime;
boolean alarm1_active = false;
boolean alarm2_active = false;
String alarm1_hour;
String alarm1_minute;
String alarm1_second;
String alarm1Days[] = {};
String alarm2_hour;
String alarm2_minute;
String alarm2_second;
String alarm2Days[] = {};
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
void setup() {
Serial.begin(250000);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.setBacklight(HIGH);
lcd.begin(20, 4); // set up the LCD's number of columns and rows
Wire.begin();
}
void loop() {
key = NO_KEY;
keypressx = NO_KEY;
keypressm = NO_KEY;
while (key == NO_KEY) {
key = Tastenfeld.getKey();
DateTime now = rtc.now();
if (endedAlarm) {
if ((millis() - endedAlarmTime) > 60000) {
endedAlarm = false;
endedAlarmTime = 0;
}
}
String nowhour = correctedValue(now.hour());
String nowmin = correctedValue(now.minute());
String nowsec = correctedValue(now.second());
String nowday = daysOfTheWeek[now.dayOfTheWeek()];
if (alarm1_active && nowhour == alarm1_hour && nowmin == alarm1_minute && !endedAlarm && arrayIncludeElement(alarm1Days, nowday)) { // && correctedValue(now.second()) == alarm1_second) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
if (alarm2_active && nowhour == alarm2_hour && nowmin == alarm2_minute && !endedAlarm && arrayIncludeElement(alarm2Days, nowday)) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
keypressx = NO_KEY;
noTone(alarm_pin);
now = rtc.now();
lcd.setCursor(0, 0); //Text soll beim ersten Zeichen in der ersten Reihe beginnen..
lcd.print(""); //In der ersten Zeile soll der Text „Test Zeile 1“ angezeigt werden
lcd.setCursor(18, 0);
if (alarm1_active == true) {
lcd.print("A1");
}
lcd.setCursor(6, 1); //Genauso geht es bei den weiteren drei Zeilen weiter
lcd.print(correctedValue(now.hour()) + ":" + correctedValue(now.minute()) + ":" + correctedValue(now.second()));
lcd.setCursor(18, 1);
if (alarm2_active == true) {
lcd.print("A2");
}
lcd.setCursor(7, 2);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 3);
lcd.print(correctedValue(now.day()) + "." + correctedValue(now.month()) + "." + String(now.year()));
delay(100);
}
Serial.println(key);
if (key == '1') {
if (alarm1_active) {
alarm1_active = false;
lcd.clear();
} else {
alarm1_active = true;
}
}
if (key == '2') {
if (alarm2_active) {
alarm2_active = false;
lcd.clear();
} else {
alarm2_active = true;
}
}
if (key == '*') {
menu(0);
}
}
boolean arrayIncludeElement(String array[], String element) {
for (int i = 0; i < 7; i++) {
if (array[i] == element) {
return true;
}
}
return false;
}
String correctedValue(char x) {
int y;
String z;
y = int(x);
if (y < 10) {
z = String(y);
z = "0" + z;
return z;
} else {
return String(y);
}
}
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
key = "*";
}
if (keypressm == "1") {
//menupos = 1;
menuSetAlarm();
//menupos = 0;
} else if (keypressm == "2") {
//menupos = 2;
menuSetLanguage();
//menupos = 0;
} else if (keypressm == "3") {
//menupos = 3;
menuOtherSettings();
//menupos = 0;
} else if (keypressm == "9") {
lcd.clear();
loop();
//menupos = -1;
}
lcd.clear();
}
void menuSetAlarm() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarm 1");
lcd.setCursor(0, 1);
lcd.print("2. Alarm 2");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
} else if (keypressm == "2") {
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
menu(1);
lcd.clear();
}
void menuSetLanguage() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Deutsch");
lcd.setCursor(0, 1);
lcd.print("2. English");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
changeLanguage("en");
} else if (keypressm == "2") {
changeLanguage("de");
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menu(2);
}
void menuOtherSettings() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Permanent " + !currentStateBacklight);
lcd.setCursor(0, 1);
lcd.print("2. Geplant");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
currentStateBacklight = !currentStateBacklight;
if (currentStateBacklight) {
lcd.setBacklight(HIGH);
} else {
lcd.setBacklight(LOW);
}
} else if (keypressm == "2") {
scheduledBacklight = true;
int x;
int y;
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_ontime = x;
} else if (keypressm == "4") {
bs_ontime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_offtime = x;
} else if (keypressm == "4") {
bs_offtime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
lcd.clear();
}
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menupos = 1;
}
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
return; // !!
}
lcd.clear();
}