Tout d'abord, très nouveau pour Arduino et j'ai parcouru les tutoriels dans un effort pour faire ce travail et rien n'a semblé m'aider. Ce que j'essaie de faire est d'avoir un rétroéclairage LCD activé pendant 8 secondes quand un bouton est enfoncé.mettre en œuvre des opérations chronométrées non bloquantes - sans délai() - pour Arduino
J'ai eu un succès mineur avec ma première tentative du code:
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();
}
}
Cela fonctionne très bien, j'appuie sur le bouton et il appelle le script, le seul problème est qu'il met tout le reste en pause. Il semble que j'ai besoin de mettre en œuvre une solution en utilisant le `millis 'mais tout ce que j'ai vu fonctionne sur l'esquisse BlinkWithoutDelay et je ne peux pas sembler y arriver.
Tout conseil ou didacticiel pertinent serait formidable.
EDIT:
Je voudrais remercier Pirho pour l'explication ci-dessous. Ce fut le code que j'ai pu faire travailler grâce à leurs conseils:
// 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);
}
}
Merci, qui était en fait très utile quand vous l'avez cassé vers le bas. J'ai posté le code final que j'ai pu faire travailler. Merci encore pour votre aide! –