// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel innerPixel = Adafruit_NeoPixel(16, D2, PIXEL_TYPE);
Adafruit_NeoPixel hourPixel = Adafruit_NeoPixel(12, D5, PIXEL_TYPE);
Adafruit_NeoPixel minPixel = Adafruit_NeoPixel(12, D6, PIXEL_TYPE);
//For random colors
int r;
int g;
int b;
//For Button
int buttonPin = D4;
bool lightStatus = 0;
//For Time/Snooze Tracking
int snooze = 2;
long wakeTime = 0;
long lastUpdate = 0;
//For Speaker
int speakerPin = D3;
// create an array for the notes in the melody //C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024};
// create an array for the duration of notes. // note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4};
//For Pressure sensor
int fsrPin = A0;
int fsrReading;
int dataCount = 0;
void setup() {
//For Alarm function
Particle.function("Wake-Up Call", WakeUpCall);
//For inner lights
innerPixel.begin();
innerPixel.clear();
innerPixel.show();
//For Clock lights
hourPixel.begin();
hourPixel.clear();
hourPixel.show();
minPixel.begin();
minPixel.clear();
minPixel.show();
//For Speaker
pinMode(speakerPin, OUTPUT );
//For Button
pinMode(buttonPin, INPUT_PULLUP);
//For Clock
Time.zone(-5.00);
//For Data updating
Particle.subscribe( "googlesheet" , DataHandler, "e00fce68d4068f92d1a5b092" );
}
void loop() {
fsrReading = analogRead(fsrPin);
if( fsrReading > 2000 ){
if(snooze == 2){
Clock();
}
if(millis() < wakeTime+300000 && snooze == 0){
//Check for snooze button press
if(digitalRead(buttonPin) == LOW){
AlarmSnooze();
delay(200);
return;
}
AlarmOn();
}
if(snooze == 1 && millis() < wakeTime){
innerPixel.setBrightness(100);
innerPixel.show();
}
if(snooze == 1 && millis() >= wakeTime){
snooze = 0;
AlarmOn();
}
return;
}
if(fsrReading <2000){
if(snooze != 2){
innerPixel.clear();
innerPixel.show();
wakeTime = 0;
snooze = 2;
dataCount = 0;
}
//show Clock
Clock();
if(digitalRead(buttonPin) == LOW){
AmbientLight();
delay(500);
}
}
}
int WakeUpCall(String trigger){
innerPixel.setBrightness(255);
r = (rand() % 51)*5;
g = (rand() % 51)*5;
b = (rand() % 51)*5;
for(int i=0; i<16; i++){
uint32_t c = innerPixel.Color(r, g, b);
innerPixel.setPixelColor(i, c);
}
// set a color
innerPixel.show();
hourPixel.clear();
hourPixel.show();
minPixel.clear();
minPixel.show();
wakeTime = millis();
snooze = 0;
return 1;
}
void AlarmOn(){
//Turn on light, pick random color, & set brightness to max
innerPixel.setBrightness(255);
r = (rand() % 51)*5;
g = (rand() % 51)*5;
b = (rand() % 51)*5;
for(int i=0; i<16; i++){
uint32_t c = innerPixel.Color(r, g, b);
innerPixel.setPixelColor(i, c);
}
innerPixel.show();
playNotes();
// delay(400);
return;
}
void AlarmSnooze(){
snooze = 1;
wakeTime = millis() + 540000;
innerPixel.setBrightness(10);
innerPixel.show();
return;
}
void DataHandler(const char *event, const char *data){
String dataString = String(data);
dataCount = dataCount + dataString.toInt();
hourPixel.clear();
uint32_t h = hourPixel.Color(255,0,0);
for(int j=0; j < (dataCount / 10 % 10); j++){
hourPixel.setPixelColor(j, h);
}
hourPixel.show();
minPixel.clear();
uint32_t m = minPixel.Color(255,200,200);
for(int i=0; i < (dataCount % 10); i++){
minPixel.setPixelColor(i, h);
}
minPixel.show();
//Pulse Light
for(int i=101; i<142; i++){
innerPixel.setBrightness(i);
innerPixel.show();
delay(12);
}
for(int i=141; i>100; i--){
innerPixel.setBrightness(i);
innerPixel.show();
delay(12);
}
return;
}
void playNotes(){
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 7; thisNote++) {
if(digitalRead(buttonPin) == LOW){
AlarmSnooze();
return;
}
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
return;
}
void Clock(){
int hour = Time.hourFormat12();
int minute = Time.minute();
//int min =
hourPixel.clear();
hourPixel.setBrightness(5);
uint32_t h = hourPixel.Color(255,255,255);
hourPixel.setPixelColor(hour-1, h);
minPixel.clear();
minPixel.setBrightness(5);
uint32_t m = minPixel.Color(100,100,255);
if(minute >= 0 && minute <= 5){
minPixel.setPixelColor(0, m);
}
if(minute >= 5 && minute <= 10){
minPixel.setPixelColor(1, m);
}
if(minute >= 10 && minute <= 15){
minPixel.setPixelColor(2, m);
}
if(minute >= 15 && minute <= 20){
minPixel.setPixelColor(3, m);
}
if(minute >= 20 && minute <= 25){
minPixel.setPixelColor(4, m);
}
if(minute >= 25 && minute <= 30){
minPixel.setPixelColor(5, m);
}
if(minute >= 30 && minute <= 35){
minPixel.setPixelColor(6, m);
}
if(minute >= 35 && minute <= 40){
minPixel.setPixelColor(7, m);
}
if(minute >= 40 && minute <= 45){
minPixel.setPixelColor(8, m);
}
if(minute >= 45 && minute <= 50){
minPixel.setPixelColor(9, m);
}
if(minute >= 50 && minute <= 55){
minPixel.setPixelColor(10, m);
}
if(minute >= 55 && minute <= 60){
minPixel.setPixelColor(11, m);
}
minPixel.show();
hourPixel.show();
return;
}
void AmbientLight(){
lightStatus = !lightStatus;
if(lightStatus == 1){
innerPixel.setBrightness(255);
r = (rand() % 51 + 1)*5;
g = (rand() % 51 + 1)*5;
b = (rand() % 51 + 1)*5;
for(int i=0; i<16; i++){
uint32_t c = innerPixel.Color(r, g, b);
innerPixel.setPixelColor(i, c);
}
innerPixel.show();
delay(300);
return;
}
if(lightStatus ==0){
innerPixel.clear();
innerPixel.show();
delay(300);
return;
}
}
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. .