私は長い間プログラミングをしていないので、ArduinoUNOボードを使って電子工学から拡張したいと思っています。
GrathioのSteveHoeferによるSecretKnockDetecting Door Lockに基づいた新しいプロジェクトを開始しました。次のことを実装したいと思います:
(http://grathio.com/2009/11/secret_knock_detecting_door_lock/)(http://grathio.com/assets/secret_knock_detector.pde)_ _
実装
グローバル値が0に等しく、有効なノックパターンがtrueの場合、遅延ではなくミリ秒を使用して黄色のLEDを4回点滅させ、「リッスン」できるようにします。
別の有効なノックパターンが6秒以内に聞こえない場合、タイムアウトしてグローバルを0にリセットし、最初の真のパターンを確認して黄色のLEDを点滅させることができます。
6秒以内に別の有効なノックパターンが聞こえた場合は、カウンターをインクリメントします。
カウンターが1に等しい場合は、別の有効なノックパターンを待ち、6秒以内に真の場合は、カウンターを再度インクリメントし、黄色のLEDを点滅させないでください。
それ以外の場合は、タイムアウトしてすべての値をリセットします。
カウンターが4以上になるまで、マスターLEDアレイをトリガーします。
ノックが4回成功したら、作成したマスターLEDアレイをトリガーします。
問題
このプロジェクトは、旅客機で使用されているテストパネルに触発されました。私はそれらをたくさん見て、タイミングについて学び始めるのに良い場所だと思いました。
毎回millis()をリセットしたくないので、いくつかの問題があります。ノック検出スクリプト内でブール値ではなくボタンを使用しているため、コードに迷うことはありません。
これは50秒後に応答しないことを理解しています。これは初心者の間違いですが、ボタンを押したままにすると、私が得たものを証明します。以下のコードも、最初のdigitalRead HIGHまたは真のブール値の後にタイムアウトがありません(私はこれに苦労しています)。
Arduinoスケッチ
int inPin = 2; // input pin switch
int outPin = 3; // output pin LED
long currentTime = 0; // counter
long nextTime = 0; // counter
long lastTime = 0; // counter
int patternCounter = 0; // build up
int globalValue = 0; // lock out
int breakIn = 0; // waste of time?
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
Serial.begin(9600);
Serial.println("GO");
}
void loop(){
// boolean true, switch just for testing
if (digitalRead(inPin)==HIGH&&globalValue==0&&breakIn==0) {
Serial.println("CLEARED 1st");
delay (500); // flood protection
globalValue++;
breakIn++;
if (globalValue>0&&breakIn>0){
currentTime = millis(); // start a 'new' counter and 'listen'
if (currentTime<6000) { // less than
if (digitalRead(inPin)==HIGH) { // and true
Serial.println("CLEARED 2nd"); // cleared the stage
delay (500); // flood protection
patternCounter++;
} // if counter less
} // if true or high
if (currentTime>6000) {
Serial.println("TIMEOUT waiting 2nd"); // timed out
globalValue = 0;
patternCounter = 0;
breakIn = 0;
} // if more than
} // global master
}
// 3rd attempt
if (globalValue==1&&patternCounter==1){ // third round
nextTime = millis(); // start a 'new' counter and 'listen'
if (nextTime<6000) { // less than
if (digitalRead(inPin)==HIGH) { // and true
Serial.println("CLEARED 3rd");
delay (500); // flood protection
patternCounter++;
} // if counter less
} // if true or high
if (nextTime>6000) {
Serial.println("TIMEOUT waiting 3rd"); // timed out
globalValue = 0;
patternCounter = 0;
} // if more than
} // global master
// 4th attempt and latch
if (globalValue==1&&patternCounter==2){ // last round
lastTime = millis(); // start a 'new' counter and 'listen'
if (lastTime<6000) { // less than
if (digitalRead(inPin)==HIGH) { // and true
digitalWrite(outPin, HIGH); // LED on
Serial.println("CLEARED 4th ARRAY"); // cleared the stage
delay(500); // flood protection
} // true or high
} // counter
if (lastTime>6000) {
Serial.println("TIMEOUT waiting 4th"); // timed out
globalValue = 0;
patternCounter = 0;
} // if more than
} // global and alarm
} // loop end
これが現在のスケッチです。私が使用したカウンターはほとんど無意味であると理解しています。
どんな助けでも大歓迎です!