Photoresistor

Dear friends!
The code for this project is pretty simple, but… I need a help.
Project:
-The resistance value becomes smaller when there is much light in the room. So in the dark the led remains off because the resistance has become very big. It’s also called light-dependent resistor (LDR).
The LED on the Arduino does not turn on and does not turn on when the condition is met. Why? What to fix?

#define CAYENNE_PRINT Serial
#define led_pin 14
#include <CayenneMQTTESP8266.h>

char ssid = “”;
char wifiPassword = “”;

char username = “”;
char password = “”;
char clientID = “”;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(led_pin, OUTPUT);
}
void loop() {
Cayenne.loop();
Cayenne.virtualWrite(2, analogRead(A0));
delay(1000);

}

CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite(0, millis());
}

CAYENNE_IN_DEFAULT() {
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
int i=getValue.asInt();
digitalWrite(led_pin,i);
if (i < 800) {
digitalWrite(led_pin, HIGH);
}
else if (i > 820) {
digitalWrite(led_pin, LOW);
}
}

int i=getValue.asInt();
digitalWrite(led_pin,i);
if (i < 800) {
digitalWrite(led_pin, HIGH);
}
else if (i > 820) {
digitalWrite(led_pin, LOW);
}

this code is not at in right function

CAYENNE_IN_DEFAULT() function for processing actuator commands from the Cayenne Dashboard.

you should do this:

CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite(0, millis());
int i=analogRead(A0);
if (i < 800) {
digitalWrite(led_pin, HIGH);
}
else if (i > 820) {
digitalWrite(led_pin, LOW);
}
}

@rrusskova are you using a slider to the value int i=getValue.asInt(); from cayenne dashboard and pass the value to led? you should also use analogWrite(led_pin, i); to control the brigtness.

or are you trying to read the value of LDR, then you need
int i =analogRead(ldr);//Reads the Value of LDR(light).
and as @fyhy1234 mentioned the code should belong to CAYENNE_OUT_DEFAULT if you want to send the LDR value to cayenne.

1 Like