Hello,
I currently have 3 LED strips (not the standard, I just got these from Target as Christmas tree lights, just a bunch of 1.5v LEDs in parallel for about 9’ each) connected to my Photon in D0, D1 and D2. I have a functioning section of code that’s more or less getting the results I want for the LEDs to actually react to the music, but it’s in the void loop section. I would like to be able to turn it on or off via Amazon Alexa via IFTTT. I have had some success in testing turning on solid colors and such, but anything that’s a loop will end up timing out the Photon and I can’t turn them off. I would like to figure out a way to loop it without timing out, as well as have a solid on state option as well.
This is the code I currently have, “musicTrigger” is a digitalOut that I could send to a mosfet to interrupt the signal to the lights, but I would like to avoid this to save space and make it more code reliant than component.
int r = D0; //red strip
int g = D1; //green strip
int w = D2; //white/yellow strip
int led1 = D7; //onboard led
int Ai = A2; //where I am reading the audio input from; a subwoofer line out channel on my receiver.
int musicTrigger = D3; //the on/off for the loop if using a mosfet
void setup()
{
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(w, OUTPUT);
pinMode(musicTrigger, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(Ai, INPUT);
Particle.subscribe("simple", simpleLights); //solid lights
Particle.subscribe("music", musicLights); //lights reacting to music via line out subwoofer channel on receiver
//both of those have an IFTTT on/off recipe
analogWrite(r, 0); //starts in off mode whenever photon restarts or is flashed
analogWrite(g, 0);
analogWrite(w, 0);
digitalWrite(led1, LOW);
digitalWrite(musicTrigger, LOW);
}
void loop() // this is what I want to get into a function to turn on and off. As is, it just is always on. If I use the while loop it stays off until the trigger is 1, then times out.
{
// while(musicTrigger == 1) { // 1 is On and 0 is Off. Only using this if a mosfet is necessary
int lightOut = analogRead(Ai);
if (lightOut >= 9) { // if audio source is "louder" than an input of 9, it will output that times 20 to the lights. They're roughly fully on here if it is true.
analogWrite(r, (lightOut * 20));
delay(0);
}
else if (lightOut >= 6) { //same as above, but a little less bright and more frequent
analogWrite(g, (lightOut * 17));
delay(0);
}
else if (lightOut >= 4) { //same pattern as above
analogWrite(w, (lightOut * 15));
delay(0);
}
else if (lightOut < 3) { // whenever the signal is lower than 3 I just have the lights in a low brightness state but still on, usefull for long breaks in bass.
analogWrite(r, 10);
analogWrite(g, 10);
analogWrite(w, 10);
}
else {
}
//I know that if I made the delay on those longer, it would be fine. But in doing so I lose the effect of lighting to the beat.
/* analogWrite(r,255); //A test function when music isn't available, this actually works fine and doesn't time out when in use.
analogWrite(g,150);
analogWrite(w,0);
delay(250);
analogWrite(w,255);
delay(250);
*/
// }
}
void simpleLights(const char *event, const char *data) //I couldn't get calling a function to work in IFTTT, so I had to learn the subscribe method instead. Not sure If I'm utilizing it correctly though, although it seems to work fine so far.
{
if (strcmp(data, "On") == 0) {
digitalWrite(musicTrigger, LOW); // turns off the loop if using a mosfet
analogWrite(r, 255);
analogWrite(g, 255);
analogWrite(w, 255);
digitalWrite(led1, HIGH); // blink onboard led 3 times
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
}
else if (strcmp(data, "Off") == 0) {
digitalWrite(musicTrigger, LOW);
analogWrite(r, 0);
analogWrite(g, 0);
analogWrite(w, 0);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
}
else {
}
}
void musicLights(const char *event, const char *data)
{
if (strcmp(data, "On") == 0) {
digitalWrite(musicTrigger, HIGH);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
}
else if (strcmp(data, "Off") == 0) {
digitalWrite(musicTrigger, LOW);
analogWrite(r, 0);
analogWrite(g, 0);
analogWrite(w, 0);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
digitalWrite(led1, HIGH);
delay(250);
digitalWrite(led1, LOW);
delay(250);
}
else {
}
}
If anyone has any ideas or a solution to this, it is much appreciated!
Thanks!