Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Tal O
Published © MIT

Walk Away From Delay

Using millis in Arduino to time things.

BeginnerFull instructions provided10,788
Walk Away From Delay

Things used in this project

Story

Read more

Schematics

Schematics

Code

Example Code

Arduino
const int ledPin =  LED_BUILTIN;
int ledState = LOW;             
unsigned long previousMillis = 0; 
const long interval = 1000;           

const int ledPin2 =  12;
int ledState2 = LOW;             
unsigned long previousMillis2 = 0; 
const long interval2 = 300;     

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  } //end if 

  if (currentMillis - previousMillis2 >= interval2) {
    previousMillis2 = cur rentMillis;
    if (ledState2 == LOW) {
      ledState2 = HIGH;
    } else {
      ledState2 = LOW;
    }
    digitalWrite(ledPin2, ledState2);
  } //end if 

  
}

Credits

Tal O
20 projects • 55 followers
Maker @ heart

Comments