Back to Parent

//Date - 02/10/2019  - Small prototype for the concept Pixel Away. Contributors: Ranveer Katyal, Menghan Zhang, and Sijia Li
//Idea: The follwoing code lets a user light up a specific Pixel on a strip of neo-pixel and transmit it to another device 

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 30
#define PIXEL_TYPE WS2812

long lastPublishedAt = 0;

int publishAfter = 10000;

int pushSendButton = D6;
int pushButtonPin = D4;
int oldButtonState = HIGH;
uint16_t i = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
    Particle.subscribe(  "diot2019/PixelAway/" , handleSharedEvent );
    pinMode(pushButtonPin, INPUT_PULLUP);
    pinMode(pushSendButton, INPUT_PULLUP);
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
}

void loop() {
   
   int count = neoPixelCounter();
  String countString = String(count); //send the loction to other devices when the send push button is pressed. 
       if (digitalRead(pushSendButton) == 0){
    publishMyEvent(countString);
    }

    // delay for a bit
    delay(100);
   

}


void publishMyEvent(String Loc)
{
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "diot2019/PixelAway/" + System.deviceID();
      Particle.publish(eventName, Loc);
      lastPublishedAt = millis();
  }

}
void handleSharedEvent(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();
    String Loc = String(data);
    int count = Loc.toInt(); //convert the string data  into Int

    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    } else {
        pixelLit(count); // Lights up the Pixel sent byt the other device
        
    }

}

//The function neoPixelCounter lets the user select the specific pixel they want to light up by the press of push button
//The function returns the pixel locetion to be transmiited to other devices.
int neoPixelCounter(){
    int newButtonState = digitalRead(pushButtonPin);

    uint32_t c = strip.Color(255, 255, 255);
    if (newButtonState != oldButtonState){
            if (newButtonState == LOW ){
                i++; //increase the position counter with press of button 
                //since the neoPixel strip is 30 led long the if condition and the modulo operator lets the user select the pixel in an rotational manner
                if (i>30){
                    strip.setPixelColor(i-1,0,0,0);
                    i = i % 30;
                    newButtonState = HIGH;
                    strip.setPixelColor(i-1,0,0,0);
                    delay(50);
                    strip.setPixelColor(i, c );
            		strip.show();
            		delay( 100 );
                }
                else{
                    newButtonState = HIGH;
                    strip.setPixelColor(i-1,0,0,0); //turns off the previous pixels.
                    delay(50);
                    strip.setPixelColor(i, c );
            		strip.show();
            		delay( 100 );
                }
        }
        delay(100);
    }
    oldButtonState = newButtonState; //prevents the overflow of button press
    return i;
}

void pixelLit(int pixelLoc){
    
    strip.setPixelColor(pixelLoc, 255,0,0 ); //lights up the specific pixel receviced through other device in red color.
    strip.show();
    delay( 100 );
    
}
Ranveer Katyal, Menghan Zhang, and Sijia Li (2019) Click to Expand

Content Rating

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

0