Back to Parent

#include <neopixel.h>

#include <stdio.h>
#include <stdlib.h>


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

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

// Message 
char *message;
int mug2 = 0;

// Local Pressure sensing
int FSR_PIN = A0; 
int pressure_threshold = 250;
int pressure_reading = 0;

// Local lights
int LED = D7;

void setup() {
    Particle.publish("MUGa", "Mug 1 test online");
    Particle.subscribe("MUGb", handler);
    Particle.variable("mug2", pressure_reading);
    
    pinMode(FSR_PIN, INPUT);
    pinMode(LED, OUTPUT);
    
    strip.begin();
    strip.show();
}

void loop() {
    // local pressure reading
    pressure_reading = analogRead(FSR_PIN);
    
    char valtext[1];
    sprintf(valtext,"%i", pressure_reading);
    Particle.publish("mug 1 weight",valtext);
    
    if (pressure_reading > pressure_threshold){
        Particle.publish("MUGa", "1");
    }
    else {
        Particle.publish("MUGa", "0");
    }
    
    // Other mug pressure reading
    if (mug2 == 1){
        digitalWrite(LED,HIGH);
        neo_on();
    }
    else {
        neo_off();
        digitalWrite(LED,LOW);
    }
    delay(2000);
}

void handler(const char *event, const char *data){
    if(data){
        message = const_cast<char*> (data);
        mug2 = atoi(message);
    }
}

void neo_on(){
    for(int i = 0 ; i < strip.numPixels(); i++  ){
        if( i % 3 == 0 ){
            strip.setPixelColor( i, strip.Color(210,105,30) );
        }
        else if( i % 3 == 1 ){
        strip.setPixelColor( i, strip.Color(0,128,0) );
        }
        else{
            strip.setPixelColor( i, strip.Color(255,104,180) );
            }
        }
        strip.show();
}

void neo_off(){
    uint32_t c = strip.Color(0, 0, 0); 
    for( int i = 0; i < strip.numPixels(); i++ ) {
        strip.setPixelColor(i, c); // set a color 
        strip.show();
    }    
}
Click to Expand

Content Rating

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

0