//~~~~~~~~~~~~~~~~~~~~~~~ALL the libraries!~~~~~~~~~~~~~~~~~
//TIME
#include <Time.h>
//SD CARD
#include <SD.h>
#include <SPI.h>
//BTLE
//char INBYTE;
#define BTLE Serial1
//SPEAKER
#include "pitches.h"
//LEDS
#include <Adafruit_NeoPixel.h>
//VIBE MOTOR
//BUTTON
//~~~~~~~~~~~~~~~~~~~~~~~~PRE-SETUP~~~~~~~~~~~~~~~~~~~~~~~~~~~
//TIME
//SD CARD
File logFile;
const int chipSelect = 10;
//BTLE
//SPEAKER
const int speakerPin = 20;
int celebration1notes[8]={NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
//LEDS
#define PIN 16
#define NUMPIXELS 12
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
//VIBE MOTOR
const int motorPin = 9;
//BUTTON
const int buttonPin = 8;
int buttonState = 0;
int pushNumber = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~SETUP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void setup() {
//TIME
// set the Time library to use Teensy 3.0's RTC to keep time
setSyncProvider(getTeensy3Time);
Serial1.begin(9600);
if (timeStatus()!= timeSet) {
Serial1.println("Unable to sync with the RTC");
} else {
Serial1.println("RTC has set the system time");
Serial1.println("Time is:");
digitalClockDisplay();
}
//SD CARD
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial1.println("SD initialization failed!");
return;
}
//BTLE
//SPEAKER
//LEDS
strip.begin();
strip.show(); // Initialize all pixels to 'off'
//VIBE MOTOR
pinMode(motorPin, OUTPUT);
//BUTTON
pinMode(buttonPin, INPUT_PULLUP);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~LOOP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
time_t t = now();
Serial1.print("Log time: ");
digitalClockDisplay();
//~~~~~~~~~~~~~~~~~~~ CELEBRATION~~~~~~~~~~~~~~~~~~~~~~~~
celebration1();
//~~~~~~~~~~~~~~~~~~~~LOG TO SD~~~~~~~~~~~~~~~~~~~~~~~~
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
logFile = SD.open("logFile.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (logFile) {
Serial1.print("Writing to logFile.txt...");
logFile.print(hour());
logFile.print(":");
logFile.print((minute()));
logFile.print(":");
logFile.print((second()));
logFile.print(" ");
logFile.print(day());
logFile.print(" ");
logFile.print(month());
logFile.print(" ");
logFile.print(year());
logFile.println();
// close the file:
logFile.close();
Serial1.println("done.");
}
else {
// if the file didn't open, print an error:
Serial1.println("error opening logFile.txt");
}
pushNumber++;
delay(10);
}
else if(pushNumber>0){
greenDots(pushNumber);
}
else (pushNumber==0){
setBlank();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~FUNCTIONS~~~~~~~~~~~~~~~~~~~~~~~
//CELEBRATION 1
void celebration1() {
strip.setBrightness(255);
for(int i=0; i<8; i++){
digitalWrite(motorPin, HIGH);
if(i<7){
//Serial1.println(i);
tone(speakerPin, celebration1notes);
strip.setPixelColor(i, strip.Color(50,10,250));
strip.show();
delay(100);
}
else{
tone(speakerPin, celebration1notes);
theaterChase(strip.Color(127, 127, 127), 20);
delay(200);
}
}
digitalWrite(motorPin, LOW);
noTone(speakerPin);
fadeOut(1000);
}
//TIME
void digitalClockDisplay() {
// digital clock display of the time
Serial1.print(hour());
printDigits(minute());
printDigits(second());
Serial1.print(" ");
Serial1.print(day());
Serial1.print(" ");
Serial1.print(month());
Serial1.print(" ");
Serial1.print(year());
Serial1.println();
}
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial1.find(TIME_HEADER)) {
pctime = Serial1.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial1.print(":");
if(digits < 10)
Serial1.print('0');
Serial1.print(digits);
}
//SD CARD
//BTLE
//SPEAKER
//LEDS
// 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);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
void blackout()
{
for (int i = 0 ; i < NUMPIXELS; i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
void fadeOut(int time) {
for (int i = 0; i < 255; i++) {
strip.setBrightness(255-i);
strip.show();
delay (time/255);
}
blackout();
}
void setGreen() {
strip.setBrightness(20);
for (int j = 0 ; j < NUMPIXELS; j++)
{
strip.setPixelColor(j, 0, 250, 20);
}
strip.show();
}
void setRed()
{
strip.setBrightness(20);
for (int j = 0 ; j < NUMPIXELS; j++)
{
strip.setPixelColor(j, 250, 0, 20);
}
strip.show();
}
void greenDots(int number)
{
for (int i = 0 ; i < NUMPIXELS; i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.setBrightness(50);
int s=0;
int z=0;
for (int j = 0 ; j < number; j++)
{
if (((j+1) % 2) == 0) { //even
strip.setPixelColor(j+1+z, 0, 250, 20);
z++;
}
else { //odd
strip.setPixelColor(s, 0, 250, 20);
strip.setPixelColor(s+1, 0, 250, 20);
s= s+3;
}
}
strip.show();
}
void setBlank()
{
strip.setBrightness(10);
for (int j = 0 ; j < NUMPIXELS; j++)
{
strip.setPixelColor(j, 0, 0, 0);
}
strip.show();
}
//VIBE MOTOR
//BUTTON