2016-04-03 18 views
1

私は、RFIDタグが読み込まれているかどうかを確認するプログラムを持っています。残りのプログラム実行を妨げるArduinoのカウントダウンタイマー

バックグラウンドで実行したいカウントダウンタイマー機能(タイマー)もあります。 LCDの下部に時計を表示します。

私が持っている問題は、timer();メインループでは、プログラムの残りの部分がカウントダウンの間遅延されます。つまり、RFIDカードが読み取られません。

タイマーは機能しますが、timer()の下には何もありません。が走っています。

誰も私がこれを防ぐ方法を知っていますか?ありがとうございました。これらの変数の値が両方decrement機能は、もはやそれらを変更しません、0に達すると

while (minutes > 0 || seconds >= 0) 

#include <SPI.h>  
#include <MFRC522.h> 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 


int readflag; 
int initstate = 1; 

int nextred = 4; 
int nextblue = 8; 

uint8_t readCard[4];   

int minutes = 1; //start min 
int seconds = 0; //start seconds 


MFRC522 mfrc522(10, 9); 
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 


///////////////////////////////////////// Setup /////////////////////////////////// 
void setup() { 

    //Protocol Configuration 
    Serial.begin(9600); 
    SPI.begin();   
    mfrc522.PCD_Init();  
    mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); 

    lcd.begin(20, 4); 


} 


/////////////////////////////////////////////////////////////////////////////// 
    // Main loop 
/////////////////////////////////////////////////////////////////////////////// 
void loop() { 

do {                
    readflag = checkread(); 
if (initstate == 0) { 
    timer(); //start timer - timer works but nothing below timer(); is running. 
    lcd.setCursor(0,0); 
    lcd.print("Red team next: "); 
    lcd.print(nextred); 
    lcd.setCursor(0,1); 
    lcd.print("Blue team next: "); 
    lcd.print(nextblue); 

} 
    } 
    while (!readflag); 
    tone(8, 850, 15); 
    recordid(); 
    updatenextteamnumbers(); 
} 


/////////////////////////////////////////////////////////////////////////////// 
    // Function to update team next numbers 
/////////////////////////////////////////////////////////////////////////////// 
void updatenextteamnumbers() { 
//If Blue Tag scanned show team info 
    if (*((uint32_t *)readCard) == 0xACB7D573) { 

    Serial.println("Blue Tag detected, showing team info"); 
    initstate=0; 

    } 


//Red Team (sequence 4, 5, 2, 1, 9) 
else if (*((uint32_t *)readCard) == 0x29FDC9F5 && initstate == 0) { 
    Serial.println("CARD 4 detected"); 
    nextred = 5; 

     } 

else if (*((uint32_t *)readCard) == 0x3FCE7832 && initstate == 0) { 

    Serial.println("CARD 5 detected"); 
    nextred = 2; 

} 

else if (*((uint32_t *)readCard) == 0x3FCCECE2 && initstate == 0) { 

    Serial.println("CARD 2 detected"); 
    nextred = 1; 

     } 


    else if (*((uint32_t *)readCard) == 0x3F7AB752 && initstate == 0) { 

    Serial.println("CARD 1 detected"); 
    nextred = 9; 

    //activateredrelay 

     } 


    else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) { 

    Serial.println("CARD 9 detected"); 
    redwins();  

     } 



//Blue Team (sequence 8, 6, 3, 7, 10) 

else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) { 
    Serial.println("CARD 8 detected"); 
    nextblue = 6; 

     } 

else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) { 

    Serial.println("CARD 6 detected"); 
    nextblue = 3; 

} 

else if (*((uint32_t *)readCard) == 0x3F7ABA82 && initstate == 0) { 

    Serial.println("CARD 3 detected"); 
    nextblue = 7; 

     } 


    else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) { 

    Serial.println("CARD 7 detected"); 
    nextblue = 10; 

    //activatebluerelay 

     } 


    else if (*((uint32_t *)readCard) == 0x00000000 && initstate == 0) { 

    Serial.println("CARD 10 detected"); 
    bluewins();  

     } 

} 


/////////////////////////////////////////////////////////////////////////////// 
    // Stores the ID of the card that's been detected in readCard byte array 
/////////////////////////////////////////////////////////////////////////////// 
void recordid() { 
    mfrc522.PICC_IsNewCardPresent(); 
    mfrc522.PICC_ReadCardSerial(); 

    for (int i = 0; i < mfrc522.uid.size; i++) { 
     readCard[i] = mfrc522.uid.uidByte[i]; 
     Serial.print(mfrc522.uid.uidByte[i], HEX); 
     } 
Serial.println(""); 
    mfrc522.PICC_HaltA(); 
    } 


///////////////////////////////////////////// 
    // Returns 1 if a card has been detected 
///////////////////////////////////////////// 
int checkread() { 
    if (! mfrc522.PICC_IsNewCardPresent()) { 
    return 0; }          //no card detected 

    mfrc522.PICC_HaltA(); 
    return 1; }          //card detected 


///////////////////////////////////////////// 
    // Runs if red team wins 
///////////////////////////////////////////// 
void redwins() { 
     lcd.clear(); 
     lcd.setCursor(0,0); 
     lcd.print("Red team wins!"); 
     lcd.display(); 
     Serial.println("Red team wins!"); 
     delay(36000); 
} 

///////////////////////////////////////////// 
    // Runs if blue team wins 
///////////////////////////////////////////// 
void bluewins() { 
     lcd.clear(); 
     lcd.setCursor(0,0); 
     lcd.print("Blue team wins!"); 
     lcd.display(); 
     Serial.println("Blue team wins!"); 
     delay(36000); 
} 


///////////////////////////////////////////// 
    // Count down timer 
///////////////////////////////////////////// 
void timer() { 
lcd.setCursor(0,3); 
    lcd.print("Time left:"); 
while (minutes > 0 || seconds >= 0) { 
    lcd.setCursor(15, 3); 
(minutes < 10) ? lcd.print("0") : NULL; 
lcd.print(minutes); 
lcd.print(":"); 
(seconds < 10) ? lcd.print("0") : NULL; 
lcd.print(seconds); 
lcd.display(); 
decrement(); 
delay(1000); 
} 
} 

///////////////////////////////////////////// 
    // Decrement timer 
///////////////////////////////////////////// 
void decrement() { 
     if (seconds > 0) { 
      seconds -= 1; 
     } 
     else  { 
     if (minutes > 0) { 
      seconds = 59; 
      minutes -= 1; 
      } 
      else { 
      timeup(); 
      } 
     } 
} 

///////////////////////////////////////////// 
    // Called when time expires 
///////////////////////////////////////////// 
void timeup() { 
     lcd.clear(); 
     lcd.setCursor(0,0); 
     lcd.print("Time up!"); 
     lcd.display(); 
} 
+1

http://arduino.stackexchange.com/でこれをお尋ねしてください。 http://stackoverflow.com/のアカウントを使ってこれらのコミュニティにリンクすることもできます。サイトに行き、右上の "このコミュニティに参加"をクリックします。 – jbm

答えて

0

ここでの喫煙銃は、次の行になりそうです。 seconds >= 0がまだtrueと評価され、無限ループが発生するため、これは問題です。

+0

こんにちは@ブライアン - カウンター機能はうまくいく - 私はそれがバックグラウンドで実行するために必要です。私はmillis()を使って実装しました(遅れを取り除いて、それは 'ブロック'を引き起こしていますが、残念ながらそれはまだ動作しません)。 – mh512

+0

@ mh512 - 私はあなたが私の元の答えを誤解しているかもしれないと思います。これはハングしますが、 'while(minutes> 0 || seconds> = 0)'行は0分、0秒で停止するため、常にtrueと評価されます。 – bcarrigan

関連する問題