Arduino: programació dirigida per events
Salta a la navegació
Salta a la cerca
- http://blog.ardublock.com/2013/10/29/evaluating-of-event-driven-libraries-on-arduino/
- http://rain.aa.washington.edu/Tutorials/Robotics/Arduino/02%3A_Efficient_Arduino_Programming/01%3A_Event-Driven_Programming
- http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
La manera més senzilla de fer-ho és com en el segon enllaç. Ara bé, aquesta és la manera senzilla de fer-ho: fer que el LED s'encengui i s'apagui cada segon, però no bloquejar tot el codi amb un delay.
// constants won't change. Used here to // set pin numbers: const int ledPin = 13; // the number of the LED pin // Variables will change: int ledState = LOW; // ledState used to set the LED long previousMillis = 0; // will store last time LED was updated // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long interval = 1000; // interval at which to blink (milliseconds) void setup() { // set the digital pin as output: pinMode(ledPin, OUTPUT); } void loop() { // here is where you'd put code that needs to be running all the time. // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) ledState = HIGH; else ledState = LOW; // set the LED with the ledState of the variable: digitalWrite(ledPin, ledState); } }
Una cosa més interessant és mirar altres exemples i llebreries que realment fan programació orientada als events, o programació orientada al pin (detectar l'event canvi d'estat en el pin).
creat per Joan Quintana Compte, desembre 2013