Hi all,
I’m pretty new to coding and photon-arduino applications but I’m a quick learner (or so I think). Hopefully this is the right forum area to post this.
I’m trying to write up a code that allows me to control cheap RGB LED with my voice through Amazon Echo and IFTTT. My code doesn’t work for some reason and I’m not sure what I’m doing wrong.
For some background, I’m using tutorials in the following links and trying to combine sections of the codes to make it work for my situation.
I was able to successfully replicate infrared application (first tutorial below) but I have interference issues from other remotes, so I decided to give radio frequency a try. I recorded the RF signatures of certain light functions and was able to use an Arduino Uno to figure out signal delays. But when I plug these information into the code below, I can’t control the lights with my voice.
int rfTransmitPin = D4; //RF Transmitter pin = digital pin 4
int ledPin = D7; //Onboard LED = digital pin 7
const int codeSize = 25; //The size of the code to transmit
int codeToTransmit[codeSize]; //The array used to hold the RF code
int lightON[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,2,2,2,2,4,3}; //The RF code that will turn the light ON
int lightOFF[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,2,2,2,2,4,3}; //The RF code that will turn the light OFF
int red[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,4,2,2,2,2,3}; //The RF code that will turn the light red
int green[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,4,2,2,4,2,2}; //The RF code that will turn the light green
int blue[]={2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,4,2,2,2,4,3,4}; //The RF code that will turn the light blue
int mode[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,2,2,4,2,4,3}; //The RF code that will cycle modes
int brighter[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,2,4,4,2,2,2}; //The RF code that will increase brightness
int dimmer[]={4,2,4,2,2,4,4,4,2,2,2,2,4,4,4,2,2,2,2,2,4,4,4,4,3}; //The RF code that will decrease brightness
int calibratorDelay=140; // Calibrated for RF signal lengths.
int controlRGB(String command); // String will be provided by voice commands through Amazon Echo and IFTTT recipe
void setup(){
pinMode(rfTransmitPin, OUTPUT); //Transmit pin is an output
pinMode(ledPin, OUTPUT); //I'm using on-board LED signal to make sure my voice is translated into string
Particle.function("lights", controlRGB);
}
int controlRGB(String command){
if(command == "onoff"){
codeToTransmit[codeSize]=lightON[codeSize];
transmitCode();
}
if(command == "red"){
codeToTransmit[codeSize]=red[codeSize];
transmitCode();
}
if(command == "green"){
codeToTransmit[codeSize]=green[codeSize];
transmitCode();
}
if(command == "blue"){
codeToTransmit[codeSize]=blue[codeSize];
transmitCode();
}
if(command == "mode"){
codeToTransmit[codeSize]=mode[codeSize];
transmitCode();
}
if(command == "brighter"){
codeToTransmit[codeSize]=brighter[codeSize];
transmitCode();
}
if(command == "dimmer"){
codeToTransmit[codeSize]=dimmer[codeSize];
transmitCode();
}
}
void transmitCode(){
// The LED will be turned on to create a visual signal transmission indicator.
digitalWrite(ledPin, HIGH);
delay(1000);
//initialise the variables
int highLength = 0;
int lowLength = 0;
//The signal is transmitted 6 times in succession
for(int j = 0; j<6; j++){
for(int i = 0; i<codeSize; i++){
switch(codeToTransmit[i]){
case 2: // SH + LL (indicates Short HIGH and Long LOW signal)
highLength=3; //These values were registered by testing the actual remote control with a RF receiver circuit
lowLength=10;
break;
case 3: // SL + VLL (indicates Short HIGH and Very Long LOW signal)
highLength=3;
lowLength=108;
break;
case 4: // LH + SL (indicates Long HIGH and Short LOW signal)
highLength=10;
lowLength=3;
break;
}
/* Transmit a HIGH signal - the duration of transmission will be determined
by the highLength and timeDelay variables */
digitalWrite(rfTransmitPin, HIGH);
delay(highLength*calibratorDelay);
/* Transmit a LOW signal - the duration of transmission will be determined
by the lowLength and timeDelay variables */
digitalWrite(rfTransmitPin,LOW);
delay(lowLength*calibratorDelay);
}
}
//Turn the LED off after the code has been transmitted.
digitalWrite(ledPin, LOW);
}
I see the on board LED firing after correct voice commands but I can’t detect transmission of RF signal (as evidenced by no color change in LED strip and also by using an Arduino board attached to receiver).
Any help is greatly appreciated!