LDR to pause a constructor to stop a LED from flashing

Hello, I'm trying to use a LDR to avoid turning on a LED on daylight.
I'm running a ISR to run blink the led and flash it, actually I force the led to go off when the LDR value is below some threshold.
But ,I noticed that the LED still wants to lit a bit, I think that the constructor runs, until the loop force it to go off.
Maybe there's some other way to do this,what about changing the intensity value of the constructor. And if so, how could I return to the previous state when the LDR gets no light?

Here is the code....thank you for your help.

#include <SoftwareSerial.h>

#define rxPin 4  
#define txPin 5
SoftwareSerial LoRa1(rxPin,txPin); 

int readPin=A6; // lectura de voltaje de la bateria
const int   ldrPin = A1;  //Donde esta el LDR 

int BLUELed = 6;  //pin our blue LED is connected to
int ledState = LOW; //used to control blue LED state, we're starting with it OFF

unsigned long currentMillis = 0; //stores the current time
unsigned long previousMillis = 0; //stores last time blue LED was updated

//--------------------------ISR-------------------------------------------------------

class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int  ledPin;      // the number of the LED pin
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time
  int  intensity;
  // These maintain the current state
  int ledState;                 // ledState used to set the LED
  unsigned long previousMillis;   // will store last time LED was updated

  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off,int intensidad)
  {
  ledPin = pin;
      
  OnTime = on;
  OffTime = off;
  
  ledState = LOW; 
  previousMillis = 0;
  }

  void begin() {
      pinMode(ledPin, OUTPUT);
    }
    
  void setOnTime(long time1){
    OnTime=time1;
  }
  void setOffTime(long time2){
    OffTime=time2;
  }
  void setIntensity(long i){
    intensity=i;
  }
  void Update(){
  
    unsigned long currentMillis=millis();
    
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      analogWrite(ledPin, 0);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      analogWrite(ledPin, intensity);   // Update the actual LED
    }
  }
};


Flasher led1(6,1000,1000,50); //(6,123,400)

//--------------------------FIN ISR-------------------------------------------------------

void setup(){

  pinMode(readPin,INPUT);
  pinMode(BLUELed, OUTPUT);
  pinMode(ldrPin,   INPUT);
  
//-----------------ISR------------------------------------------------------------------

 // Timer0 is already used for millis() - we'll just interrupt somewhere
  // in the middle and call the "Compare A" function below
  OCR0A = 0xAF;
  TIMSK0 |= _BV(OCIE0A);

//--------FIN ISR ----------------------------------------------------------------------
  
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  //Begin serial communication with Arduino and LoRa1 
  LoRa1.begin(9600);
  
  Serial.println("Initializing...");
 
}

void loop(){
                     
                     unsigned long currentMillis = millis();
                     
int   ldrStatus = analogRead(ldrPin);
                     if (ldrStatus <=700) {
                                          dim();               
                                        }
                    else {
                              digitalWrite(BLUELed,   LOW);          //Apago la baliza
                                        }
                                                                         
                        }

// -----------------DIM--------------------------------------------------------------

void dim(){
            if(LoRa1.available()>0){
                            String content = LoRa1.readString();
                            Serial.print("Se recibió: ");
                            Serial.println(content);
                            Serial.println(content[0]);
                            Serial.println(content[1]);
                            Serial.println(content[2]);                                                                
//----------Decodificacion de intensidad-----------------------------------------                   
                           if(content.substring(0,1)== "5"){
                                      led1.setIntensity(255);
                                            }
                       else if (content.substring(0,1)== "4"){
                                     led1.setIntensity(200);    
                                            }
                       else if (content.substring(0,1)== "3"){
                                      led1.setIntensity(150);
                                            }
                       else if (content.substring(0,1)== "2"){
                                      led1.setIntensity(100);
                                             }
                       
                       else if (content.substring(0,1)== "1"){
                                      led1.setIntensity(50);
                                             }
                       
                       else if (content.substring(0,1)== "0"){
                                      led1.setIntensity(0);
                                             } 
                                               
//--------------Decodificacion de tiempoPrendida---------------------------------------             
                       
                       if(content.substring(1,2)== "5"){
                                     led1.setOnTime(2500); 
                                          }
                       else if (content.substring(1,2)== "4"){
                                     led1.setOnTime(2000); 
                                          }
                       else if (content.substring(1,2)== "3"){
                                     led1.setOnTime(1000); 
                                          }
                        else if (content.substring(1,2)== "2"){
                                     led1.setOnTime(500);
                                          }
                       else if (content.substring(1,2)== "1"){
                                    led1.setOnTime(250);
                                          }   
                       else if (content.substring(1,2)== "0"){
                                     led1.setOnTime(0); 
                                          } 
                                          
//----------------------Decodificacion de tiempoApagada---------------------------------
                                                                                                                                                           
                       if(content.substring(2,3)== "5"){
                                     led1.setOffTime(2500);
                                          }
                       else if (content.substring(2,3)== "4"){
                                     led1.setOffTime(2000); 
                                          }
                       else if (content.substring(2,3)== "3"){
                                     led1.setOffTime(1000);
                                          }
                        else if (content.substring(2,3)== "2"){
                                     led1.setOffTime(500); 
                                          }
                       else if (content.substring(2,3)== "1"){
                                     led1.setOffTime(250);
                                          }   
                       else if (content.substring(2,3)== "0"){
                                     led1.setOffTime(0); 
                                          } 
                        content = "";
                              }

}
//-------------------ISR-------------------------------------------------------

// Interrupt is called once a millisecond, to update the LEDs

SIGNAL(TIMER0_COMPA_vect) {
      
        led1.Update();
                  }
                  
//------------------FIN ISR-----------------------------------------------------

using the same timer-interrupt as millis() is a very uncommon way to blink an LED

Your code inside update switches on the LED

      else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
      {
        ledState = HIGH;  // turn it on
        previousMillis = currentMillis;   // Remember the time
        analogWrite(ledPin, intensity);   // Update the actual LED

for really keeping the LED dark= off:
as you want to have a whole class for something simple like LED blinking
add another function for complete off and then modify update
to use a condition

if (LED_mode != completely_OFF) {
  // do the blinking
}

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.