0% found this document useful (0 votes)
70 views5 pages

Delay Without Delay, Aurdino Project

This document provides a tutorial on using millis() instead of delay() in Arduino to allow other tasks to run during a delay period. It describes a simple circuit with two LEDs, one blinking on a one second interval using millis(), the other lighting when a button is pressed. The code example uses millis() to toggle the red LED on and off each second while also responding to button presses by lighting the green LED. Using millis() allows the button response to occur during the red LED blinking delay.

Uploaded by

HARRY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views5 pages

Delay Without Delay, Aurdino Project

This document provides a tutorial on using millis() instead of delay() in Arduino to allow other tasks to run during a delay period. It describes a simple circuit with two LEDs, one blinking on a one second interval using millis(), the other lighting when a button is pressed. The code example uses millis() to toggle the red LED on and off each second while also responding to button presses by lighting the green LED. Using millis() allows the button response to occur during the red LED blinking delay.

Uploaded by

HARRY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Instructables

Beginning Arduino: Delay Without Delay()


Published Dec. 28, 2014 StatsDownloadFavorite

Introduction: Beginning Arduino: Delay Without Delay()

About: Most of my instructables will be tutorials for Atmel microcontrollers, Arduino, or Raspberrypi. I
try to show concepts that you can use in your own programs by creating simple programs and circuits to
illus... More About JRV31 »

When you use the delay() function your program stops and nothing else can happen during the delay.
That is easy, but what if you want to have something else going on during the delay?

The answer; use millis().

This tutorial is a simple sketch and circuit to show how this is done.

You will need:

Arduino

Breadboard

Jumper wires

2 - LEDs, I used one red and one green

2 - 330-560 Ohm resistors, for LEDs

Pushbutton switch

Add TipAsk QuestionCommentDownload

Step 1: The Circuit


Follow the diagram and build the circuit from the parts list on the previous page.

Add TipAsk QuestionCommentDownload

Step 2: The Code


/*********************************************************

* Demonstration using millis() instead of delay() so

* another activity can happen within the delay.

*
* The anode of a red LED is connected to pin 10 with a

* resistor in series connected to ground.

* The anode of a green LED is connected to pin 11 with a

* resistor in series connected to ground.

* A pushbutton switch is connected to pin 12 and ground.

* The red LED blinks on for one second then off for one

* second.

* The green LED lights when the button is pressed.

*********************************************************/

unsigned long time = millis();

int toggle = 1;

/**********************************************

* setup() function

**********************************************/

void setup()

pinMode(10, OUTPUT); //Red LED

pinMode(11, OUTPUT); //Green LED

pinMode (12, INPUT_PULLUP); //Switch


digitalWrite(10, HIGH); //Initial state of red LED

/**********************************************

* loop() function

**********************************************/

void loop()

if(millis()-time > 1000) //Has one second passed?

toggle = !toggle; //If so not toggle

digitalWrite(10, toggle); //toggle LED

time = millis(); //and reset time.

digitalWrite(11, !digitalRead(12)); //Light green LED if Button pressed.

You might also like