Cayenne Single RGB LED Slider


#1

Good day,

Im very new to arduino and coding the arduino so I wanted to start out simple.
I want to control a ws2812 rgb led with three sliders in Cayenne.

This is the code i got so far, but i get error massages.
I think the last bit is in a wrong spot, but i tried a lot of other places but got error massages again.
Can somebody tell me how the code should be?

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

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

#define NUM_LEDS 1
#define VIRTUAL_CHANNEL1 10
#define VIRTUAL_CHANNEL2 11
#define VIRTUAL_CHANNEL3 12
#define ACTUATOR_PIN 6

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812, ACTUATOR_PIN, RGB>(leds, NUM_LEDS);
Cayenne.begin(username, password, clientID);
}

void loop()

{

Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
int value1 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL1, ACTUATOR_PIN, value1);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value1);
}
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
int value2 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL2, ACTUATOR_PIN, value2);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value2);
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
int value3 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL3, ACTUATOR_PIN, value3);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value3);
}
leds[0] = CRGB(value1,value2,value3);
FastLED.show();
delay(1000);


#2

Make the following Change in your code

 void loop(){
  leds[0] = CRGB(value1,value2,value3);
  FastLED.show();
  delay(1000);
   }

Can you post what error are getting.


#3

the error message is get now is:
exit status 1
‘value1’ was not declared in this scope
(that is in the third line)

now the script is like this:

void loop()
{
leds[0] = CRGB(value1,value2,value3);
FastLED.show();
delay(1000);

}
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
int value1 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL1, ACTUATOR_PIN, value1);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value1);
}
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
int value2 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL2, ACTUATOR_PIN, value2);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value2);
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
int value3 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL3, ACTUATOR_PIN, value3);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value3);
}


#4

You need to declare value1 value2 value3 globally.


#5

thank you,
but how do i do that?


#6

Now I did it like this:

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

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

#define NUM_LEDS 1
#define VIRTUAL_CHANNEL1 10
#define VIRTUAL_CHANNEL2 11
#define VIRTUAL_CHANNEL3 12
#define ACTUATOR_PIN 6
int value1;
int value2;
int value3;
// Define the array of leds
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812, ACTUATOR_PIN, RGB>(leds, NUM_LEDS);
Cayenne.begin(username, password, clientID);
}

void loop()

{
Cayenne.loop();

}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
int value1 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL1, ACTUATOR_PIN, value1);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value1);

}
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
int value2 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL2, ACTUATOR_PIN, value2);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value2);
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
int value3 = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL3, ACTUATOR_PIN, value3);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value3);
}

leds[0] = CRGB(value1,value2,value3);
FastLED.show();
delay(1000);

now i get error message:
‘leds’ does not name a type (3th last sentence)


#7

try this code:

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

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

#define NUM_LEDS 1
#define VIRTUAL_CHANNEL1 10
#define VIRTUAL_CHANNEL2 11
#define VIRTUAL_CHANNEL3 12
#define ACTUATOR_PIN 6
int value1;
int value2;
int value3;
// Define the array of leds
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812, ACTUATOR_PIN, RGB>(leds, NUM_LEDS);
Cayenne.begin(username, password, clientID);
}

void loop()

{
Cayenne.loop();
leds[0] = CRGB(value1,value2,value3);
FastLED.show();
delay(1000);
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
int value1 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value1);

}
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
int value2 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value2);
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
int value3 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value3);
}

#8

Thanks, it did not work at first, but after i put in the
‘CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL1, ACTUATOR_PIN, value1);’
again, there was no error and i could upload the program.
but when i moved the sliders the led stays off. the analog write functions quote exactly the number that I put my slider on.
so all that is working, I put a analog write in the Cayenne loop to see if that value was changing, but it did not show in the serial monitor.
so i think for some reason that in the beginning of the loop the value is put to 0 again.
any suggestions?


#9

or the ‘fast led’ is some kind of static program???


#10

Try this code:

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

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

#define NUM_LEDS 1
#define VIRTUAL_CHANNEL1 10
#define VIRTUAL_CHANNEL2 11
#define VIRTUAL_CHANNEL3 12
#define ACTUATOR_PIN 6
int value1;
int value2;
int value3;
// Define the array of leds
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(115200);
FastLED.addLeds<WS2812, ACTUATOR_PIN, RGB>(leds, NUM_LEDS);
Cayenne.begin(username, password, clientID);
}

void loop()

{
Cayenne.loop();
leds[0] = CRGB(value1,value2,value3);
FastLED.show();
delay(1000);
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
value1 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value1);

}
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
 value2 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value2);
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
value3 = getValue.asInt(); // 0 to 255
analogWrite(ACTUATOR_PIN, value3);
}

#11

I tried, but it did not work. I can upload the program, but when i use the slider the led stays off


#12

When i use this program i can change the colour of the led by changing the (255,0,0)

#include "FastLED.h"

#define NUM_LEDS 6

// Data pin that led data will be written out over
#define DATA_PIN 6
// Clock pin only needed for SPI based chipsets when not using hardware SPI
//#define CLOCK_PIN 8

CRGB leds[NUM_LEDS];

void setup() {
	// sanity check delay - allows reprogramming if accidently blowing power w/leds
   	delay(2000);
   	FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
   
}

void loop() {
   leds[0] = CRGB(255,0,0); 
   leds[1] = CRGB(0,255,0);
   leds[2] = CRGB(0,255,0);
   leds[3] = CRGB(0,0,255);
   leds[4] = CRGB(0,0,255);
   leds[5] = CRGB(0,0,255);
   FastLED.show();
   delay(1000);
}

#13

can you try this code and change the slider only connected to channel 10

/*
  This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

  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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  2. Compile and upload the sketch.
  3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#include "FastLED.h"
#define LED_PIN 6
#define NUM_LEDS 6

CRGB leds[NUM_LEDS];

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

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();
}

CAYENNE_IN(10)
{
  int value1 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( value1, 0, 0);
    FastLED.show();
    delay(40);
  }

}

#14

this is working for one slider, so i tried this program

/*
  This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

  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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  2. Compile and upload the sketch.
  3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#include "FastLED.h"
#define LED_PIN 6
#define NUM_LEDS 6

CRGB leds[NUM_LEDS];

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

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();
}

CAYENNE_IN(10)
{
  int value1 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( value1, 0, 0);
    FastLED.show();
    delay(40);
  }

}
CAYENNE_IN(11)
{
  int value2 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( 0, value2, 0);
    FastLED.show();
    delay(40);
  }

}

CAYENNE_IN(12)
{
  int value3 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( 0, 0, value3);
    FastLED.show();
    delay(40);
  }

}

when i do the red slider the red is lighting up
when i do the green slider the green is lighting up and the red disapears.


#15
/*
  This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

  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. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  2. Compile and upload the sketch.
  3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#include "FastLED.h"
#define LED_PIN 6
#define NUM_LEDS 6
 int value1; 
 int value2;
 int value3; 
CRGB leds[NUM_LEDS];

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

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();
}

CAYENNE_IN(10)
{
  value1 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( value1, value2, value3);
    FastLED.show();
    delay(40);
  }

}
CAYENNE_IN(11)
{
  value2 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( value1, value2, value3);
    FastLED.show();
    delay(40);
  }

}

CAYENNE_IN(12)
{
   value3 = getValue.asInt();
  for (int i = 0; i <= 5; i++) {
    leds[i] = CRGB ( value1, value2, value3);
    FastLED.show();
    delay(40);
  }

}

#16

Great, thanks a lot. the sliders are finally working.
I understand a lot more about coding the arduino now.
i got one more question:
can you explain to me what this line of code means
for (int i = 0; i &lt;= 5; i++)


#17

https://www.arduino.cc/reference/en/language/structure/control-structure/for/

best way to learn is using debug. Add Serial.println(i) after the line and check your serial monitor what happens.