Back to Parent

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

//set up for NeoPixel
#define PIXEL_COUNT 7
#define PIXEL_TYPE WS2812
#define PIXELR1_PIN A1
#define PIXELR2_PIN A5
#define PIXELB1_PIN A3
#define PIXELB2_PIN A4
Adafruit_NeoPixel stripR1 = Adafruit_NeoPixel(PIXEL_COUNT, PIXELR1_PIN, PIXEL_TYPE);
Adafruit_NeoPixel stripR2 = Adafruit_NeoPixel(PIXEL_COUNT, PIXELR2_PIN, PIXEL_TYPE);
Adafruit_NeoPixel stripB1 = Adafruit_NeoPixel(PIXEL_COUNT, PIXELB1_PIN, PIXEL_TYPE);
Adafruit_NeoPixel stripB2 = Adafruit_NeoPixel(PIXEL_COUNT, PIXELB2_PIN, PIXEL_TYPE);

//define input pins for stepping tiles
int buttonPinR1L = D0;
int buttonPinR1R = D1;
int buttonPinB1L = D2;
int buttonPinB1R = D3;
int buttonPinR2L = D4;
int buttonPinR2R = D5;
int buttonPinB2L = D6;
int buttonPinB2R = D7;

long timeStartedAt = 0;
long timeElapsed = 0;
long now = 0;
long lastRecord = 0;

int frequencyR = 0;
int frequencyB = 0;

int stepBrightnessR = 0;
int stepBrightnessB = 0;

int frequencyR1LA = 0;
int frequencyR1LB = 0;
int frequencyR1RA = 0;
int frequencyR1RB = 0;

int frequencyB1LA = 0;
int frequencyB1LB = 0;
int frequencyB1RA = 0;
int frequencyB1RB = 0;

int frequencyR2LA = 0;
int frequencyR2LB = 0;
int frequencyR2RA = 0;
int frequencyR2RB = 0;

int frequencyB2LA = 0;
int frequencyB2LB = 0;
int frequencyB2RA = 0;
int frequencyB2RB = 0;

int frequencyAddA = 0;
int frequencyAddB = 0;

long lastPublishedAt = 0;

//the time delay before we should publish a new event from this device
int publishAfter = 100;

uint32_t b = stripB1.Color(10,150,70); //Cyan
uint32_t r = stripR1.Color(200, 50, 5); //Peach

void setup() {
    //setup input pins for stepping tiles
    pinMode( buttonPinR1L , INPUT_PULLUP);
    pinMode( buttonPinR1R , INPUT_PULLUP);
    pinMode( buttonPinB1L , INPUT_PULLUP);
    pinMode( buttonPinB1R , INPUT_PULLUP);
    pinMode( buttonPinR2L , INPUT_PULLUP); 
    pinMode( buttonPinR2R , INPUT_PULLUP);
    pinMode( buttonPinB2L , INPUT_PULLUP);
    pinMode( buttonPinB2R , INPUT_PULLUP);

    //setup NeoPixels for tile signal lights
    stripR1.begin();
    stripR1.show();
    stripR2.begin();
    stripR2.show();
    stripB1.begin();
    stripB1.show();
    stripB2.begin();
    stripR2.show();
}

void loop() {
    //count the steps
    howFrequent();
    
    //reset the game if not stepping for 10 seconds
    if(lastRecord==0){
        lastRecord = millis();
        frequencyAddA = frequencyR + frequencyB;
    }
    else if(now - lastRecord >= 10*1000){
        frequencyAddB = frequencyR + frequencyB;
        if (frequencyAddA == frequencyAddB){
            frequencyR = 0;
            frequencyB = 0;
            lastRecord = millis();
        }
        else{
            frequencyAddA = frequencyR + frequencyB;
            lastRecord = millis();
        }
    }
    
    //setup each team's NeoPixels for tile signal lights 
    for( int i = 0; i < stripR1.numPixels(); i++ ){
        stripR1.setPixelColor(i, r);
        stripR2.setPixelColor(i, r);
        stripB1.setPixelColor(i, b);
        stripB2.setPixelColor(i, b);
    }
    stripR1.show();
    stripR2.show();
    stripB1.show();
    stripB2.show();
    
    //count time
    now = millis();
}

void howFrequent(){
    if( timeStartedAt == 0 ){
        timeStartedAt = millis();
    }
        
    timeElapsed = millis() - timeStartedAt;
    
    //count the amount of steps using 3 seconds as a unit
    if( timeElapsed <= 3*1000 ){
        
	//Count frequency for team Red
        frequencyR1LA = digitalRead( buttonPinR1L );
        frequencyR1RA = digitalRead( buttonPinR1R );
        frequencyR2LA = digitalRead( buttonPinR2L );
        frequencyR2RA = digitalRead( buttonPinR2R );
        
        delay( 100 );
        
        frequencyR1LB = digitalRead( buttonPinR1L );
        frequencyR1RB = digitalRead( buttonPinR1R );
        frequencyR2LB = digitalRead( buttonPinR2L );
        frequencyR2RB = digitalRead( buttonPinR2R );
        
        if( frequencyR1LA > frequencyR1LB){
            frequencyR++;
        }
        if( frequencyR1RA > frequencyR1RB){
            frequencyR++;
        }
        if( frequencyR2LA > frequencyR2LB){
            frequencyR++;
        }
        if( frequencyR2RA > frequencyR2RB){
            frequencyR++;
        }
        
	//Counting frequency for team Blue
        frequencyB1LA = digitalRead( buttonPinB1L );
        frequencyB1RA = digitalRead( buttonPinB1R );
	    frequencyB2LA = digitalRead( buttonPinB2L );
        frequencyB2RA = digitalRead( buttonPinB2R );        
    
        delay( 100 );
        
        frequencyB1LB = digitalRead( buttonPinB1L );
        frequencyB1RB = digitalRead( buttonPinB1R );
        frequencyB2LB = digitalRead( buttonPinB2L );
        frequencyB2RB = digitalRead( buttonPinB2R );
        
        if( frequencyB1LA > frequencyB1LB){
            frequencyB++;
        }
        if( frequencyB1RA > frequencyB1RB){
            frequencyB++;
        }
        if( frequencyB2LA > frequencyB2LB){
            frequencyB++;
        }
        if( frequencyB2RA > frequencyB2RB){
            frequencyB++;
        }
    }
    
    //map the amount of steps to brightness/light behavior
    else
    {
        timeStartedAt = 0;
        timeElapsed = 0;
        if (frequencyR == 0 && frequencyB == 0){
            stepBrightnessR = 0;
            stepBrightnessB = 0;
        }
        else{
            stepBrightnessR = map(frequencyR, 0, 20, 0,270);
            stepBrightnessB = map(frequencyB, 0, 20, 0,270);
        }
        
        if (stepBrightnessR >255){
            stepBrightnessR = 255;
        }
        if (stepBrightnessB >255){
            stepBrightnessB = 255;
        }
        
        //publish the value to The Bus Sign
        publishRedEvent();
        delay(110);
        publishBlueEvent();
        
        //change all the tile signal lights to the winning team's color for 10 seconds
        if (stepBrightnessR == 255){
            stepBrightnessR = 0;
            stepBrightnessB = 0;
            frequencyR = 0;
            frequencyB = 0;
            uint32_t c = stripR2.Color(200, 50, 5); //Peach
 
            for( int i = 0; i < stripR2.numPixels(); i++ ){
                stripR1.setPixelColor(i, c);
                stripR2.setPixelColor(i, c);
                stripB1.setPixelColor(i, c);
                stripB2.setPixelColor(i, c);
            }
            stripR1.show();
            stripR2.show();
            stripB1.show();
            stripB2.show();
            delay(10000);
            
            c = stripR2.Color(0,0,0); // Off
 
            for( int i = 0; i < stripR2.numPixels(); i++ ){
                stripR1.setPixelColor(i, c);
                stripR2.setPixelColor(i, c);
                stripB1.setPixelColor(i, c);
                stripB2.setPixelColor(i, c);
            }
            stripR1.show();
            stripR2.show();
            stripB1.show();
            stripB2.show();
        }
        else if (stepBrightnessB ==255){
           stepBrightnessR = 0;
           stepBrightnessB = 0;
           frequencyR = 0;
           frequencyB = 0;
            uint32_t c = stripR2.Color(10,150,70); // Cyan
 
            for( int i = 0; i < stripR2.numPixels(); i++ ){
                stripR1.setPixelColor(i, c);
                stripR2.setPixelColor(i, c);
                stripB1.setPixelColor(i, c);
                stripB2.setPixelColor(i, c);
            }
            stripR1.show();
            stripR2.show();
            stripB1.show();
            stripB2.show();
            delay(10000);
            
            c = stripR2.Color(0,0,0); // Off
 
            for( int i = 0; i < stripR2.numPixels(); i++ ){
                stripR1.setPixelColor(i, c);
                stripR2.setPixelColor(i, c);
                stripB1.setPixelColor(i, c);
                stripB2.setPixelColor(i, c);
            }
            stripR1.show();
            stripR2.show();
            stripB1.show();
            stripB2.show();
        }
    }
}

//publish the value for red team to The Bus Sign
void publishRedEvent()
{
  // check that it's been 0.1 secondds since the last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "SpringPandaIsCallingMe" + System.deviceID();
      Particle.publish( eventName, String(stepBrightnessR) );

      //capture publish time
      lastPublishedAt = millis();
  }
}

//publish the value for blue team to The Bus Sign
void publishBlueEvent()
{
  // check that it's been 0.1 secondds since the last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "AutumnPandaIsCallingMe" + System.deviceID();
      Particle.publish( eventName, String(stepBrightnessB) );

      //capture publish time
      lastPublishedAt = millis();
  }
}
Click to Expand

Content Rating

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

0