Arduino lcd屏幕在随机时间显示中断字符
所以我一直在做一个arduino项目,为一个逃生室拼图。它本质上是一个在lcd屏幕上有定时器的炸弹。当炸弹的所有元素都被解决后,计时器停止,lcd屏幕显示炸弹已被拆除,并为小组提供下一个谜题的线索。10次中有9次它工作得很好。但每隔一段时间,当它应该显示炸弹已被拆除时,lcd屏幕就会显示随机中断的字符。我没法诊断出这个问题。希望这里的人能有个主意Arduino lcd屏幕在随机时间显示中断字符,arduino,Arduino,所以我一直在做一个arduino项目,为一个逃生室拼图。它本质上是一个在lcd屏幕上有定时器的炸弹。当炸弹的所有元素都被解决后,计时器停止,lcd屏幕显示炸弹已被拆除,并为小组提供下一个谜题的线索。10次中有9次它工作得很好。但每隔一段时间,当它应该显示炸弹已被拆除时,lcd屏幕就会显示随机中断的字符。我没法诊断出这个问题。希望这里的人能有个主意 #include <Keypad.h> #include <LiquidCrystal.h> #include <Ton
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&@$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
#包括
#包括
#包括
#定义14磅
音调1;
int-scont=0;
int Mcount=0;
int Hcount=1;
int DefuseTimer=0;
长secMillis=0;
长间隔=1000;
字符密码[6]=“594432”;
int currentLength=0;
int i=0;
输入字符[6];
int-ledPin=23;
int-ledPin2=25;
int-ledPin3=27;
int-ledPin4=29;
int-ledPin5=31;
int-ledPin6=34;
常量int plugin1=44;
常量int plugin2=46;
常量int plugin3=48;
常量int plugin4=50;
常量int plugin5=52;
int-plugin1State=0;
int-plugin2State=0;
int-plugin3State=0;
int-plugin4State=0;
int-plugin5State=0;
常数int开关1=37;
int switch1State=0;
常数int key1=40;
int key1State=0;
int=1=0;
int=2=0;
int=3=0;
int=0;
液晶显示器(7,8,10,11,12,13);
常量字节行=4;
常量字节COLS=3;
字符键[行][列]={
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
字节rowPins[行]={A0,A1,A2,A3};
字节colPins[COLS]={A4,A5,A6};
小键盘=小键盘(makeyMap(键)、行PIN、列PIN、行、列);
无效设置(){
引脚模式(LED引脚,输出);
引脚模式(LED引脚2,输出);
引脚模式(LED引脚3,输出);
引脚模式(LED引脚4,输出);
引脚模式(LED引脚5,输出);
引脚模式(LED引脚6,输出);
pinMode(插件1,输入);
pinMode(plugin2,输入);
pinMode(插件3,输入);
引脚模式(插件4,输入);
引脚模式(插件5,输入);
数字写入(plugin1,高);
数字写入(plugin2,高);
数字写入(插件3,高);
数字写入(插件4,高);
数字写入(插件5,高);
引脚模式(开关1,输入);
数字写入(开关1,高);
pinMode(按键1,输入\上拉);
数字写入(键1,高);
音调1.开始(9);
lcd.begin(16,2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0,0);
音调1.播放(注6,200);
延迟(3000);
lcd.clear();
currentLength=0;
}
void循环()
{
定时器();
plugin1State=digitalRead(plugin1);
plugin2State=digitalRead(plugin2);
plugin3State=digitalRead(plugin3);
plugin4State=digitalRead(plugin4);
plugin5State=digitalRead(plugin5);
如果(plugin1State==低&&plugin2State==低&&plugin3State==低&&plugin4State==低&&plugin5State==低)
{
1=1;
}
如果(1==1)
{
数码写入(发光二极管4,高);
开关1状态=数字读取(开关1);
如果(开关1状态==低)
{
2=1;
}
}
如果(2==1)
{
数字写入(ledPin5,高);
key1State=数字读取(key1);
如果(key1State==低)
{
3=1;
}
如果(key1State==高)
{
数字写入(ledPin6,低电平);
}
}
如果(3==1)
{
数字写入(ledPin6,高);
lcd.clear();
lcd.setCursor(0,0);
lcd.打印(“代码:”);
while(电流长度<6)
{
定时器();
char key2=keypad.getKey();
如果(键2==“#”)
{
currentLength=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.打印(“代码:”);
}
否则如果(键2!=无键)
{
lcd.setCursor(当前长度+7,0);
lcd.cursor();
lcd.打印(按键2);
输入的[currentLength]=键2;
currentLength++;
音调1.演奏(注6,200);
延迟(100);
noCursor();
lcd.setCursor(当前长度+6,0);
lcd.打印(“*”);
lcd.setCursor(当前长度+7,0);
lcd.cursor();
}
}
如果(currentLength==6)
{
如果(输入[0]==密码[0]&&T输入[1]==密码[1]&&T输入[2]==密码[2]&&T输入[3]==密码[3]&&T输入[4]==密码[4]&&T输入[5]==密码[5])
{
已解决=1;
while(已解决==1)
{
noCursor();
lcd.clear();
lcd.home();
lcd.打印(“炸弹1已拆除”);
currentLength=0;
数字写入(ledPin3,高);
延迟(1500);
noCursor();
lcd.clear();
lcd.home();
lcd.打印(“发布”);
延迟(1500);
noCursor();
lcd.clear();
lcd.home();
lcd.打印(“有毒气体”);
延迟(1500);
noCursor();
lcd.clear();
lcd.home();
lcd.print(“线索:%&@$#”);
延迟(6000);
}
}
其他的
{
noCursor();
lcd.clear();
lcd.home();
lcd.print(“密码错误!”);
延迟(1500);
currentLength=0;
}
}
}
}
无效计时器()
{
如果(Hcount=10)
{
lcd.setCursor(7,1);
lcd.print(Hcount);
}
如果(Hcount<10)
{
lcd.setCursor(7,1);
lcd.write(“0”);
lcd.setCursor(8,1);
lcd.print(Hcount);
}
lcd.print(“:”);
如果(Mcount>=10)
{
lcd.setCursor(10,1);
lcd.print(Mcount);
}
如果(Mcount<10)
{
lcd.setCursor(10,1);
lcd.write(“0”);
lcd.setCursor(11,1);
lcd.print(Mcount);
}
lcd.print(“:”);
如果(童子军>=10)
{
lcd.setCursor(13,1);
lcd.print(scont);
}
如果(童子军<10)
{
lcd.setCursor(13,1);
lcd.write(“0”);
lcd.setCursor(14,1);
lcd.print(scont);
}
如果(Hcount<0)
{
Hcount=0;
}
如果(Mcount<0)
{
Hcount--;
Mcount=59;
}
if(scont<1)//if 60执行此操作
{
Mcount--;//将1添加到Mcount
Scont=59;//重置Scont
}
如果(Scont>0)//执行此操作59