Back to Parent

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

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 24
#define PIXEL_TYPE WS2812

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

int buttonPin = D0;    // RED pin of the LED to PWM pin **D0**
int toggle = 0;
int offPin = D1;
int offState = 1;
int isOn = TRUE;

int transitionTime = 5000; // Tranition Time in ms

void setup(){
  //Particle.variable("brightness", ledBrightness);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  pinMode( buttonPin , INPUT_PULLUP);
  pinMode( PIXEL_PIN, OUTPUT );
  pinMode( offPin , INPUT_PULLUP );

  Particle.subscribe("initiateSunrise", myHandlerSunrise);
  Particle.subscribe("initiateSunset", myHandlerSunset);
  Particle.subscribe("turnOff", myHandlerOff);
}

void loop(){

  int buttonState = digitalRead(buttonPin);
  int offPinState = digitalRead(offPin);

    if( buttonState == LOW )
    {
      if( toggle == 0){
        colorWipe(strip.Color(255, 210, 25), 40);
        toggle = 1;
        Particle.publish("sunrise complete");
      }else{
          colorWipe(strip.Color(30, 0, 0), 40);
          toggle = 0;
          Particle.publish("sunset complete");
        }
    }else{
  }
}


void myHandlerSunset(const char *event, const char *data){
  rainbow(300);
}

void myHandlerSunrise(const char *event, const char *data){
  reverseRainbow(300);
}

void myHandlerOff(const char *event, const char *data){
  colorWipe(strip.Color(0, 0, 0), 25);
}

void rainbow(int wait) {
  uint16_t i, j;

  for(j=50; j<85; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((j) & 255));
    }
    strip.show();
    delay(wait);
  }
  Particle.publish("sunset complete");
}

void reverseRainbow(int wait) {
  uint16_t i, j;

  for(j=85; j>50; j--) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((j) & 255));
    }
    strip.show();
    delay(wait);
  }
  Particle.publish("sunrise complete");
}

void colorWipe(uint32_t c, int wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void colorAll(uint32_t c, uint8_t wait) {
  uint16_t i;

  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(wait);
}

/**
 * Scale a value returned from a trig function to a byte value.
 * [-1, +1] -> [0, 254]
 * Note that we ignore the possible value of 255, for efficiency,
 * and because nobody will be able to differentiate between the
 * brightness levels of 254 and 255.
 */
byte trigScale(float val) {
  val += 1.0; // move range to [0.0, 2.0]
  val *= 127.0; // move range to [0.0, 254.0]

  return int(val) & 255;
}

/**
 * Map an integer so that [0, striplength] -> [0, 2PI]
 */
float map2PI(int i) {
  return M_PI*2.0*float(i) / float(strip.numPixels());
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout, then wait (ms)
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) { // 1 cycle of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0