Introduction: Blink Led With Any On/off Timing, Using 1 Function and No Delay

About: Apprentice Maker

In this Instructables we will go from a simple sketch to blink a led with a choosen On and Off time to a single function that we can use with any led.

Required:

  • Arduino Board
  • Arduino IDE installed
  • 1 Leds
  • 1 Current Limiting Resistors
  • Breadboard and Jumper Wires

If you just want to read the final code jump to the final step.

Connect the hardware like in the picture and open the Arduino IDE.

Step 1: How It Works

This is the sketch, all the code is written in the loop section.

int tOn = 500;

int tOffTime = 500;
int timer;

unsigned long previousMillis=0;

#define pinLed 4

void setup() {
  pinMode(pinLed, OUTPUT);
}
 
void loop() {
  if (millis() - previousMillis) >= timer) {
   
    if (digitalRead(pinLed)==HIGH) {     
      timer = tOff;
    } else {
     
      timer = tOn;
    }

digitalWrite(pinLed, !digitalRead(pinLed)); 

 }
}

Let's see how it works:

  • tOn and tOff values represent how long the led will stay on and how long will stay off
  • timer: at every cycle this value means "how long till the led will switch state" and it's going to be equal to tOn or tOff
  • previousMillis: we already met this in our previous Inst. Will store the previous millis() value at witch the led changed status.
  • if ((millis() - previousMillis) >= timer): this will happen only when the values of millis elapsed from the previous event will be equal to "timer"
  • if (digitalRead(ledPin)...: if the led is on, the variable "timer" will store the Off time for the next cycle and viceversa

In the next step we will write all this code in a simple function using parameters to adjust tOn and tOff

Step 2: Write It in a Function and Take Advantage of Arguments

So here it is the new code:

#define ledPin 4

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

OnOffBlink(300, 100); //OnOffBlink(tOn, tOff);
}

void OnOffBlink(int tOn, int tOff){

static int timer=tOn;
  static long previousMillis;
  
  if ((millis() - previousMillis) >= timer) {
    
    if (digitalRead(ledPin) == HIGH) {
      timer = tOff;
    } else {
      timer = tOn;
    }

digitalWrite(ledPin, !digitalRead(ledPin));
     previousMillis = millis();
  }
}

As you can see the only change we made is in the way tOn and tOff are declared; also all the related variables are now inside the function.

In the next step we will make this function usable with any led in the code, in the same way we did for the BlinkWithoutDelay in this Instructables.

Step 3: A Function Usable by Any Led

And here is the final code with a second led.

the only var to declare outside the function is the array, that stores the millis value for each of the led you want to use.

So when you call the function you have to type: which led, On time, Off time, array position (that is unique for each led!)

#define redLed 5
#define bluLed 6

long previousMillis[2]; //[x] = number of leds

void setup() { pinMode(redLed, OUTPUT); pinMode(bluLed, OUTPUT); } void loop() { //OnOffBlink(which led?, tOn, tOff, One of the previousMillis); OnOffBlink(redLed, 300, 500, 0); //array must be different OnOffBlink(bluLed, 300, 300, 1); //for each led }

void OnOffBlink(int led, int tOn, int tOff, int array){

static int timer=tOn; if ((millis() - previousMillis[array]) >= timer) { if (digitalRead(led) == HIGH) { timer = tOff; } else { timer = tOn; } digitalWrite(led, !digitalRead(led)); previousMillis[array] = millis(); } }

Hope you like it.

If you have tips, hint, critics and everything please comment below!