You are on page 1of 1

To Improve efficiency of blinking LED in STM32:

Using delay() function for blinking LED causes the microcontroller to stop the program for a
specific amount in milliseconds specified in the parameter. To make the programme more
efficient we can use the millis() function which will return number of milliseconds that have
passed since the microcontroller started running the program. millis() function will allow the
programme to run without breaking or stopping the programme unlike delay() function and
hence is more effective and efficient.

Code for Blinking LED using millis() function:


int Led1,Led2;
int Period1, Period2;
unsigned long Time1, Time2;

void setup() {
pinMode(13,OUTPUT); // Selected pin for output
pinMode(12,OUTPUT);
Time1 = Time2 = millis();
Period1 = 500;
Period2 = 500;
}

void loop() {
if(millis()-Time1>=Period1){
Led1=!Led1;
digitalWrite(13,Led1);
Time1=millis();
}
if(millis()-Time2>=Period2){
Led2=!Led2;
digitalWrite(12,Led2);
Time2=millis();
}
}

You might also like