Back to Parent

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

#define x_axis A0
#define y_axis A1
#define z_axis A2
#define PIXEL_PIN D4
#define PIXEL_COUNT 11
#define PIXEL_TYPE WS2812

int x_val;
int y_val;
int z_val;
int steps = 0;
int up = 0;
int down = 0;
long lastPublishedAt = 0;
int publishAfter = 10000;
int ledPos = 0;

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

void setup() {
    
    Particle.subscribe(  "diot2019/steps_count" , handleSharedEvent );
    strip.begin();
    strip.show();
    
    pinMode(x_axis, INPUT);
    pinMode(y_axis, INPUT);
    pinMode(z_axis, INPUT);
    
    Particle.variable("X",&x_val,INT);
    Particle.variable("Y",&y_val,INT);
    Particle.variable("Z",&z_val,INT);
    Particle.variable("Steps",&steps,INT);
    Particle.variable("LED",&ledPos,INT);
}

void loop() {
    
    //up=0;
    //down = 0;
    
    x_val = analogRead(x_axis);
    y_val = analogRead(y_axis);
    z_val = analogRead(z_axis);
    
    if (x_val<1820) {
        up = 1;
        delay(200);
    }
    
    if (x_val>2020) {
        down=1;
        delay(200);
    }
    
    if (up==1&&down==1) {
        steps++;
        up=0;
        down=0;
    }
    
    publishMyEvent();
}


void publishMyEvent() {
    if( lastPublishedAt + publishAfter < millis() ) {
        String eventName = "diot2019/steps_count" + System.deviceID();
        Particle.publish(eventName, String(steps));
        lastPublishedAt = millis();
    }

}

void handleSharedEvent(const char *event, const char *data) {
    String eventName = String(event);
    String dataValue = String(data);
    String deviceID = System.deviceID();
    
    if (eventName.indexOf( deviceID ) != -1) {
        return;
    } else {
        ledPos = (dataValue.toInt()/10)%11;
        lightUp();
    }
    
}

void lightUp() {
    
    uint16_t i;
    uint32_t c = strip.Color(255, 0, 0);
    uint32_t c2 = strip.Color(0, 0, 0);

    for(i=0; i< strip.numPixels(); i++) {
        if (i==ledPos) {
            strip.setPixelColor(i, c);
        } else {
            strip.setPixelColor(i, c2);
        }
		strip.show();
		delay( 100 );
	}
}
Click to Expand

Content Rating

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

0