Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <math.h>
/*
 * Project NeoPixel
 * Description:
 * Author:
 * Date:
 */
#include "neopixel.h"
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D0
#define PIXEL_COUNT 24
#define PIXEL_TYPE SK6812RGBW

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int brightness =0;
int dir =5;
char val; // Data received from the serial port

// setup() runs once, when the device is first turned on.
void setup(){
  pixels.begin();
  Serial.begin(9600); // Start serial communication at 9600 bps
  Particle.variable("val",val);
}
void loop(){
    
    
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
      
   }
   
    
    if (val == '1') 
    { // If 1 was received
        loopLed();
         
    } else if(val == '2') {
         //ChangeBrightness();
        speak();
        
   }else if(val == '0'){
        ChangeBrightness();
   }else{
        loopLed();
   }
}

void loopLed(){
 for(byte x = 0; x < 20; x++){
    rotatePixels(true);
    delay(50);
  }
  for(byte x = 0; x < 20; x++){
    rotatePixels(false);
    delay(50);
  }
}

void rotatePixels(bool pixColour){
  static char currentPos = PIXEL_COUNT;



  currentPos++;

  // if(pixColour == true){
  //   colour = 0xFF00; // Green
  // }
  uint32_t colour = pixels.Color(50,0,0,0); // Default to Red
  pixels.setPixelColor((currentPos - 1) % PIXEL_COUNT, 0);
  colour = pixels.Color(0,10,10,5); // Default to Red
  pixels.setPixelColor((currentPos + 0) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,20,20,10); // Default to Red
  pixels.setPixelColor((currentPos + 1) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,30,30,15); // Default to Red
  pixels.setPixelColor((currentPos + 2) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,50,50,20); // Default to Red
  pixels.setPixelColor((currentPos + 3) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,30,30,15); // Default to Red
  pixels.setPixelColor((currentPos + 4) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,20,20,10); // Default to Red
  pixels.setPixelColor((currentPos + 5) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,10,10,5); // Default to Red
  pixels.setPixelColor((currentPos + 6) % PIXEL_COUNT, colour);
  colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 8) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 9) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 10) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 11) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 pixels.setPixelColor((currentPos + 12) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 13) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 pixels.setPixelColor((currentPos + 14) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 15) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 pixels.setPixelColor((currentPos + 16) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
  pixels.setPixelColor((currentPos + 17) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 pixels.setPixelColor((currentPos + 18) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
   pixels.setPixelColor((currentPos + 19) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
   pixels.setPixelColor((currentPos + 20) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
   pixels.setPixelColor((currentPos + 21) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 pixels.setPixelColor((currentPos + 22) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
   pixels.setPixelColor((currentPos + 23) % PIXEL_COUNT, colour);
   colour = pixels.Color(0,5,5,5); // Default to Red
 
 
  pixels.show();

}



void ChangeBrightness(){
  float val = (exp(sin(millis()/2000.0*3.14159)) - 0.36787944)*15.0;

  uint32_t c = pixels.Color(0,val,30,5); // Blue

  for( int i = 0; i < pixels.numPixels(); i++ ){
     pixels.setPixelColor(i, c); // set a color

  }
  pixels.show();
  delay( 50 );

}
void speak(){
  float val = (exp(sin(millis()/2000.0*3.14159)) - 0.36787944)* random(10,50);

  uint32_t c = pixels.Color(0,30,val,5); // Blue

  for( int i = 0; i < pixels.numPixels(); i++ ){
     pixels.setPixelColor(i, c); // set a color
  }
  pixels.show();
  delay( 100 );

}
void LoopingRing(){


  for( int i = 0; i < pixels.numPixels(); i++ ){
     uint32_t c = pixels.Color(255,0,0,0); // Blue
     pixels.setPixelColor(i, c); // set a color
  }
  pixels.show();
  delay( 10 );
}
Click to Expand

Content Rating

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

0