Hi guys!
I just updated my project to make it fully compatible with Arduino board. I was forced to make many changes, especially because of the PWM pins in my Arduino UNO + Ethernet shield. It was not easy but it working
After reading a thousand times Arduino documentation about analogWrite & PWM, I was forced to update my sketch + wiring. (I’m web developer and I have some knowledge in electrical but I need to improve my wiring skills because It’s still the beginning for me in Arduino world. My best friend offered me a Arduino Starter kit for my birthday only 4 months ago…
It would be easier if I had Arduino Mega because it has more available PWM pins but I didn’t buy this kind of Arduino board yet. Bad choice for me
Fist of all, I had to connect my RGB LED strips together. It allows me to get more PWM pins to plug my hardware:
- 1 x 433MHz transmitter
- 1 x IR LED
- 1 x ULN2803 (instead of 2)
Then, I had to update IRremoteInt.h (from IRremote library) to force to use another Timer with PWM Pin #9 instead of PWM Pin #3. (Because my PWM Pin #3 is already used by RGB LED strips)
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
#else
#define IR_USE_TIMER1 // tx = pin 9
//#define IR_USE_TIMER2 // tx = pin 3
#endif
Then, I plugged:
- LED strip Blue to PWM pin 3
- LED strip Red to PWM pin 5
- LED strip Green to PWM pin 6
-
IR LED to PWM pin 9
-
433MHz transmitter to PWM pin 10
Then, I had to update my sketch:
#define CAYENNE_PRINT Serial
#include "CayenneDefines.h"
#include "RCSwitch.h"
#include "IRremote.h"
#include "CayenneEthernet.h"
char token[] = "XXXXXXXXXX"; //your Cayenne token
int loadingTimeChaconZap = 20000; //enable 433MHz 20 seconds after board boot
#define VIRTUAL_PIN_0 V0 //uptime
#define VIRTUAL_PIN_6 V6 //led R
#define VIRTUAL_PIN_7 V7 //led V
#define VIRTUAL_PIN_8 V8 //led B
//Chacon
#define VIRTUAL_PIN_9 V9 //Chacon2
#define VIRTUAL_PIN_10 V10 //Chacon3
#define VIRTUAL_PIN_11 V11 //Chacon4
//Zap
#define VIRTUAL_PIN_12 V12 //Zap1
#define VIRTUAL_PIN_13 V13 //Zap2
#define VIRTUAL_PIN_14 V14 //Zap3
#define VIRTUAL_PIN_15 V15 //Zap4
//All
#define VIRTUAL_PIN_16 V16 //turn on/off all Chacon + Zap
#define VIRTUAL_PIN_17 V17 //turn on/off all Zap
#define VIRTUAL_PIN_18 V18 //turn on/off all Chacon
//IR LED lamp
#define VIRTUAL_PIN_23 V23 //IR LED lamp
//433MHz
RCSwitch mySwitch = RCSwitch();
int transmit_pin = 10;
int pulseLength = 185;
int repeatTransmit = 5;
int transmitterDelay = 100; //Delay between 433 transmitter actions
//RGB LED strip
int LED_STRIP_2_BLUE = 3;
int LED_STRIP_2_RED = 5;
int LED_STRIP_2_GREEN = 6;
//IR
IRsend irsend;
void setup()
{
//Serial
Serial.begin(9600);
//Init LED
pinMode(LED_STRIP_2_RED, OUTPUT);
pinMode(LED_STRIP_2_GREEN, OUTPUT);
pinMode(LED_STRIP_2_BLUE, OUTPUT);
//433MHz Transmitter
mySwitch.enableTransmit(transmit_pin);
mySwitch.setPulseLength(pulseLength);
mySwitch.setRepeatTransmit(repeatTransmit);
//LED strip is green
analogWrite(LED_STRIP_2_RED, 0);
analogWrite(LED_STRIP_2_GREEN, 255);
analogWrite(LED_STRIP_2_BLUE, 0);
//Cayenne
Cayenne.begin(token);
}
void loop()
{
Cayenne.run();
}
//Uptime
CAYENNE_OUT(VIRTUAL_PIN_0){
int uptime = millis() / 1000;
Cayenne.virtualWrite(VIRTUAL_PIN_0, uptime);
}
//Led RED
CAYENNE_IN(VIRTUAL_PIN_6)
{
if(millis() > loadingTimeChaconZap){
float currentValue = getValue.asDouble();
currentValue = currentValue / 1023;
int led2RED = currentValue;
setLED2Red(led2RED); // custom red
}
}
//Led GREEN
CAYENNE_IN(VIRTUAL_PIN_7)
{
if(millis() > loadingTimeChaconZap){
float currentValue = getValue.asDouble();
currentValue = currentValue / 1023;
int led2GREEN = currentValue;
setLED2Green(led2GREEN);
}
}
//Led BLUE
CAYENNE_IN(VIRTUAL_PIN_8)
{
if(millis() > loadingTimeChaconZap){
float currentValue = getValue.asDouble();
currentValue = currentValue / 1023;
int led2BLUE = currentValue;
setLED2Blue(led2BLUE);
}
}
//Chacon 2 on/off
CAYENNE_IN(VIRTUAL_PIN_9)
{
if(millis() > loadingTimeChaconZap){
int currentValue = getValue.asInt(); // 0 to 1
if(currentValue == 1){
chacon2_on();
}
else{
chacon2_off();
}
}
}
//Chacon 3 on/off
CAYENNE_IN(VIRTUAL_PIN_10)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
chacon3_on();
}
else{
chacon3_off();
}
}
}
//Chacon 4 on/off
CAYENNE_IN(VIRTUAL_PIN_11)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
chacon4_on();
}
else{
chacon4_off();
}
}
}
//Zap 1 on/off
CAYENNE_IN(VIRTUAL_PIN_12)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
zap1_on();
}
else{
zap1_off();
}
}
}
//Zap 2 on/off
CAYENNE_IN(VIRTUAL_PIN_13)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
zap2_on();
}
else{
zap2_off();
}
}
}
//Zap 3 on/off
CAYENNE_IN(VIRTUAL_PIN_14)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
zap3_on();
}
else{
zap3_off();
}
}
}
//Zap 4 on/off
CAYENNE_IN(VIRTUAL_PIN_15)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
zap4_on();
}
else{
zap4_off();
}
}
}
//turn on/off all
CAYENNE_IN(VIRTUAL_PIN_16)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
turn_on_all();
}
else{
turn_off_all();
}
}
}
//turn on/off Zaps
CAYENNE_IN(VIRTUAL_PIN_17)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
zap_on();
}
else{
zap_off();
}
}
}
//turn on/off Chacons
CAYENNE_IN(VIRTUAL_PIN_18)
{
if(millis() > loadingTimeChaconZap){
if(getValue.asInt() == 1){
chacon_on();
}
else{
chacon_off();
}
}
}
//IR Slider
CAYENNE_IN(VIRTUAL_PIN_23) {
if(millis() > loadingTimeChaconZap){
float currentValue = getValue.asDouble();
currentValue = currentValue / 1023;
if(currentValue == 85){
irsend.sendNEC(0xFF1AE5, 32);
Serial.println("Circle is RED");
}
if(currentValue == 170){
irsend.sendNEC(0xFF9A65, 32);
Serial.println("Circle is GREEN");
}
if(currentValue == 255){
irsend.sendNEC(0xFFA25D, 32);
Serial.println("Circle is BLUE");
}
}
}
//Set LED strip custom RVB color by strip id
void setColorLED(int red, int green, int blue, int strip)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(LED_STRIP_2_RED, red);
analogWrite(LED_STRIP_2_GREEN, green);
analogWrite(LED_STRIP_2_BLUE, blue);
}
//Set LED custom Red value 0 - 255
void setLED2Red(int red)
{
#ifdef COMMON_ANODE
red = 255 - red;
#endif
analogWrite(LED_STRIP_2_RED, red);
}
//Set LED custom Green value 0 - 255
void setLED2Green(int green)
{
#ifdef COMMON_ANODE
green = 255 - green;
#endif
analogWrite(LED_STRIP_2_GREEN, green);
}
//Set LED custom Blue value 0 - 255
void setLED2Blue(int blue)
{
#ifdef COMMON_ANODE
blue = 255 - blue;
#endif
analogWrite(LED_STRIP_2_BLUE, blue);
}
void chacon2_on(){
mySwitch.sendTriState("0FFFF0FFFFFF"); delay(transmitterDelay); //Serial.println("Chacon #2: ON");
}
void chacon3_on(){
mySwitch.sendTriState("0FFFFF0FFFFF"); delay(transmitterDelay); //Serial.println("Chacon #3: ON");
}
void chacon4_on(){
mySwitch.sendTriState("0FFFFFF0FFFF"); delay(transmitterDelay); //Serial.println("Chacon #4: ON");
}
void chacon2_off(){
mySwitch.sendTriState("0FFFF0FF0000"); delay(transmitterDelay); //Serial.println("Chacon #2: OFF");
}
void chacon3_off(){
mySwitch.sendTriState("0FFFFF0FFFF0"); delay(transmitterDelay); //Serial.println("Chacon #3: OFF");
}
void chacon4_off(){
mySwitch.sendTriState("0FFFFFF0FFF0"); delay(transmitterDelay); //Serial.println("Chacon #4: OFF");
}
void zap1_on(){
mySwitch.sendTriState("F00F0FFF0101"); delay(transmitterDelay); //Serial.println("Zap #1: ON");
}
void zap2_on(){
mySwitch.sendTriState("F00F0FFF1001"); delay(transmitterDelay); //Serial.println("Zap #2: ON");
}
void zap3_on(){
mySwitch.sendTriState("F00F0FF10001"); delay(transmitterDelay); //Serial.println("Zap #3: ON");
}
void zap4_on(){
mySwitch.sendTriState("F00F0F1F0001"); delay(transmitterDelay); //Serial.println("Zap #4: ON");
}
void zap1_off(){
mySwitch.sendTriState("F00F0FFF0110"); delay(transmitterDelay); //Serial.println("Zap #1: OFF");
}
void zap2_off(){
mySwitch.sendTriState("F00F0FFF1010"); delay(transmitterDelay); //Serial.println("Zap #2: OFF");
}
void zap3_off(){
mySwitch.sendTriState("F00F0FF10010"); delay(transmitterDelay); //Serial.println("Zap #3: OFF");
}
void zap4_off(){
mySwitch.sendTriState("F00F0F1F0010"); delay(transmitterDelay); //Serial.println("Zap #4: OFF");
}
//turn on all Zap
void zap_on(){
if(millis() > loadingTimeChaconZap){
zap1_on(); zap2_on(); zap3_on(); zap4_on();
}
}
//turn off all Zap
void zap_off(){
if(millis() > loadingTimeChaconZap){
zap1_off(); zap2_off(); zap3_off(); zap4_off();
}
}
//turn on all Chacon
void chacon_on(){
if(millis() > loadingTimeChaconZap){
chacon2_on(); chacon3_on(); chacon4_on();
}
}
//turn off all Zap
void chacon_off(){
if(millis() > loadingTimeChaconZap){
chacon2_off(); chacon3_off(); chacon4_off();
}
}
//turn on all
void turn_on_all(){
if(millis() > loadingTimeChaconZap){
//Turn ON Chacon
chacon4_on(); //chacon2_on(); chacon3_on();
delay(10);
//Turn ON Zap
zap1_on(); zap2_on(); zap3_on(); zap4_on();
delay(10);
//Turn ON LED Strip
setColorLED(0, 255, 0, 2);
}
}
//turn off all
void turn_off_all(){
if(millis() > loadingTimeChaconZap){
//Turn OFF Chacon
chacon4_off(); //chacon2_off(); chacon3_off();
delay(10);
//Turn OFF Zap
zap1_off(); zap2_off(); zap3_off(); zap4_off();
delay(10);
//Turn OFF LED Strip
setColorLED(0, 0, 0, 2);
}
}
With this updated sketch, it’s working
I created new Trigger to change circle light color automatically. For example, if I set LED strip to Blue, trigger sends an IR value to set the circle light to blue.
At last, I create new Cayenne dashboard to controle my living room devices.
Thanks to Cayenne, I can play with IR + 433MHz + RGB LED strips using official Arduino board.
Don’t hesitate to reply me if there is an easier solution
@Neoxelox Is it better for you ?
Ben