Back to Parent

The Bus Stop Sign code
// Bus Stop Sign - version 4.0

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

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 30
#define PIXEL_PIN D5
#define PIXEL_TYPE WS2812B

// Define color codes
#define WHITE 150,150,150
#define CYAN 10,150,70
#define PEACH 200,50,5

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

int waitTime = 25; // number of the delay time
int i; // number of the LED

// The stepping frequency of team A (top)
int top_brightness = 0; 
// The stepping frequency of team B (bottom)
int bottom_brightness = 0;

void setup() {
    
    // initiate the LED strip
    strip.begin();
    strip.show();
    
    // for testing and debugging - set the progress data of team A
    Particle.function( "Set_Top_Brightness", set_top_brightness ); // top
    // for testing and debugging - set the progress data of team B
    Particle.function( "Set_Bottom_Brightness", set_bottom_brightness ); // bottom
    
    // listen to the progress data of team A from the tiles
    Particle.subscribe( "SpringPandaIsCallingMe", handleSharedEventTop ); // top
    // listen to the progress data of team B from the tiles
    Particle.subscribe( "AutumnPandaIsCallingMe", handleSharedEventBottom ); // bottom
}

void loop() {
    
    // if team A or team B wins, display the running effect
    if( top_brightness == 255 || bottom_brightness == 255 )
    {
        if( top_brightness == 255 )
        {
            show_all( PEACH, 25 );
        }
        else
        {
            show_all( CYAN, 25 );
        }
    }
    // if no team has won yet
    else
    {
        // if no one has stepped on the tile
        if( top_brightness == 0 && bottom_brightness == 0 )
        {
            show_prepare( WHITE, CYAN, PEACH, 125 );
        }
        // if someone has stepped on the tile
        else
        {
            // if only team A has stepped on the tile
            if( top_brightness > 0 && bottom_brightness == 0 )
            {
                show_top( CYAN );
                show_bottom( WHITE );
            }
            
            // if only team B has stepped on the tile
            if( bottom_brightness > 0 && top_brightness == 0 )
            {
                show_top( WHITE );
                show_bottom( PEACH );
            }
            
            // if both team has started playing
            if( top_brightness > 0 && bottom_brightness > 0 )
            {
                show_top( CYAN );
                show_bottom( PEACH );
            }
        }
    }
}

// make the top part of the bus stop sign display the progress of team A
void show_top( int R, int G, int B ) {
    
    int j = map( top_brightness, 0, 255, 0, 15 );
    
    for( i = 0; i < j; i++ )
    {
        strip.setPixelColor( 0, R, G, B );
        strip.setPixelColor( i, R, G, B );
        strip.show();
    }
    for( i = j; i < 15; i++ )
    {
        strip.setPixelColor( i, 150, 150, 150 );
        strip.show();
    }
}

// make the bottom part of the bus stop sign display the progress of team B
void show_bottom( int R, int G, int B ) {

    int j = map( bottom_brightness, 0, 255, 15, 30 );
    
    for( i = 15; i < j; i++ )
    {
        strip.setPixelColor( 15, R, G, B );
        strip.setPixelColor( i, R, G, B );
        strip.show();
    }
    for( i = j; i < 30; i++ )
    {
        strip.setPixelColor( i, 150, 150, 150 );
        strip.show();
    }
}

// before any team has started playing, display (running effect) cyan and peach color pixel on the top and bottom part of the bus stop sign
void show_prepare( int R, int G, int B, int R1, int G1, int B1, int R2, int G2, int B2, int waitTime )
{
    for( i = 0; i <= 15; i++ )
    {
        strip.setPixelColor( i - 1, R, G, B );
        strip.setPixelColor( i, R1, G1, B1 );
        
        strip.setPixelColor( i + 14, R, G, B );
        strip.setPixelColor( i + 15, R2, G2, B2 );
        
        strip.show();
        delay( waitTime );
    }
}

// if team A or team B wins, display the running effect on the bus stop sign, showing the color of the winning team
void show_all( int R, int G, int B, int waitTime )
{
    for( i = 0; i < 30; i++ )
    {
        strip.setPixelColor( i, R, G, B );
        strip.show();
        delay( waitTime );
    }
    
    for( i = 0; i < 30; i++ )
    {
        strip.setPixelColor( i, 0, 0, 0 );
        strip.show();
        delay( waitTime );
    }
}

// for testing and debugging - set the progress data of team A
int set_top_brightness( String arg )
{
    top_brightness = arg.toInt();
    
    return top_brightness;
}

// for testing and debugging - set the progress data of team B
int set_bottom_brightness( String arg )
{
    bottom_brightness = arg.toInt();
    
    return bottom_brightness;
}

// get the progress data of team A from the tiles
void handleSharedEventTop(const char *event, const char *data)
{
    set_top_brightness( data );
}

// get the progress data of team B from the tiles
void handleSharedEventBottom(const char *event, const char *data)
{
    set_bottom_brightness( data );
}
Click to Expand

Content Rating

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

0