最初に、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);
}
}
おかげで、このサイトを検索します。私は仕事をすることができた最終的なコードを投稿しました。助けてくれてありがとう! –