// SENDING DEVICE CODE
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D4
#define PIXEL_COUNT 22
#define PIXEL_TYPE WS2812
// Define variables
int numGenres = 126;
String genreToFind = "";
String artistID = "";
String genreSimplified = "";
String trackID = "";
String spotify = "spotify:track:";
String track_success_cd = "";
int color[3] = {0};
int r = 0;
int g = 0;
int b = 0;
int buttonState = 0;
int GlowState = 0;
unsigned long lastCheckTime = 0;
// unsigned long now = 0;
String genres [] = { "acoustic","afrobeat","alt-rock","alternative","ambient", "anime", "black-metal", "bluegrass", "blues", "bossanova", "brazil", "breakbeat", "british", "cantopop", "chicago-house", "children", "chill", "classical", "club", "comedy", "country", "dance", "dancehall", "death-metal", "deep-house", "detroit-techno", "disco", "disney", "drum-and-bass", "dub", "dubstep", "edm", "electro", "electronic", "emo", "folk", "forro", "french", "funk", "garage", "german", "gospel", "goth", "grindcore", "groove", "grunge", "guitar", "happy", "hard-rock", "hardcore", "hardstyle", "heavy-metal", "hip-hop", "holidays", "honky-tonk", "house", "idm", "indian", "indie", "indie-pop", "industrial", "iranian", "j-dance", "j-idol", "j-pop", "j-rock", "jazz", "k-pop", "kids", "latin", "latino", "malay", "mandopop", "metal", "metal-misc", "metalcore", "minimal-techno", "movies", "mpb", "new-age", "new-release", "opera", "pagode", "party", "philippines-opm", "piano", "pop", "pop-film", "post-dubstep", "power-pop", "progressive-house", "psych-rock", "punk", "punk-rock", "r-n-b", "rainy-day", "reggae", "reggaeton", "road-trip", "rock", "rock-n-roll", "rockabilly", "romance", "sad", "salsa", "samba", "sertanejo", "show-tunes", "singer-songwriter", "ska", "sleep", "songwriter", "soul", "soundtracks", "spanish", "study", "summer", "swedish", "synth-pop", "tango", "techno", "trance", "trip-hop", "turkish", "work-out", "world-music"};
int colors [][3] = {{255,255,0}, {0,255,0}, {255,0,0}, {128,128,128}, {255,0,255}, {255,0,255}, {128,0,128}, {0,0,255}, {0,0,255}, {128,128,0}, {0,255,0}, {0,255,255}, {128,0,0}, {0,128,128}, {255,0,255}, {255,255,255}, {255,0,255}, {0,0,128}, {255,0,0}, {255,255,0}, {128,128,0}, {255,0,0}, {255,255,0}, {128,0,0}, {255,0,255}, {255,0,0}, {0,128,128}, {0,255,255}, {255,0,0}, {0,255,0}, {0,255,0}, {0,0,255}, {255,255,0}, {255,255,0}, {255,0,255}, {128,128,128}, {255,255,0}, {0,255,255}, {128,0,128}, {128,0,0}, {255,0,0}, {0,0,255}, {255,0,255}, {128,128,0}, {0,255,255}, {128,0,0}, {128,128,0}, {255,255,0}, {128,0,0}, {128,0,0}, {128,0,0}, {128,0,0}, {0,255,0}, {0,255,0}, {128,128,0}, {255,0,255}, {255,255,255}, {255,255,255}, {128,0,128}, {128,0,128}, {255,0,0}, {0,255,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,128}, {0,0,255}, {255,255,255}, {0,255,0}, {0,255,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {192,192,192}, {255,0,255}, {0,255,255}, {128,128,0}, {255,0,255}, {128,0,128}, {128,0,128}, {255,0,0}, {128,0,128}, {0,255,255}, {0,255,255}, {0,255,255}, {0,255,255}, {0,0,255}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,255}, {0,0,128}, {0,255,0}, {0,255,0}, {128,128,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,255}, {0,0,128}, {255,0,0}, {0,255,0}, {255,0,0}, {255,0,0}, {0,128,128}, {0,255,0},
{0,128,128}, {255,255,255}, {255,255,0}, {255,255,255}, {255,0,0}, {0,128,128}, {255,255,0}, {0,0,255}, {0,255,255}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,255,255}
};
// Define Neopixel
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Define Switch
int switchPin = D3;
int buttonPin = D2;
// int buttonState = LOW;
int switchState = LOW;
//Initiate millis fade calculator & fadeOut
unsigned long lastFade = 0;
int fadeOut = 255;
void setup() {
//set up Neopixel
pinMode( switchPin , INPUT_PULLUP); // sets pin as input
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//set up Particle variables
Particle.variable("Genre",genreToFind);
Particle.variable("R",r);
Particle.variable("G",g);
Particle.variable("B",b);
Particle.variable("Artist",artistID);
Particle.variable("Root Genre",genreSimplified);
Particle.variable("Switch",buttonState);
Particle.variable("TrackID", trackID); // establish variable to hold value of Track ID received from get_currently_playing_track webhook
Particle.variable("TrackAddedToPlaylist", track_success_cd);
// Particle.variable("TrackAddedToPlaylist", track_success_cd);
Particle.variable("ButtonState", digitalRead( buttonPin ));
Serial.print("Setup Complete");
//Subscribe functions
// Particle.subscribe("hook-response/get_recent_track", get_artistID);
Particle.subscribe("hook-response/get_currently_playing_artist", get_artistID);
Particle.subscribe("hook-response/get_genre", get_genreName);
Particle.subscribe("hook-response/add_song_to_playlist", add_to_playlist);
Particle.subscribe("hook-response/get_currently_playing_track", get_track_id);
for(int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, 255,255,255); // set a color
// delay(100);
strip.show();
}
delay(30000);
}
void loop() {
unsigned long now = millis();
int switchState = digitalRead( switchPin );
int buttonState = digitalRead( buttonPin );
if( switchState == LOW){
if (GlowState == 0) {
startupGlow();
GlowState = 1;
}
if (buttonState == LOW) { //Feature 2 send song to playlist
getCurrentlyPlaying();
delay(3000);
if(trackID!="") {
// delay(3000);
addSongToPlaylist();
successPulse(r, g, b);
Particle.publish("AddSongSuccess");
}
}
else { //Feature 1 grab song and show light
if ((now - lastCheckTime) >= 10000){
getGenre2();
lightShow();
lastCheckTime = now;
}
}
}
else {
pixelOff();
// GlowState = 0;
}
}
void get_artistID(const char *event, const char *data) {
artistID = data;
}
void get_genreName(const char *event, const char *data) {
genreToFind = data;
}
void get_track_id(const char *event, const char *data) {
trackID = spotify + data;
}
void add_to_playlist(const char *event, const char *data) {
track_success_cd = data;
}
void lightShow() {
// find the ID of a genre
int indexOfGenre = -1;
// loop over each item
for( int i = 0; i < numGenres; i++ ){
String currentGenre = genres[ i ];
if( genreToFind.indexOf(currentGenre) != -1){
indexOfGenre = i;
r = colors[i][0];
g = colors[i][1];
b = colors[i][2];
for(int y = 0; y < strip.numPixels(); y++){
strip.setPixelColor(y, r,g,b); // set a color
// delay(100);
strip.show();
// strip.show();
}
genreSimplified = currentGenre;
}
}
}
// void getGenre() {
// //initiate webhook get artistID
// Particle.publish("get_recent_track");
// //initiate webhook get genre
// delay(3000);
// Particle.publish("get_genre",artistID);
// }
void getGenre2() {
//initiate webhook get artistID
Particle.publish("get_currently_playing_artist");
//initiate webhook get genre
if(artistID!="") {
// delay(1500);
Particle.publish("get_genre",artistID);
}
}
void lightResponseAddSong() {
for(int a = 0; a < strip.numPixels(); a++){
strip.setPixelColor(a, 255,0,0); // set a color
strip.show();
}
delay(3000);
}
void addSongToPlaylist() {
Particle.publish("add_song_to_playlist", trackID);
}
void getCurrentlyPlaying() {
Particle.publish("get_currently_playing_track");
}
void pixelOff() {
for(int x = 0; x < strip.numPixels(); x++){
strip.setPixelColor(x,0,0,0); // set a color
strip.show();
}
}
void startupLED(int r, int g, int b, int speed) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, r, g, b);
strip.show();
delay(speed);
}
}
void startupGlow() {
startupLED(0, 150, 150, 50); //Initiate Startup Sequence
startupLED(0, 50, 255, 50);
lastFade = 0;
fadeOut = 255;
for (int fadeOut = 255; fadeOut > 0; fadeOut--) {
for (int i = 0; i < strip.numPixels(); i++) {
//Pulse to green
strip.setPixelColor(i, 255 - fadeOut, max(50, 255 - fadeOut), 255);
strip.show();
}
delay(1);
}
}
void successPulse(int r, int g, int b) {
lastFade = 0;
fadeOut = 255;
for (int fadeOut = 255; fadeOut > 0; fadeOut--) {
for (int i = 0; i < strip.numPixels(); i++) {
//Pulse to green
strip.setPixelColor(i, max(0, r - (255 - fadeOut)), min(255, g + (255 - fadeOut)), max(0, b - (255 - fadeOut)));
strip.show();
}
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .