Back to Parent

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

int startButtonPin = D5;
int buttonPin = D7;    
int ledPin = D2;       

int counter = 0;
int buttonState;
int startButtonState;

long lastPublishedAt = 0;
int publishAfter = 10000;

bool myState = false; //indicate my ready state
bool yourState = false; //indicate the other person's ready state

#define PIXEL_PIN D2
#define PIXEL_COUNT 14
#define PIXEL_TYPE WS2812

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

void setup() {

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(startButtonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Particle.subscribe(  "gameStart" , handleSharedEvent ); // Subscribe to game starting event
  Particle.subscribe(  "gameEnd" , handleSharedEvent );  // Subscribe to game ending event 
  strip.begin();
}

void loop() {
  startButtonState = digitalRead(startButtonPin);
  buttonState = digitalRead(buttonPin);

  if (startButtonState == LOW) {
      publishStartingEvent();
  }
      
  if (myState == true and yourState == true){
      counts();
      //light effect function
      uint32_t c = strip.Color(0, 0, 255); // Blue
      for(int i = 0; i<14; i++){
        strip.setPixelColor(i, c); // set a color 
        strip.show();
      }
  }
 
      
  if (counter >= 10){
      publishEndingEvent();
      //light effect function 2
        uint32_t c = strip.Color(0, 143, 255); // Blue
        uint32_t b = strip.Color(0, 32, 0); // Blue
        uint32_t a = strip.Color(255, 10, 10); // Blue
         
        for( int i = 0; i < strip.numPixels(); i++ ){
           strip.setPixelColor(i, c); // set a color 
           strip.show();
           delay( 50 );
        }
         
        for( int i = 0; i < strip.numPixels(); i++ ){
           strip.setPixelColor(i, b); // set a color 
           strip.show();
           delay( 50 );
        }
        
        for( int i = 0; i < strip.numPixels(); i++ ){
           strip.setPixelColor(i, a); // set a color 
           strip.show();
           delay( 10 );
        }    
      counter = 0;
  }

}

void counts()
{
   if (buttonState == LOW){
       counter += 1;
       delay(50);
          } 
}

void publishStartingEvent() // Publish function for the starting event 
{

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "gameStart" + System.deviceID();

      // then we share it out
      Particle.publish( eventName, "data goes here" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      // lastPublishedAt = millis();
      myState = true;
  }

}

void publishEndingEvent() // Publish function for the ending event
{

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "gameEnd" + System.deviceID();

      // then we share it out
      Particle.publish( eventName, "data goes here" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      // lastPublishedAt = millis();
      myState = false;
      //LED effects
  }

}

void handleSharedEvent(const char *event, const char *data){ // Only need one handle event function for both events 

    // Let's check the event name
    String eventName = String( event ); // convert to a string object

    // We can use this to check if our event name contains the
    // id of this device

    String deviceID = System.deviceID();

    // device id = 0123456789abcdef
    // event = "diot/2019/paired/0123456789abcdef"

    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }
    
    if(eventName.startsWith("gameStart")){

        yourState = true;

    }
    
    if(eventName.startsWith("gameEnd")){
        
        yourState = false;
    }
 

}
Click to Expand

Content Rating

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

0