Hello everyone,
I am new in the community, I know just a bit about coding and I would like to get help from you.
My mood was awesome when I managed to reproduce a code I found on internet which is using a photoresistor to turn on upto 4 LEDs.
Based on this code, my next step was to try to use this on cayenne so to to turn it on or off but I do not know how to.
I have tried to add a new sensor (photoresistor) and a new actuator (LED) but without success.
PS. All credits and the connecrion scheme of code goes to siddh-1
Can some give me some help?
The code which is using the photoresistor to turn on leds is the following:
#include <EEPROM.h>
const int ldrsen = A1;
int BlueLed = 13;
int value = 0;
int RedLed = 12;
int YellowLed = 11;
int WhiteLed = 10;
//
void setup() {
delay(2000);
Serial.begin(9600);
pinMode(RedLed, OUTPUT);
pinMode(YellowLed, OUTPUT);
pinMode(WhiteLed, OUTPUT);
pinMode(BlueLed, OUTPUT);
pinMode(ldrsen, INPUT);
// Starting the TEST
Serial.println("======Starting the Test======");
Serial.println("You should see the Led blinking every 2 sec");
digitalWrite(BlueLed, HIGH);
delay(200);
digitalWrite(BlueLed, LOW);
delay(200);
digitalWrite(RedLed, HIGH);
delay(200);
digitalWrite(RedLed, LOW);
delay(200);
digitalWrite(YellowLed, HIGH);
delay(200);
digitalWrite(YellowLed, LOW);
delay(200);
digitalWrite(WhiteLed, HIGH);
delay(200);
digitalWrite(WhiteLed, LOW);
delay(200);
Serial.println("Test 1 is completed");
Serial.println("Now testing the Photoresistor, Make sure it is connected to the Pin A1");
delay(2000);
Serial.println("Starting the test now!");
rerun:
value = analogRead(ldrsen) /4;
if(value >1){
Serial.print("The Photoresistor is connected and here are some value: ");
delay(200);
Serial.println(value);
delay(200);
Serial.println(value);
delay(200);
Serial.println(value);
}
else{
value = analogRead(ldrsen)/4 ;
if(value<0)
Serial.println("Hmm the Photoresistor is not connected");
Serial.println("Retrying in 5s");
delay(5000);
goto rerun;
}
Serial.println("======END======");
}
//*************************LOOP*******************
void loop() {
value = analogRead(ldrsen)/4;
delay(100);
if(value < 130){
digitalWrite(BlueLed, HIGH);
}
else
{
digitalWrite(BlueLed, LOW);
}
//Led2
if(value < 150){
digitalWrite(RedLed, HIGH);
}
else
{
digitalWrite(RedLed, LOW);
}
if(value < 250){
digitalWrite(YellowLed, HIGH);
}
else
{
digitalWrite(YellowLed, LOW);
}
if(value < 500){
digitalWrite(WhiteLed, HIGH);
}
else
{
digitalWrite(WhiteLed, LOW);
}
}
While the cayenne standard code for a photoresistor is
Cayenne Photoresistor Example
This sketch shows how to send Photoresistor Sensor data to the Cayenne Dashboard.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Attach a photoresistor to an analog pin on your Arduino.
Make sure to use an analog pin, not a digital pin.
Schematic:
[Ground] -- [10k-resistor] -- | -- [Photoresistor] -- [5V]
|
Analog Pin
2. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
3. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of a Photoresistor Sensor widget you have added) in the Dashboard.
4. Set the Cayenne authentication info to match the authentication info from the Dashboard.
5. Compile and upload this sketch.
6. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the Photoresistor Sensor widget you have added) with data.
To make a temporary widget permanent click the plus sign on the widget.
*/
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char password[] = "78xxxxxxxxxxxxxxxxxxxxxxxx2xxx0";
char clientID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxafxxxx7";
#define SENSOR_PIN 0
#define VIRTUAL_CHANNEL undefined
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
}
void loop()
{
Cayenne.loop();
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(SENSOR_PIN), "analog_sensor", "null");
}```
My idea was to turn the system on and off via internet (cayenne) and get a message about luminosity (the message can be easily done via trigger in cayenne. The issue is I have no idea how to "turn on" the system via cayenne
PS. I apologize for the link shared in that way, It was my first message in the forum