.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _ar_interval: 1.11 Interval ============= Overview -------- Sometimes you need to do two things at once. For example you might want to blink an LED while reading a button press. In this case, you can't use delay(), because Arduino pauses your program during the delay(). If the button is pressed while Arduino is paused waiting for the delay() to pass, your program will miss the button press. This sketch demonstrates how to blink an LED without using delay(). It turns the LED on and then makes note of the time. Then, each time through loop(), it checks to see if the desired blink time has passed. If it has, it toggles the LED on or off and makes note of the new time. In this way the LED blinks continuously while the sketch execution never lags on a single instruction. An analogy would be warming up a pizza in your microwave, and also waiting some important email. You put the pizza in the microwave and set it for 10 minutes. The analogy to using delay() would be to sit in front of the microwave watching the timer count down from 10 minutes until the timer reaches zero. If the important email arrives during this time you will miss it. What you would do in real life would be to turn on the pizza, and then check your email, and then maybe do something else (that doesn't take too long!) and every so often you will come back to the microwave to see if the timer has reached zero, indicating that your pizza is done. Components Required ------------------- .. image:: img/list_1.11.png * :ref:`cpn_mega2560` * :ref:`cpn_breadboard` * :ref:`cpn_wires` * :ref:`cpn_led` * :ref:`cpn_resistor` Fritzing Circuit ---------------- In this example, we use digital pin 9 to drive the LED, and we attach one side of the resistor to the corresponding digital pins. The longer pin of LED (a positive electrode, referred to as anode) is connected to the other side of the resistor. The shorter pin (a negative electrode, referred to as cathode) of LED is attached to GND. .. image:: img/image30.png Schematic Diagram ----------------- .. image:: img/image466.png Code ---- .. note:: * You can open the file ``1.11_interval.ino`` under the path of ``sunfounder_vincent_kit_for_arduino\code\1.11_interval`` directly. * Or copy this code into Arduino IDE 1/2. .. raw:: html When you finish uploading the codes to the Mega2560 board, you can see the LED uploading. Code Analysis ------------- Declare the digital pin 9 as ledPin. .. code-block:: arduino const int ledPin = 9; Set the state of ledState to LOW to turn off the LED. .. code-block:: arduino int ledState = LOW; Initial a variable named previousMillis to store previous operating time of microcontroller. .. code-block:: arduino unsigned long previousMillis = 0; Set the interval time to 1000ms (milliseconds). .. code-block:: arduino const long interval = 1000; Set ledPin to OUTPUT mode. .. code-block:: arduino pinMode(ledPin, OUTPUT); In loop(), declare currentMillis to store the current time. .. code-block:: arduino unsigned long currentMillis = millis(); When the interval between the current operating time and last updating time is larger than 1000ms, certain functions are triggered. Meanwhile, update the previousMillis to the current time for the next triggering that is to happen 1 second latter. .. code-block:: arduino if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;// save the last time you blinked the LED //... } Here, certain functions executed at intervals are to change the state of LED. .. code-block:: arduino if (ledState == LOW) {ledState = HIGH;} else {ledState = LOW;} digitalWrite(ledPin, ledState); Phenomenon Picture ------------------ .. image:: img/image36.jpeg