Photon Twitter with Neopixel

Trying to make my photon particle pull from my twitter and flash a Neopixel based on the hashtag in my tweet. Right now, it’s turning each LED on one by one instead of flashing. I’ve been trying to figure out how to use a loop with the Spark.functio but from what I understand I need the ‘int’ for each tweet function. (Correct me if I’m wrong please!)

Anyone know what I can do?

#include "neopixel.h"
#include <math.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE

#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int pixel_position = 0;
int blinkNum = 10;
int blinkSpeed = 10;


void setup() {
  Serial.begin( 9600 );
  strip.begin();
  strip.show(); //Initialize all pixels to 'off'
  Spark.function("sad", sadtweet);
  Spark.function("mad", madtweet);
  Spark.function("glad", gladtweet);
  Spark.function("off", offtweet);
}

int sadtweet(String cmd) {
    for(int j = 0; j < blinkNum*2; j++) {
      if(j % 2 == 0) {
        for(int i = 0; i < strip.numPixels(); i++){
          strip.setPixelColor(i, 0, 0, 255);
          strip.show();
          delay(1000/blinkSpeed);
        }
      }
    }
  }


int madtweet(String cmd) {
  for(int j = 0; j < blinkNum*2; j++) {
    if(j % 2 == 0) {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 255, 0, 0);
        strip.show();
        delay(1000/blinkSpeed);
      }
    }
  }
}

int gladtweet(String cmd){
  for(int j = 0; j < blinkNum*2; j++) {
    if(j % 2 == 0) {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 255, 255, 50);
        strip.show();
        delay(1000/blinkSpeed);
      }
    }
  }
}

int offtweet(String cmd){
  strip.Color(0,0,0); // Blue
}

First set all pixels, then do strip.show() and delay. So, the code shoud be (untested, have no way to even compile it here):

int sadtweet(String cmd) {
    for(int j = 0; j < blinkNum*2; j++) {
      // switched order of if() and for() for less code
      for(int i = 0; i < strip.numPixels(); i++){
        if(j % 2 == 0) {
            strip.setPixelColor(i, 0, 0, 255);
          } else {
            strip.setPixelColor(i, 0, 0, 0); // NOTE: added this command to set other part of blink
          }
      }
      // all pixels are set, now send it to he strip and wait for next blink
      strip.show();
      delay(1000/blinkSpeed);
    }
 }

// other *tweet functions are same, just different blink color

int offtweet(String cmd) {
  // you need to set all pixels to blach (and actually set it) and save it
  for(int i = 0; i < strip.numPixels(); i++){
    strip.setPixelColor(i, 0, 0, 0);
  }
}

Don’t use delays in a Particle.function() callback (Spark is deprecated)
Call strip.show() only when done with updating all LEDs

You should also not do the blinking in the callback.
Callbacks should be short and leave quickly - and they have to return an int.
You could also use one function and just check the cmd value for “sad”, “mad”, “glad” or “off”.


@lami beat me to this one

Oh, missed they are particle function callbacks, good point :smile: