2017-10-31 27 views
-1

私はエスケープルームパズルのためのarduinoプロジェクトに取り組んできました。それは本質的には液晶スクリーン上のタイマー付き爆弾です。爆弾のすべての要素が解決された後、タイマが停止し、爆弾が鎮圧されたことが液晶画面に表示され、グループに次のパズルへの手掛かりが与えられます。 10回中9回完全に動作します。しかし、しばらくの間、爆弾が鎮圧されていると表示されると、液晶画面にはランダムな壊れた文字が表示されます。私は問題を診断する運がなかった。ここに誰かが欲しいと思うかもしれません。ランダムな時間に壊れた文字を表示するArduino液晶画面

#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(); 
    } 
    } 
} 

答えて

0

これは、お使いのLCDの回路に電気的な問題がある可能性が高いです。あなたのようなシンプルなコード(何らかの方法であなたを侮辱しようとしていない)が9/10回動作するという事実は、おそらくコードに何も間違いがないことを意味します(私は何も見ていません)。

すぐに確認するには、IDEを再インストールしてみてください。 IDEでダウンロードしたArduinoライブラリが更新される可能性があります。私はそれが問題を解決するだろうとは思っていませんが、それは試してみて簡単に簡単な方法です

私はすべてを切断し、それらを再接続することをお勧めします。それがうまくいかない場合は、LCDを戻して自分自身を新しいものにしてください。

0

16x2 LCDにシールドされていない1mリボンケーブルを使用した場合、同じ問題が発生しました。 MCUを搭載したメインボードをディスプレイからさらに離して配置する必要がありました。それは電磁干渉のランダムな性質を示した。例えば、蛍光灯を点灯させ、電動ドライバを始動させ、リレースイッチなどを作動させます。リボンケーブルを短くすることで問題は解決しました。 とにかく、LCDは電磁干渉に敏感です。これらの情報源から遠ざけてください。

関連する問題