Back to Parent

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

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D4
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812


/**************************************************************/
/********************   Initialization     ********************/ 
/**************************************************************/

/*
	Ultrasonic Sensor Pins:
	VCC: 5V 
	Trig : Trigger (INPUT) - Digital Pin 2
	Echo: Echo (OUTPUT) - Digital Pin 3
	GND: Ground
*/

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

int trigPin = D2;    // Trigger Pin
int echoPin = D3;    // Echo Pin

long duration, cm, inches;

/*
	Pull-down Resistor
	VCC: 3.3V
	Analog Pin: A3
	GND: Ground
	Remember to add a 10K Ohm pull-down resistor too.
*/

int fsrPin = A3;

// Create a variable to hold the FSR reading
int fsrReading = 0;

/*
	Neopixel: WS2812
	VCC: 3.3V
*/

// color codes
uint32_t red = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t green = strip.Color(0, 255, 0);
uint32_t yellow = strip.Color(255, 255, 0);
uint32_t white = strip.Color(255, 255, 255);
uint32_t pink = strip.Color(255, 192, 203);


// other neopixel variables
int MinBrightness = 10;       //value 0-255
int MaxBrightness = 100;      //value 0-255

int numLoops1 = 10;
int numLoops2 = 5;            //add new integer and value for more color's loop if needed.

int fadeInWait = 30;          //lighting up speed, steps.
int fadeOutWait = 30;         //dimming speed, steps.

// other variables: timers and status
String currStatus = "work";
int workTimer = 0;
int restTimer = 0;
int breakTimer = 0;

/**************************************************************/
/*************************   Setup    *************************/ 
/**************************************************************/

void setup() 
{
    //Suscription of announcement of another device
    Particle.subscribe(  "hammered/backtowork" , handleBacktoWork );
    Particle.subscribe(  "hammered/break" , handleBreak );
    Particle.subscribe(  "hammered/rest" , handleRest );
	//Define inputs and outputs
	pinMode(trigPin, OUTPUT);
	pinMode(echoPin, INPUT);
	
	//initialization of neopixel
	strip.begin();
    strip.show(); // Initialize all pixels to 'off'
}

/**************************************************************/
/**********************     main loop    **********************/ 
/**************************************************************/

void loop() 
{
    // due to instability of HC-SR04 we use a loop of 10 time detections
    // if there are 2 times detected long distance (no person at the desk)
    // then it is break or rest status
    int counter = 0;
    for(int i = 0; i <= 10; i++) {
        // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
	    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
	    digitalWrite(trigPin, LOW);
	    delayMicroseconds(5);
	    digitalWrite(trigPin, HIGH);
	    delayMicroseconds(10);
	    digitalWrite(trigPin, LOW);

	    // Read the signal from the sensor: a HIGH pulse whose
	    // duration is the time (in microseconds) from the sending
	    // of the ping to the reception of its echo off of an object.
	    pinMode(echoPin, INPUT);
	    duration = pulseIn(echoPin, HIGH);

	    // Convert the time into a distance
	    cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
	    inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
	    Particle.publish("inches", String(inches)); // This will publish the distance that the Ultrasonic Sensor reads every second
	    delay(1000);
	    if (inches >= 50) {
	        counter ++;
	    }
    }
    
    // Use analogRead to read the FSR reading
    // This gives us a value from 0 to 4095
    fsrReading = analogRead(fsrPin);
    
	if (counter >= 2) {
	    if (fsrReading <= 1500) {
	        currStatus = "break";
	        annouceBreak();
	        workTimer = 0;
	        breakTimer ++;
	        rgbBreathe(blue, 3, 20); // breathing light blue for break
	    } else {
	        currStatus == "rest";
	        annouceRest();
	        workTimer = 0;
	        breakTimer = 0;
	        restTimer ++;
	        rgbBreathe(green, 3, 20); // breathing light green for rest
	    }
	} else {
	    currStatus = "work";
	    colorAll(0, 5000);
	    //annouceWork();
	    if (restTimer >= 2) {
	        currStatus = "backtowork";
	        annouceBacktoWork();
	    }
	    restTimer = 0;
	    breakTimer = 0;
	    workTimer ++;
	    
	}
	// red solid light to show you have worked long enough
	if (workTimer >= 4) {
	    colorAll(red, 5000);
	}
	// green solid light to show you have rested long enough
	if (restTimer >= 2) {
	    colorAll(green, 5000);
	}
	// green solid light to show you have broke long enough
	if (breakTimer >= 2) {
	    colorAll(blue, 5000);
	}
}

/**************************************************************/
/**********************   Publishing     **********************/ 
/**************************************************************/


// publish functions
void annouceBacktoWork() {
    Particle.publish( "agreeable/backtowork" );
}

void annouceBreak() {
    Particle.publish( "agreeable/break" );
}

void annouceRest() {
    Particle.publish( "agreeable/rest" );
}


// handlers

// when I'm working long enough but the other one started rest
// wipe pink
void handleRest(const char *event, const char *data) {
    if (currStatus == "work" && workTimer >=4) {
        colorWipe(pink, 500);
    }
}

// when I'm working long enough but the other one started break
// wipe pink
void handleBreak(const char *event, const char *data) {
    if (currStatus == "work" && workTimer >=4) {
        colorWipe(pink, 500);
    }
}

// when I'm in rest or break but the other one started working again
// wipe yellow
void handleBacktoWork(const char *event, const char *data) {
    if (currStatus == "rest" || currStatus == "break") {
        colorWipe(yellow, 500);
    }
}


/**************************************************************/
/***********************     Functions     ********************/ 
/**************************************************************/

// Neopixel functions
// Set all pixels in the strip to a solid color, then wait (ms)
void colorAll(uint32_t c, uint8_t wait) {
    uint16_t i;

    for(i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
    }
    strip.show();
    delay(wait);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

// breathing
void rgbBreathe(uint32_t c, uint8_t x, uint8_t y) {
  for (int j = 0; j < x; j++) {
    for (uint8_t b = MinBrightness; b < MaxBrightness; b++) {
      strip.setBrightness(b * 255 / 255);
      for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
      }
      strip.show();
      delay(fadeInWait);
    }
    strip.setBrightness(MaxBrightness * 255 / 255);
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(y);
    }
    for (uint8_t b = MaxBrightness; b > MinBrightness; b--) {
      strip.setBrightness(b * 255 / 255);
      for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
      }
      strip.show();
      delay(fadeOutWait);
    }
  }
}
Click to Expand

Content Rating

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

0