//LED library setup
#include "neopixel.h"
#include <math.h>
//capacitive library setup
//#include "captouch.h"
//#define CAPTOUCH_DEBUG
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
//push button
int buttonPush = A2;
// Pin numbers for vibrating discs
int vib1 = D4;
int vib2 = D5;
// pin number for motor
int Motorpin = D1;
//Levels of counter
int unread = 0;
int timeHeld = 0;
int level1 = 1;
int level2 = 2;
int level3 = 3;
void setup()
{
//LED setup
strip.begin();
//for(i=0; i< strip.numPixels(); i++)
//strip.setPixelColor(i, 0, 0, 0 );//white
strip.show(); // Initialize all pixels to 'off'
// Set pins to output, meaning info flows from particle to component (vibrating discs)
pinMode(vib1, OUTPUT);
pinMode(vib2, OUTPUT);
pinMode(Motorpin, OUTPUT);
// Set all outputs to off to begin
digitalWrite(vib1, LOW);
digitalWrite(vib2, LOW);
digitalWrite(Motorpin, LOW);
//listening for IFTTT trigger for Gmail
Particle.variable("unread", &unread, INT);
Particle.subscribe("NewEmail", myHandler);
Serial.begin( 9600 );
Serial.print("startup");
}
void loop()
{
int buttonState = analogRead(buttonPush);
delay(50);
//Serial.print(timeHeld);
//Serial.print("");
//Serial.print(buttonState);
//Serial.print(" ");
//when button is pressed, reset unread count to zero
if (buttonState <600 ){
unread = 0;
Serial.print("reset");
Serial.print(unread);
Serial.print(" ");
//timeHeld is triggered if button is held down for a few seconds
timeHeld++;
if (timeHeld > 60){
timeHeld = 0;
}
}
//when DC motor is running in level 3, analog output of button is different, needs its own if
if (buttonState >2000 ){
unread = 0;
Serial.print("reset");
Serial.print(unread);
Serial.print(" ");
timeHeld++;
if (timeHeld > 60){
timeHeld = 0;
}
}
if (unread ==0){
if (timeHeld > 30){
//if button is held down for a few seconds, turn off the lights
uint32_t i;
for(i=0; i< strip.numPixels(); i++)
strip.setPixelColor(i, 0, 0, 0 );//lights off
strip.show();
}else{
breathe();
//delay(50);
digitalWrite(vib1, LOW);
digitalWrite(vib2, LOW);
digitalWrite(Motorpin, LOW);
}
}
}
void myHandler(const char *event, const char *data){
unread++;//when handler gets a NewEmail event, increment unread count
//Serial.print("email");
//Serial.print(unread);
checklevelchangestate();
}
//function to change state of cube
void checklevelchangestate(){
Serial.print("checking level");
if ( unread <= level1)
{
Serial.print("level");
Serial.print(level1);
//turn the first level of vibrators
digitalWrite(vib1, HIGH);
// Turn on LED to yellow
uint32_t i;
for(i=0; i< strip.numPixels(); i++)
strip.setPixelColor(i, 255, 96, 0 );//yellow
strip.show();
}else if(unread <= level2){
Serial.print("level");
Serial.print(level2);
// We'll turn the all vibrators
digitalWrite(vib1, HIGH);
digitalWrite(vib2, HIGH);
// Turn on LED to orange
uint32_t i;
for(i=0; i< strip.numPixels(); i++)
strip.setPixelColor(i, 255, 20, 0 );//orange
strip.show();
}else if(unread <= level3){
Serial.print("level");
Serial.print(level3);
// We'll turn the first level of vibrators
digitalWrite(vib1, HIGH);
digitalWrite(vib2, HIGH);
digitalWrite(Motorpin, HIGH);
// Turn on LED to red
uint32_t i;
for(i=0; i< strip.numPixels(); i++)
strip.setPixelColor(i, 255, 0, 0 );//red
strip.show();
}
}
float breathe( ){
float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
//Serial.println( val );
uint16_t i;
uint32_t c = strip.Color(val, val, val);
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, c );
}
strip.show();
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .