2017-10-31 1 views
-1

그래서 탈출 실 퍼즐을위한 arduino 프로젝트를 진행하고 있습니다. 그것은 기본적으로 LCD 화면에 타이머가있는 폭탄입니다. 폭탄의 모든 요소가 해결 된 후 타이머가 멈추고 LCD 화면에 폭탄이 제거되었음을 표시하고 그룹에게 다음 퍼즐에 대한 단서를 제공합니다. 10 회 중 9 회 완벽하게 작동합니다. 그러나 때로는 폭탄이 흩어져 있다는 것을 보여 주려고 할 때마다 액정 화면에는 무작위로 깨진 문자가 표시됩니다. 문제를 진단하는 행운이 없었습니다. 여기 누군가를 바라는 것은 생각이들 것입니다.임의의 시간에 깨진 문자를 표시하는 Arduino lcd 화면

#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는 전자기 간섭에 민감합니다. 이 소스에서 멀리 떨어져 보관하십시오.

관련 문제