Check the status of the lamp with LDR( replacing chain sensor)

Good night, I’m having problems with a code to read the value of (LDR), to check the status of the lamp, I used an already ready code from a moderator to check the status of the relay, but I would try Add the LDR sensor to check the status of the lamp, as I will also activate the lamp with a physical switch when necessary.if anyone can help me, I’m a little noob in programming, in brazil have poor support

  #define BLYNK_PRINT SwSerial

   const int led = 8;   
      const int ldr = A0;  


 #include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX

#include <BlynkSimpleStream.h>


char auth[] = "*****************";

BLYNK_WRITE(V1)
{
   int pinValue = param.asInt();
   SwSerial.print("V1 Slider value is: ");
   SwSerial.println(pinValue);
   digitalWrite(13,pinValue);
}
 BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); 
  SwSerial.print("V0 Slider value is: ");
  SwSerial.println(pinValue);

}

 void setup()
  { 
    
           Serial.begin(9600);
       pinMode(led, OUTPUT);  
       pinMode(ldr, INPUT);
      

 SwSerial.begin(9600);

 Serial.begin(9600);
 Blynk.begin(Serial, auth);
 pinMode(13, OUTPUT);
}

void loop()

{


  int ldrStatus = analogRead(ldr); 
 Serial.println(ldrStatus);
 delay(300);  

 if (ldrStatus >=420) {

digitalWrite(led, HIGH);               //turn LED on


 }


  Blynk.run();

}

Hi @Guilherme_Ferreira
You don’t tell us anything about the hardware you’re using and what results you get when you try using this code, so it’s difficult to give definitive answers. Here are my assumptions and observations…

a) I’m guessing that you’re using something like an Arduino Uno connected to your the internet via the serial connection of your PC. This is okay for initial familiarisation with Blynk, but obviously isn’t a sustainable solution going forward, so consider scrapping the Arduino and using something like a Wemos D1 Mini.
I’ve never used this serial connection method myself, so i’m not familiar with the best way to do it, but you seem to be doing it the opposite way to the examples in Sketch Builder. You’re using the hardware serial for the data connection to Blynk and SoftwareSerial for your debug monitor. Do you have an FTDI adapter connected to pins 10 & 11 to monitor your serial output? If so, what reults are you seeing?

b) Your code is structured so that you’re attempting to read the LDR every time the code goes around the void loop(). This normally happens many times per second, except that you’ve thrown a 300ms delay in there to slow things down.
Rule number 1 of Blynk programming is not to use delays - they block the Blynk communication process and cause problems. Use Timers instead - see Rule 3.
Rule number 2 of Blynk programming is “take all of that crap out of your void loop()” and replace it with:

void loop()
{
  Blynk.run();
  timer.run();
}

To understand what timer.run(); does, see Rule 3.
Rule number 3 of Blynk programming is that you should put the code that does things (checking the LDR reading in this case) in a separate function (or functions) and call it with a Timer.

c) To be a good programmer you need to think logically and look at how each element within the code is going to work. At the moment, your code will turn the LED on if the reading from your LDR is 420 or higher. You’re assuming that the pin that the LED is connected to will be low at boot-up, but you don’t explicitly tell the Arduino to pull that pin low once you’ve declared the pin as an output.
More importantly, you have no mechanism for making the pin go low again if the LDR reading drops below 420. One way to do this is with an else statement like this:

if (ldrStatus >=420)
  {
    digitalWrite(led, HIGH);               //turn LED on
  }
else
  }
    digitalWrite(led, LOW);               //turn LED off
  {

Pete.

sorry if I was not clear because my English is bad I’ll give you more information:
I am using an arduino uno via cable usb only for tests then migrate to wifi connection by esp8266-01 or a nodemcu, the scheme is as follows :frowning: sorry for the poor quality of the scheme I made it as fast as possible to answer you). the objective and that with the LDR I can get a value to know that the light is on when using the physical button, changing the state of the relay to “on” when the physical switch is triggered so not being in place I will know that the light is connected or not. Because it is triggered by the physical the app does not indicate that the light is accessing.
when the ldr reads a value defined by the port A0 assono the relay, so I will know by the blynk that the light is on, and when it gives a low value I disconnect the relay to know that it is erased, so I can use the physical button and the relay to the bulb, but I will know the state of it even if I use the physical button.

Okay, that makes sense.
Most people use some sort of current sensing device to know if power is flowing to the light, but your solution would work provided the LDR doesn’t get confused by ambient light.
Personally, I always try to use the right type of development board from the outset, as there can be significant differences and issues between the various internet connection methods, so you may spend your time fixing a problem that won’t be an issue with your final setup anyway.
I realise that as newcomer you may not know what the final solution will look like, and if you’re doing a Proof of Concept then then you use whatever’s available, but you could waste a lot of time fixing issues with the serial connection via the PC when this won’t be your final system.
Don’t go for the ESP-01 and Arduino combination, it’s really not a good solution.

Are you using an FTDI adapter connected to pins 10 and 11, and if so what are you seeing on your serial monitor.

Did you understand what I said in (b) and © above.

Pete.

I think it would be better to replace the LDR with the current sensor, would you have any suggestion of a code? or any topic that addresses this? relay with physical button and ACS712? Could you give me a hand? I am a layman in programming and Blynk is great but I need a lot of knowledge: /

This, and the linked topics within it, are a good starting point:

Pete.