1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

Change to reduce call to millis() by Paul Stoffregen

This commit is contained in:
Tom Igoe
2010-02-09 03:48:57 +00:00
parent d72b7b5222
commit 3310d7c5aa

View File

@ -4,7 +4,7 @@
pin, without using the delay() function. This means that other code pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code. can run at the same time without being interrupted by the LED code.
The circuit: The circuit:
* LED attached from pin 13 to ground. * LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board * Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example. that's attached to pin 13, so no hardware is needed for this example.
@ -12,8 +12,8 @@
created 2005 created 2005
by David A. Mellis by David A. Mellis
modified 17 Jun 2009 modified 8 Feb 2010
by Tom Igoe by Paul Stoffregen
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/ */
@ -42,17 +42,20 @@ void loop()
// check to see if it's time to blink the LED; that is, is the difference // check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than // between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED. // the interval at which we want to blink the LED.
if (millis() - previousMillis > interval) { unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED // save the last time you blinked the LED
previousMillis = millis(); previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa: // if the LED is off turn it on and vice-versa:
if (ledState == LOW) if (ledState == LOW)
ledState = HIGH; ledState = HIGH;
else else
ledState = LOW; ledState = LOW;
// set the LED with the ledState of the variable: // set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState); digitalWrite(ledPin, ledState);
} }
} }