Condition met - turn ON led


I am using analog input and on Serial Monitor I received datas like this:

In some period of time values changes like this:

0.10 and after this value does not change

My code:

#include "EmonLib.h" // 
EnergyMonitor emon1; //

void setup()
  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(9600); //započinjemo serijsku komunikaciju
  emon1.current(0, 111.1); // Current: ulazni pin(A0), kalibracija.


void loop()


  double Irms = emon1.calcIrms(1480); // 

  if (Irms > 5) 

    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(100);                       // wait for a second




I want to make it work like this:

  • from begining when value is less then 0.2 LED is OFF
  • first time when value increases to 5.50 LED is blinking
  • after that value returns to 0.10 - LED continues blinking

I am stuck :(. My LED is blinking when value increases to 5.5 but turns OFF when value changes to 0.10


Some pseudo code for you

blinking = false
turn off the LED
start of loop()
  read the value
  if value >= 5.5
    blinking = true
  end if
  if blinking is true
    blink the LED
  end if
end of loop()

Incidentally, I do not see the threshold values in your code that you refer to in your post

Note that using delay() will make the program stop responding whilst they occur


my code is doing this.
When value is > 5 LED turns ON, but always after this value always value returns to 0.10.
For me is important this moment if value >5, I want LED to show me this.
After that I do not care what happens on analog input.

Okay, then simply never write code to turn it off, done :slight_smile:

Cute idea. Too bad it won't blink.

Then add a little flag which you only set when you go past the threshold.

After that, blinking will ONLY depend on that flag, NOT on the value. Aka, the blink code goes outside any code handling the value.

Aka, the blink code goes outside any code handling the value.

As in the pseudo code in post #1