Kaiya Kedzierski
Published © GPL3+

Lane Tech HS- PCL compscult "the light of social media"

A collaboration between the physical computing lab and sculpture classes.

IntermediateShowcase (no instructions)5 hours13
Lane Tech HS- PCL compscult "the light of social media"

Things used in this project

Story

Read more

Schematics

Schematics

Code

Project Code!

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

SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_INFO);


// IMPORTANT: Set pixel COUNT, PIN and TYPE
#if (PLATFORM_ID == 32)
#define PIXEL_PIN SPI1
#else
#define PIXEL_PIN D2
#endif

#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

double stockPrice = 0;
double previousPrice = 655.4;



const int stepsPerRevolution = 2048;

// FIXED: must use Dx notation on Photon
#define IN1 D3
#define IN2 D4
#define IN3 D5
#define IN4 D6

Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);


bool curtainLifted = false;
bool curtainStateRestored = false;



void setup() {
    Serial.begin(9600);
    while (Particle.disconnected());

    strip.begin();
    strip.show();

    myStepper.setSpeed(15);

    Serial.println("Particle connected to cloud!");
    
   
     Particle.subscribe("hook-response/getCurtainState", gotCurtainState, MY_DEVICES);
    Particle.publish("getCurtainState", "", PRIVATE);
    

    Particle.subscribe("hook-response/stockPrice", extractInfo, MY_DEVICES);
    Particle.publish("stockPrice", "", PRIVATE);
    
   
    
    
    
    
    // Has to be a PWM capable pin; Go look at the pinout diagram for a Photon 2!
    

    
}
unsigned long lastFetchTime = 0;
const unsigned long fetchInterval = 60000; // 1 minute

void loop() {
    
   if (!curtainStateRestored) {
    return;
   }

// Fetch stock every 60 seconds
if (millis() - lastFetchTime > fetchInterval) 
{
    Serial.println("Requesting stock price...");
    Particle.publish("stockPrice", "", PRIVATE);
    lastFetchTime = millis();
}

// Only react if we actually received a new value
    
if (stockPrice != previousPrice) {
        if (stockPrice > previousPrice && !curtainLifted) 
        {

            Serial.println("Stock UP - lifting curtain");
            
            curtainLifted = true;
            myStepper.step(21000); // small controlled movement
            for(int i = 0; i < 16; i++)
            {
                strip.setColor(i, 255, 255, 255);
                strip.show();
            }
            Serial.println("LED on");
             
            Particle.publish("curtainState", 
            String::format("{\"curtainLifted\":%s}", curtainLifted ? "true" : "false"),
             PRIVATE);
             
             
            
            
            

        } 

        else if (stockPrice < previousPrice && curtainLifted) 
        {

            Serial.println("Stock DOWN - lowering curtain");
            myStepper.step(-18000);
            curtainLifted = false;

            
            for(int i = 0; i < 16; i++)
            {
                strip.setColor(i, 0, 0, 0);
                strip.show();
            }
            Serial.println("LED off");
            Particle.publish("curtainState", 
            String::format("{\"curtainLifted\":%s}", curtainLifted ? "true" : "false"),
            PRIVATE); 
             
            
        }
        
        else 
            {
                Serial.print("curtain already reacted");
               
            }

        previousPrice = stockPrice; // IMPORTANT
    }

    //Serial.println("stock price hasnt changed");
}


void extractInfo(const char *event, const char *data) {
    Serial.println("Data Being pulled:");
    Serial.println(data);

    stockPrice = atof(data);
   
    Serial.print("Stock Price: ");
    Serial.println(stockPrice);
}

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

void gotCurtainState(const char *event, const char *data) {
    curtainLifted = (strcmp(data, "true") == 0);
    curtainStateRestored = true;
    Serial.printlnf("Restored curtain state: %s", curtainLifted ? "UP" : "DOWN");
    // Turn LEDs on if curtain is lifted at startup
    if (curtainLifted) {
        for (int i = 0; i < 16; i++) {
            strip.setColor(i, 255, 255, 255);
        }
        strip.show();
        Serial.println("LED restored to ON state");
    }
}

Code

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

SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_INFO);


// IMPORTANT: Set pixel COUNT, PIN and TYPE
#if (PLATFORM_ID == 32)
#define PIXEL_PIN SPI1
#else
#define PIXEL_PIN D2
#endif

#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

double stockPrice = 0;
double previousPrice = 655.4;



const int stepsPerRevolution = 2048;

// FIXED: must use Dx notation on Photon
#define IN1 D3
#define IN2 D4
#define IN3 D5
#define IN4 D6

Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);


bool curtainLifted = false;
bool curtainStateRestored = false;



void setup() {
    Serial.begin(9600);
    while (Particle.disconnected());

    strip.begin();
    strip.show();

    myStepper.setSpeed(15);

    Serial.println("Particle connected to cloud!");
    
   
     Particle.subscribe("hook-response/getCurtainState", gotCurtainState, MY_DEVICES);
    Particle.publish("getCurtainState", "", PRIVATE);
    

    Particle.subscribe("hook-response/stockPrice", extractInfo, MY_DEVICES);
    Particle.publish("stockPrice", "", PRIVATE);
    
   
    
    
    
    
    // Has to be a PWM capable pin; Go look at the pinout diagram for a Photon 2!
    

    
}
unsigned long lastFetchTime = 0;
const unsigned long fetchInterval = 60000; // 1 minute

void loop() {
    
   if (!curtainStateRestored) {
    return;
   }

// Fetch stock every 60 seconds
if (millis() - lastFetchTime > fetchInterval) 
{
    Serial.println("Requesting stock price...");
    Particle.publish("stockPrice", "", PRIVATE);
    lastFetchTime = millis();
}

// Only react if we actually received a new value
    
if (stockPrice != previousPrice) {
        if (stockPrice > previousPrice && !curtainLifted) 
        {

            Serial.println("Stock UP - lifting curtain");
            
            curtainLifted = true;
            myStepper.step(21000); // small controlled movement
            for(int i = 0; i < 16; i++)
            {
                strip.setColor(i, 255, 255, 255);
                strip.show();
            }
            Serial.println("LED on");
             
            Particle.publish("curtainState", 
            String::format("{\"curtainLifted\":%s}", curtainLifted ? "true" : "false"),
             PRIVATE);
             
             
            
            
            

        } 

        else if (stockPrice < previousPrice && curtainLifted) 
        {

            Serial.println("Stock DOWN - lowering curtain");
            myStepper.step(-18000);
            curtainLifted = false;

            
            for(int i = 0; i < 16; i++)
            {
                strip.setColor(i, 0, 0, 0);
                strip.show();
            }
            Serial.println("LED off");
            Particle.publish("curtainState", 
            String::format("{\"curtainLifted\":%s}", curtainLifted ? "true" : "false"),
            PRIVATE); 
             
            
        }
        
        else 
            {
                Serial.print("curtain already reacted");
               
            }

        previousPrice = stockPrice; // IMPORTANT
    }

    //Serial.println("stock price hasnt changed");
}


void extractInfo(const char *event, const char *data) {
    Serial.println("Data Being pulled:");
    Serial.println(data);

    stockPrice = atof(data);
   
    Serial.print("Stock Price: ");
    Serial.println(stockPrice);
}

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

void gotCurtainState(const char *event, const char *data) {
    curtainLifted = (strcmp(data, "true") == 0);
    curtainStateRestored = true;
    Serial.printlnf("Restored curtain state: %s", curtainLifted ? "UP" : "DOWN");
    // Turn LEDs on if curtain is lifted at startup
    if (curtainLifted) {
        for (int i = 0; i < 16; i++) {
            strip.setColor(i, 255, 255, 255);
        }
        strip.show();
        Serial.println("LED restored to ON state");
    }
}

Credits

Kaiya Kedzierski
2 projects • 0 followers

Comments