Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812 // or WS281B or WS2813

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

// Pixel number
uint16_t i;

int colorValue_R = 0; 
int colorValue_G = 0;
int colorValue_B = 0;

// Color off
uint32_t color_Off = strip.Color( 0, 0, 0 ); // unsigned integer of 32 bits
// Color show
uint32_t color_Show = strip.Color( 0, 0, 0 ); // unsigned integer of 32 bits

// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int sensorPin = A5;
int relayPin = D5;

// Create a variable to hold the FSR reading
int sensorReading = 0;
//variable to hold relay status
int relayStatus = LOW;

int letter_weight = 0;

unsigned long timeStartedAt = 0;
unsigned long timeElapsed = 0;

void setup()
{
    Particle.variable( "sensor_reading", &sensorReading, INT );
    Particle.variable( "letter_weight", letter_weight );
    Particle.variable( "time_elapsed", timeElapsed );
    Particle.variable( "relaystatus", relayStatus );
    
    Particle.function( "led_Breathe", led_Breathe );
    
    pinMode(relayPin, INPUT_PULLUP);
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'; clear the memory; important, don't remove
}

void loop(){
    
    // Use analogRead to read the photo cell reading
    // This gives us a value from 0 to 4095
    sensorReading = analogRead( sensorPin );
    
    letter_weight = constrain( map( sensorReading, 0, 100, 0, 100 ), 0, 100 );
    relayStatus = digitalRead( relayPin );
    
    if( relayStatus == HIGH){
        
        timeElapsed = millis() - timeStartedAt;
        
        if( timeStartedAt == 0 ){
        
            timeStartedAt = millis();
            timeElapsed = millis() - timeStartedAt;
        
            colorValue_R = 0;
            colorValue_G = 0;
            colorValue_B = 255;
        }
        
        else if( timeElapsed <= 10*1000 ){
            
            //Blue
            colorValue_R = 0;
            colorValue_G = 0;
            colorValue_B = 255;
        }
        
        else if( 10*1000 < timeElapsed && timeElapsed <= 20*1000 ){
         
            // Pink
            colorValue_R = 255;
            colorValue_G = 20;
            colorValue_B = 147;
        }
        
        else if( timeElapsed > 20*1000 ){
         
            // Yellow
            colorValue_R = 255;
            colorValue_G = 255;
            colorValue_B = 0;
        }
        
        led_Breathe("");
    }

    else{
        
        timeStartedAt = 0;
        timeElapsed = 0;
    }
    
    // wait 1/20th of a second and then loop
    delay(50);
}

int led_Breathe(String arg){
    
    // make LEDs breathe
    for( int b = 0; b < 255; b++ ){
        
        for( int i = 0; i < strip.numPixels(); i++ ){
    
            int colorValue_R_new = colorValue_R * b/255 * letter_weight/100.0;
            int colorValue_G_new = colorValue_G * b/255 * letter_weight/100.0;
            int colorValue_B_new = colorValue_B * b/255 * letter_weight/100.0;

            color_Show = strip.Color( colorValue_R_new, colorValue_G_new, colorValue_B_new );
            strip.setPixelColor( i, color_Show );
            strip.show();
        }
        delay(1);
    }
    for( int b = 255; b >= 0; b-- ){
        
        for( int i = 0; i < strip.numPixels(); i++ ){
            
            int colorValue_R_new = colorValue_R * b/255 * letter_weight/100.0;
            int colorValue_G_new = colorValue_G * b/255 * letter_weight/100.0;
            int colorValue_B_new = colorValue_B * b/255 * letter_weight/100.0;
            
            color_Show = strip.Color( colorValue_R_new, colorValue_G_new, colorValue_B_new );
            strip.setPixelColor( i, color_Show );
            strip.show();
        }
        delay(1);
    }
}
Click to Expand

Content Rating

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

0