2017-10-15 15 views
0

最初に、Arduinoには初めての新機能で、この作業を行うためにチュートリアルを精査しましたが、何も私を助けてくれないようです。私がやろうとしているのは、ボタンを押したときにLCDバックライトを8秒間作動させることです。私はコードで私の最初の試みでマイナーな成功を収めている遅延なしの実装 - 遅延なし() - Arduinoの場合

const int buttonPin = 13;  // the number of the pushbutton pin 
const int ledPin = 9;  // the number of the LED pin 

// variables will change: 
int buttonState = 0;   // variable for reading the pushbutton status 

void timeDelay(){ 

digitalWrite (ledPin, HIGH); 
delay(8000); 
digitalWrite (ledPin, LOW); 

} 


void setup() { 
    // initialize serial communications at 9600 bps: 
    Serial.begin(9600); 
    // initialize the LED pin as an output: 
    pinMode(ledPin, OUTPUT); 
    // initialize the pushbutton pin as an input: 
    pinMode(buttonPin, INPUT); 
} 

void loop() { 

    // read the state of the pushbutton value: 
    buttonState = digitalRead(buttonPin); 

    // check if the pushbutton is pressed. 
    // if it is, the buttonState is HIGH: 
    if (buttonState == HIGH) { 
    // turn LED on: 
    timeDelay(); 
} 
} 

これは素晴らしい作品、私はボタンを押して、それが唯一の問題は、それが一時停止に他のすべてを置くことで、スクリプトを呼び出します。私は `millis 'を使って解決策を実装する必要があるようですが、私が見たすべてのものはBlinkWithoutDelayスケッチからうまく機能していると思われます。

アドバイスや関連するチュートリアルは素晴らしいです。

編集:

以下の説明はpirhoに感謝します。

// constants won't change. Used here to set a pin number : 
const int ledPin = 9;// the number of the LED pin 
const int buttonPin = 13;  // the number of the pushbutton pin 

// Variables will change : 
int ledState = LOW;    // ledState used to set the LED 
int buttonState = 0;   // variable for reading the pushbutton status 


// Generally, you should use "unsigned long" for variables that hold time 
// The value will quickly become too large for an int to store 
unsigned long previousMillis = 0;  // will store last time LED was updated 
unsigned long currentMillis = 0; 
unsigned long ledTimer = 0; 

// constants won't change : 
const long interval = 8000;   // interval at which to blink (milliseconds) 


void setup() { 
    // set the digital pin as output: 
    pinMode(ledPin, OUTPUT); 
// digitalWrite(ledPin, ledState); 
    pinMode(buttonPin, INPUT); 
} 

void loop() { 
    // here is where you'd put code that needs to be running all the time. 
currentMillis = millis(); 
buttonState = digitalRead(buttonPin); 
ledTimer = (currentMillis - previousMillis); 
    if (buttonState == HIGH) { 
    digitalWrite (ledPin, HIGH); 
    previousMillis = millis(); 
} 

    if (ledTimer >= interval) { 
     // save the last time you blinked the LED 
     digitalWrite (ledPin, LOW); 
     } else { 
     digitalWrite (ledPin, HIGH); 
     } 

    } 

答えて

2

はい、あなたのvoid timeDelay() {...}ブロック、ループ内のdelay(8000);:これは私が彼らの指導の仕事のおかげを作ることができたコードでした。

  • BTNがpressMillis店を押された場合、currentMillis-pressMillis> 8000ならば、主導を閉めた場合率い
  • は、比較点灯:各ラウンドでは、あなたがループでなければならないブロックを解除、それを変更するには

  • 、これはあまりにも抽象的が、あなたのためのコードを記述するつもりはないではなかった他のアクション

希望を行う;)

チェックはリードステートに基づいて最適化することもできますが、io書き込みと追加コードの代わりにチェックを行うだけでパフォーマンスが変化する可能性はありません。

更新:ProtoThreadsのようなマルチスレッドライブラリも使用できます。プログラミングスキルや並列タスクのプログラム/カウントの複雑さにもよりますが、必ずしもそうでない可能性もあります。

は、あなたがそれを壊したときに実際に非常に有用であったことをarduino thread

+0

おかげで、このサイトを検索します。私は仕事をすることができた最終的なコードを投稿しました。助けてくれてありがとう! –