user52543: I had written a code to calculate rpm of the motors using IR sensor and
Arduino nano and displaying it on OLED . With that I want an LED to
blink always. Here is the code what I did:
You can skip the declarations and setup() part. Just move directly
to loop(). I have problem in loop. In loop(), only LED goes on
blinking and further rpm calculation with OLED displaying codes are not
performed.
#include "avr/sleep.h"
#include "avr/power.h"
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

int led = 12;
int in = 13;
int pushbutton=10;
unsigned long duration = 0;
float rpm = 0;
float rpm_a = 0;
int counter = 0;
int present = 0;
int previous = 0;
unsigned long elapsed = 0;
unsigned long elapsed_prev = 0;
int disabled = 0;

void setup() {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
    pinMode(in,INPUT);
    pinMode(pushbutton,INPUT);
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(35,1);
    display.print("xyz");
    display.display();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(25,13);
    display.print("abc");
    display.display();

    delay(5000);

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("RPMmeter");
    display.display();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,19);
    display.println("RPM:");
    display.setCursor(80,19);
    display.println(rpm);
    display.display();

    elapsed = micros();
}

void loop()
{
    /** led blink part **/
    digitalWrite(led, HIGH);
    delay(70);
    digitalWrite(led, LOW);
    delay(70);
    digitalWrite(led, HIGH);
    delay(70);
    digitalWrite(led, LOW);
    delay(900);

    /** led blink part over and rpm calculation and displaying on
     * oled part starts **/

    if(digitalRead(pushbutton))
    {
        //Arduino low power enabled
        if(disabled==0)
        {
            sleep_disable();
            disabled = 1;
        }
        if (digitalRead(in) == 1 && previous == 0)
        {
            previous = 1;
            duration = elapsed - elapsed_prev;
            elapsed_prev  = micros();
        }
        if (digitalRead(in) == 1 && previous == 1)
        {
            previous = 1;
        }
        if (digitalRead(in) == 0 && previous == 1)
        {
            previous = 0;
        }
        if (digitalRead(in) == 0 && previous == 0)
        {
            previous = 0;
            elapsed = micros();
        }

        rpm = 60000000/duration;

        //We add a small error in the rpm value (in this case +-2)
        if ((rpm_a-2) < rpm  &&  rpm < (rpm_a+2))
        {
            rpm_a = rpm;
            counter = counter + 1;
            if (counter == 50)
            {
                display.clearDisplay();
                display.setTextSize(1);
                display.setTextColor(WHITE);
                display.setCursor(0,0);
                display.println("JARVIS RPMmeter");
                display.setTextSize(2);
                display.setTextColor(WHITE);
                display.setCursor(0,19);
                display.println("RPM:");
                display.setCursor(80,19);
                display.println(rpm);
                display.display();
                counter = 0;
            }
        }
        if (!( (rpm_a-2) < rpm  &&  rpm < (rpm_a+2)))
        {
            rpm_a=rpm;
        }

    }//end if pushbutton=1
    else {
        display.display();
        display.clearDisplay();
        delay(10);
        duration = 0;
        rpm = 0;
        rpm_a = 0;
        counter = 0;
        present = 0;
        previous = 0;
        //Arduino low power enabled
        set_sleep_mode(SLEEP_MODE_PWR_DOWN);
        sleep_enable();
        disabled = 0;
    }
}

This was the code. The problem is without that led blinking code
part in the loop, the code works fine, but if we include that LED
blinking part, the code doesn't move further. I mean only LED goes on
blinking and further codes are not performed. Why is it so? And how to
make both rpm calculation with display on OLED as well as LED
blinking happen through a single code simultaneously?

user30198: There are a few options to use millis() for this.
Is it okay if I change the 900ms into a multiple of 70ms? Then I can count intervals of 70ms.
// blink led: 70ms on, then 70ms off, then 70ms on, then 910ms off

unsigned long previousMillis;
int count = 0;

void setup() {
  pinMode(13, OUTPUT);
}

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

  if(currentMillis - previousMillis >= 70) {
    previousMillis = currentMillis;

    switch(count) {
      case 0:
        digitalWrite(13, HIGH);
        break;
      case 1:
        digitalWrite(13, LOW);
        break;
      case 2:
        digitalWrite(13, HIGH);
        break;
      case 3:
        digitalWrite(13, LOW);
        break;
    }
    count++;

    if(count >= 16)
      count = 0;
  }
}

When the values of the intervals are in a array, then the code is probably smaller.
// blink led: 70ms on, 70ms off, 70ms on, 900ms off

unsigned long previousMillis;
const int intervals[4] = {70, 70, 70, 900};
int index = 0;


void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);  // turn led on, after 70ms it will be turned off
}

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

  if(currentMillis - previousMillis >= intervals[index]) {
    previousMillis = currentMillis;

    int level = (index % 2) == 0 ? LOW : HIGH;
    digitalWrite(13, level);

    index++;

    if(index > 3)
      index = 0;
  }
}

This second sketch is smaller, but I prefer the first one.

user32988: While executing the 'Delay' commands your arduino doesn't really do anything. The best thing you can do is have a look at BlinkWithoutDelay and adapt your code accordingly.

user313: Look at my answer to a similar question. Apply the same principle to your problem:

Move each task (blinking; calculating RPM) into its own separate function.
Make loop() call those functions as often as possible.
Make each function decide whether it is time to do its task, do it or not, and return.
Those functions must never wait.

This kind of program organization - called non-blocking - is extendable to as many tasks as you wish, until you run out of memory, or the processor runs out of time to do everything as fast as you need it to (that will be a great many more than the two you've described!).

52543: <p>I had written a code to calculate rpm of the motors using IR sensor and
Arduino nano and displaying it on OLED . With that I want an LED to
blink always. Here is the code what I did:</p>

<p>You can <strong>skip</strong> the declarations and <code>setup()</code> part. Just move directly
to <code>loop()</code>. I have <strong>problem in loop</strong>. In <code>loop()</code>, only LED goes on
blinking and further rpm calculation with OLED displaying codes are not
performed.</p>

<pre><code>#include "avr/sleep.h"
#include "avr/power.h"
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

int led = 12;
int in = 13;
int pushbutton=10;
unsigned long duration = 0;
float rpm = 0;
float rpm_a = 0;
int counter = 0;
int present = 0;
int previous = 0;
unsigned long elapsed = 0;
unsigned long elapsed_prev = 0;
int disabled = 0;

void setup() {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
    pinMode(in,INPUT);
    pinMode(pushbutton,INPUT);
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(35,1);
    display.print("xyz");
    display.display();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(25,13);
    display.print("abc");
    display.display();

    delay(5000);

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("RPMmeter");
    display.display();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,19);
    display.println("RPM:");
    display.setCursor(80,19);
    display.println(rpm);
    display.display();

    elapsed = micros();
}

void loop()
{
    /** led blink part **/
    digitalWrite(led, HIGH);
    delay(70);
    digitalWrite(led, LOW);
    delay(70);
    digitalWrite(led, HIGH);
    delay(70);
    digitalWrite(led, LOW);
    delay(900);

    /** led blink part over and rpm calculation and displaying on
     * oled part starts **/

    if(digitalRead(pushbutton))
    {
        //Arduino low power enabled
        if(disabled==0)
        {
            sleep_disable();
            disabled = 1;
        }
        if (digitalRead(in) == 1 &amp;&amp; previous == 0)
        {
            previous = 1;
            duration = elapsed - elapsed_prev;
            elapsed_prev  = micros();
        }
        if (digitalRead(in) == 1 &amp;&amp; previous == 1)
        {
            previous = 1;
        }
        if (digitalRead(in) == 0 &amp;&amp; previous == 1)
        {
            previous = 0;
        }
        if (digitalRead(in) == 0 &amp;&amp; previous == 0)
        {
            previous = 0;
            elapsed = micros();
        }

        rpm = 60000000/duration;

        //We add a small error in the rpm value (in this case +-2)
        if ((rpm_a-2) &lt; rpm  &amp;&amp;  rpm &lt; (rpm_a+2))
        {
            rpm_a = rpm;
            counter = counter + 1;
            if (counter == 50)
            {
                display.clearDisplay();
                display.setTextSize(1);
                display.setTextColor(WHITE);
                display.setCursor(0,0);
                display.println("JARVIS RPMmeter");
                display.setTextSize(2);
                display.setTextColor(WHITE);
                display.setCursor(0,19);
                display.println("RPM:");
                display.setCursor(80,19);
                display.println(rpm);
                display.display();
                counter = 0;
            }
        }
        if (!( (rpm_a-2) &lt; rpm  &amp;&amp;  rpm &lt; (rpm_a+2)))
        {
            rpm_a=rpm;
        }

    }//end if pushbutton=1
    else {
        display.display();
        display.clearDisplay();
        delay(10);
        duration = 0;
        rpm = 0;
        rpm_a = 0;
        counter = 0;
        present = 0;
        previous = 0;
        //Arduino low power enabled
        set_sleep_mode(SLEEP_MODE_PWR_DOWN);
        sleep_enable();
        disabled = 0;
    }
}
</code></pre>

<p>This was the code. The problem is <strong>without</strong> that led blinking code
part in the <strong>loop</strong>, the code works fine, but if we include that LED
blinking part, the code <em>doesn't move further</em>. I mean only LED goes on
blinking and further codes are <em>not performed</em>. Why is it so? And how to
make both <strong>rpm calculation with display on OLED</strong> as well as <strong>LED
blinking</strong> happen through a single code simultaneously?</p>
 30198: <p>There are a few options to use millis() for this.
Is it okay if I change the 900ms into a multiple of 70ms? Then I can count intervals of 70ms.</p>

<pre><code>// blink led: 70ms on, then 70ms off, then 70ms on, then 910ms off

unsigned long previousMillis;
int count = 0;

void setup() {
  pinMode(13, OUTPUT);
}

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

  if(currentMillis - previousMillis &gt;= 70) {
    previousMillis = currentMillis;

    switch(count) {
      case 0:
        digitalWrite(13, HIGH);
        break;
      case 1:
        digitalWrite(13, LOW);
        break;
      case 2:
        digitalWrite(13, HIGH);
        break;
      case 3:
        digitalWrite(13, LOW);
        break;
    }
    count++;

    if(count &gt;= 16)
      count = 0;
  }
}
</code></pre>

<p>When the values of the intervals are in a array, then the code is probably smaller.</p>

<pre><code>// blink led: 70ms on, 70ms off, 70ms on, 900ms off

unsigned long previousMillis;
const int intervals[4] = {70, 70, 70, 900};
int index = 0;


void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);  // turn led on, after 70ms it will be turned off
}

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

  if(currentMillis - previousMillis &gt;= intervals[index]) {
    previousMillis = currentMillis;

    int level = (index % 2) == 0 ? LOW : HIGH;
    digitalWrite(13, level);

    index++;

    if(index &gt; 3)
      index = 0;
  }
}
</code></pre>

<p>This second sketch is smaller, but I prefer the first one.</p>
 32988: <p>While executing the 'Delay' commands your arduino doesn't really do anything. The best thing you can do is have a look at <a href="https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay" rel="nofollow noreferrer">BlinkWithoutDelay</a> and adapt your code accordingly.</p>
 313: <p>Look at <a href="https://arduino.stackexchange.com/questions/58675/data-loging-while-browsing-a-menu-on-lcd-shield/58680#58680">my answer to a similar question</a>. Apply the same principle to your problem:</p>
<ul>
<li>Move each task (blinking; calculating RPM) into its own separate function.</li>
<li>Make loop() call those functions as often as possible.</li>
<li>Make each function decide whether it is time to do its task, do it or not, and return.</li>
<li>Those functions must never wait.</li>
</ul>
<p>This kind of program organization - called non-blocking - is extendable to as many tasks as you wish, until you run out of memory, or the processor runs out of time to do everything as fast as you need it to (that will be a great many more than the two you've described!).</p>
