The LDR is showing two values

I connected my arduino programmer by BYOT. The widget for the for the LDR appeared but keep on changing between two values

image

image

How do I make the display value stable? I want to use only one value for creating trigger

Thanks

can you post the code you are using.

*
Cayenne Button Widget Example

This sketch shows how to set up a Button Widget with Cayenne.

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. In the Cayenne Dashboard add a new Button Custom Widget.
2. Select a virtual channel number for the widget.
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7. Compile and upload this sketch.
8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
  char clientID[] = "";

unsigned long lastMillis = 0;

#define VIRTUAL_CHANNEL_YELLOW 2
#define ACTUATOR_PIN_YELLOW 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_BLUE 3
#define ACTUATOR_PIN_BLUE 3
#define VIRTUAL_CHANNEL_RED 4
#define ACTUATOR_PIN_RED 4
#define VIRTUAL_CHANNEL_ALARM 5
#define ACTUATOR_PIN_ALARM 5
#define VIRTUAL_CHANNEL_MOTOR 8
#define ACTUATOR_PIN_MOTOR 8
#define VIRTUAL_CHANNEL_GREEN 9
#define ACTUATOR_PIN_GREEN 9
#define VIRTUAL_CHANNEL_MOTION 7
#define SENSOR_PIN_MOTION 7
#define VIRTUAL_CHANNEL_TEMP 1
#define SENSOR_PIN_LDR 2
#define VIRTUAL_CHANNEL_LDR 0


// Analog pin the TMP36 is connected to.
const int tmpPin = 1;

// Voltage to the TMP36. For 3v3 Arduinos use 3.3.
const float voltage = 5.0; 

TMP36 tmpSensor(tmpPin, voltage);

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN_YELLOW, OUTPUT);
  pinMode(ACTUATOR_PIN_BLUE, OUTPUT);
  pinMode(ACTUATOR_PIN_RED, OUTPUT);
  pinMode(ACTUATOR_PIN_ALARM, OUTPUT);
  pinMode(ACTUATOR_PIN_MOTOR, OUTPUT);
  pinMode(ACTUATOR_PIN_GREEN, OUTPUT);
  
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if(millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, lastMillis);
  }
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(2)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_YELLOW, ACTUATOR_PIN_YELLOW, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_YELLOW, value);
}

CAYENNE_IN(3)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_BLUE, ACTUATOR_PIN_BLUE, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_BLUE, value);
}

CAYENNE_IN(4)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_RED, ACTUATOR_PIN_RED, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_RED, value);
}

CAYENNE_IN(5)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_ALARM, ACTUATOR_PIN_ALARM, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_ALARM, value);
}

CAYENNE_IN(8)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_MOTOR, ACTUATOR_PIN_MOTOR, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_MOTOR, value);
}

CAYENNE_IN(9)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_GREEN, ACTUATOR_PIN_GREEN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_GREEN, value);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(7)
{
  // Read data from the sensor and send it to the virtual channel here.
  // For example, to send a digital value you can use the following:
  int value = digitalRead(SENSOR_PIN_MOTION);
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_MOTION, value, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(1)
{
  // This command writes the temperature in Celsius to the Virtual Channel.
  Cayenne.celsiusWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getCelsius());
  // To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getFahrenheit());
  //Cayenne.kelvinWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getKelvin());
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL_LDR)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_LDR, analogRead(SENSOR_PIN_LDR));
}

the problem is because you are sending two value to virtual channel 0.

this sends the current uptimes in millis to virtual channel 0.

this sends the LDR value also to chancel 0 (#define VIRTUAL_CHANNEL_LDR 0)

change anyone to different virtual channel and you are good to go.

ok. i will change it. how many available virtual channels are there?

there is no limit to the number of virtual channels.

ok thanks for the swift reply. i will revise the code and i will let u know

thanks @shramik_salgaonkar. it worked

1 Like