Back to Parent

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

int potPin = A4; //Initialize potentiometer and neopixel variables
int neoPin = D6;

#define PIXEL_PIN D6 //Declare neopixel variables
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812B

// This value will store the last time we published an event
long lastPublishedAt = 0;// this is the time delay before we should publish a new event from this device
int publishAfter = 2000;// this is the time delay before we should publish a new event
int otherDevicePos = 0; // from this device

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

void setup() 
{
    pinMode (potPin, INPUT_PULLDOWN);
    strip.begin();
    Particle.subscribe("NicoleTroyGaurav", color); //Custom event name. Calls the color function
    Particle.variable ("Position", otherDevicePos); //Publish potentiometer position to monitor
}

void loop() 
{
    int position = analogRead (potPin); //Read position of the poteiometer
    String value = String (position); //Send the poisition as a string
    
    if( lastPublishedAt + publishAfter < millis() ) // check that it's been 10 secondds since our last publish
    {
        String eventName = "NicoleTroyGaurav" + System.deviceID(); // then we share it out
        Particle.publish(eventName, value);
        lastPublishedAt = millis();
    }
      
    if (otherDevicePos >= 0 && otherDevicePos < 200)
    {
        turn_off_light();
    }
    
    else if (otherDevicePos >= 200 && otherDevicePos < 1000)
    {
        blue();
    }
    
    else if (otherDevicePos >= 1000 && otherDevicePos < 2000)
    {
        yellow();
    }
    
    else if (otherDevicePos >= 2000 && otherDevicePos < 3000)
    {
        green();
    }
    
    else if (otherDevicePos >= 3000 && otherDevicePos < 3900)
    {
        pink();
    }
    
    else if (otherDevicePos >= 3900)
    {
        int position_self = analogRead (potPin);
        if (position_self < 3900)
        {
            white();
        }
        else if (position_self >= 3900) //Breathing white lights on both, when both are >3700
        {
            white_breathing();
        }
    }
    delay( 50 );
}

void color (const char *event, const char *data)
{
    String num = String (data); 
    String eventName = String(event);
    String deviceID = System.deviceID();
    
    if (eventName.indexOf(deviceID) != -1) //Check to make sure self particle is not activated
    {
        return;
    }
    otherDevicePos = num.toInt(); //Convert string position to int
}

void turn_off_light()
{
    uint32_t c = strip.Color(0, 0, 0, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
}

void blue()
{
    uint32_t c = strip.Color(0, 0, 200, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
}

void yellow()
{
    uint32_t c = strip.Color(200, 200, 0, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
}

void green()
{
    uint32_t c = strip.Color(0, 200, 0, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
}

void pink()
{
    uint32_t c = strip.Color(244, 66, 220, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }

    strip.show();
}

void white()
{
    uint32_t c = strip.Color(200, 200, 200, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
}

float white_breathing()
{
    float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
    Serial.println(val);
    
    uint16_t i;
    uint32_t c = strip.Color(val, val, val);
    
    for(i=0; i< strip.numPixels(); i++) 
    {
        strip.setPixelColor(i, c );
    }
    strip.show();
}
Click to Expand

Content Rating

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

0