Using millis()

Example on how to use millis() to turn led’s on and off at diffrent times, without using delay()

#define buttonPin 2 //button attached to pin 2

#define led1 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9

unsigned long btnMillis = 0; //clock value to be stored at the time milli() is called

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

  attachInterrupt(0, btnDown, RISING); //0 = digital pin 2 or 1 = digital pin 3
}

void loop()
{
  if (btnMillis != 0)
  {
    unsigned long timePassed = millis() - btnMillis;
    int timeOn = 0;
    int timeOff = 0;

    //LED 1
    timeOn = 0;
    timeOff = 1500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led1, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led1, LOW); //turn off event1 pin
    }

    //LED 2
    timeOn = 1200;
    timeOff = 2000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led2, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led2, LOW); //turn off event1 pin
    }

    //LED 3
    timeOn = 1000;
    timeOff = 3000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led3, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led3, LOW); //turn off event1 pin
    }

    //LED 4
    timeOn = 2500;
    timeOff = 6000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led4, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led4, LOW); //turn off event1 pin
    }

    //LED 5
    timeOn = 3000;
    timeOff = 5500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led5, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led5, LOW); //turn off event1 pin
    }

    //Longest off time, to re-enable the button
    timeOff = 6000;
    if (timePassed >= timeOff)
    {
      btnMillis = 0;
    }
  }
}

void btnDown()
{
  if (btnMillis != 0) return;
  btnMillis = millis(); //Set eventtime1 to now + 500ms
}

Using millis to both turn 8 leds on and off with the specific intervals, and fading a led op and down with pwm, without delaying the rest of the code

#define buttonPin 2 //button attached to pin 2

#define led1 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define led7 7
#define led8 6
#define fled 3 //Led on digital pin 3

unsigned long btnMillis = 0; //clock value to be stored at the time milli() is called

//Fade a led without delaying the rest of the code
//START
unsigned long fadeMillis = 0;
boolean fadeUp = true;
int fadeValue = 0;
void analogFade()
{
  if (millis() - fadeMillis >= 5)
  {
    fadeMillis = millis();

    if (fadeUp == true)
    {
      if (fadeValue < 255)
      {
        fadeValue++;
      }
      else
      {
        fadeUp = false;
        fadeValue--;
      }
    }
    else
    {
      if (fadeValue > 0)
      {
        fadeValue--;
      }
      else
      {
        fadeUp = true;
        fadeValue++;
      }
    }
    analogWrite(fled, fadeValue);
  }
}
//STOP

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);

  attachInterrupt(0, btnDown, RISING); //0 = digital pin 2 or 1 = digital pin 3
}

void loop()
{
  analogFade();

  if (btnMillis != 0)
  {
    unsigned long timePassed = millis() - btnMillis;
    int timeOn = 0;
    int timeOff = 0;

    //LED 1
    timeOn = 0;
    timeOff = 1500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led1, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led1, LOW); //turn off event1 pin
    }

    //LED 2
    timeOn = 1200;
    timeOff = 2000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led2, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led2, LOW); //turn off event1 pin
    }

    //LED 3
    timeOn = 1000;
    timeOff = 3000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led3, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led3, LOW); //turn off event1 pin
    }

    //LED 4
    timeOn = 2500;
    timeOff = 6000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led4, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led4, LOW); //turn off event1 pin
    }

    //LED 5
    timeOn = 3000;
    timeOff = 5500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led5, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led5, LOW); //turn off event1 pin
    }

    //LED 6
    timeOn = 2000;
    timeOff = 5500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led6, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led6, LOW); //turn off event1 pin
    }

    //LED 7
    timeOn = 4000;
    timeOff = 5000;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led7, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led7, LOW); //turn off event1 pin
    }

    //LED 8
    timeOn = 1200;
    timeOff = 1500;

    if (timePassed >= timeOn && timePassed <= timeOn+50)
    {
      digitalWrite(led8, HIGH); //turn off event1 pin
    }

    if (timePassed >= timeOff && timePassed <= timeOff+50)
    {
      digitalWrite(led8, LOW); //turn off event1 pin
    }

    //Longest off time, to re-enable the button
    timeOff = 6000;
    if (timePassed >= timeOff)
    {
      btnMillis = 0;
    }
  }
}

void btnDown()
{
  if (btnMillis != 0) return;
  btnMillis = millis(); //Set eventtime1 to now + 500ms
}

Leave a Reply