Skip to content

Instantly share code, notes, and snippets.

@roshkins
Created January 2, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roshkins/6bd235bfac39f6b76e97e744a8720fbc to your computer and use it in GitHub Desktop.
Save roshkins/6bd235bfac39f6b76e97e744a8720fbc to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <RTClib.h>
#include <SD.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//Copyright (C) 2016-2017 Rashi Abramson. All Rights Reserved.
long FIRST_63_LEDS = 0;
#define PIN 6
#define WHITE 0xFFFFFF
#define DAY_IN_MILLISECONDS 1000 //* 60 * 60 //* 24
// 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)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 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.
///rtc global variable.
RTC_DS1307 rtc;
//CSV file for lights global variable
File currentFile;
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
Serial.begin(9600);
//initialize color strip with pins DIN -> 6, 5v -> 5v, and GND -> GND
strip.setBrightness(255);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println(F("ARDUINO STARTED."));
if (! rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
} else {
Serial.println(F("RTC found."));
}
//initialize real time clock with pins SCL -> A4, SDA -> A5, SQW -> Unused, 5V -> 5V, GND -> GND
if (! rtc.isrunning()) {
Serial.println(F("RTC is NOT running! Date/Time being set."));
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2016, 11, 22, 3, 0, 0));
} else {
Serial.println(F("RTC IS running."));
rtc.adjust(DateTime(2016, 11, 22, 3, 0, 0)); //For debugging different dates
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //For reseting the correct datetime during debug
}
//initialize sd card Pins: CLK -> 13, MISO -> 12, MOSI -> 11, CS -> 4, 3V3 -> 3V, GND -> GND
Serial.print(F("Initializing SD card..."));
if (!SD.begin(4)) {
Serial.println(F("initialization failed!"));
return;
}
Serial.println(F("initialization done."));
}
bool isTwo = false;
void loop() {
// Some example procedures showing how to display to the pixels:
DateTime timeNow = rtc.now();
int dayOfMonth = timeNow.day(); //0-based day, month ranges from 0-30, inclusive.
int monthOfYear = timeNow.month();
int yearOfMillenia = timeNow.year(); //Current year, would have used year, but year conflicts with keyword "year"
currentFile = SD.open("lights.csv", FILE_READ); //No second parameter for reading, 8.3 filename
if (!currentFile) {
Serial.println(F("ERROR: lights.csv could not be read.\n\n"
" Are multiple files being attempted to be read? \nDoes the file exists and is it properly named?"));
return;
}
//Advance past column headings
Serial.println(F("lights.csv:"));
while(currentFile.available() && currentFile.peek() != '\n'){
Serial.print((char)currentFile.read());
}
String nowDate = String( String(monthOfYear) + "/" + String(dayOfMonth) + "/" + String(yearOfMillenia) );
Serial.println(F("Day:\tMonth:\tYear:\t"));
Serial.print(dayOfMonth);
Serial.print("\t");
Serial.print(monthOfYear);
Serial.print("\t");
Serial.println(yearOfMillenia);
String fileDate = String("");
bool foundDate = false;
while(!foundDate){
while( currentFile.available() && (currentFile.peek() == '\r' || currentFile.peek() == '\n') ){
currentFile.read();
}
while(currentFile.available() && currentFile.peek() != ','){
fileDate.concat ( (char)currentFile.read() );
}
foundDate = (fileDate.compareTo(nowDate) == 0); //set terminating condition
Serial.println(foundDate ? F("File date DID matched today's date.") : F("File date DIDN'T match today's date."));
Serial.print(F("I think today is \""));
Serial.print(nowDate.c_str());
Serial.print(F("\" and the file's date was \""));
Serial.print(fileDate.c_str());
Serial.print(F("\". I got a comparison value of "));
Serial.println(fileDate.compareTo(nowDate));
if(!foundDate){ //if we haven't foundDate yet, move on to next line.
while( currentFile.available() && (currentFile.peek() != '\r' && currentFile.peek() != '\n') ){
currentFile.read();
}
fileDate = String("");
}
}
while( currentFile.available() && !isDigit( (char)currentFile.peek() ) ){ //read until the next character is a digit
currentFile.read();
}
Serial.println(F("Digit encountered."));
//reset strip colors by flushing pixel buffer by showing buffer twice. Second buffer will be all zeros.
strip.show(); strip.show();
//Read each number and display pixel
uint32_t warm_white =
strip.Color(0xFF, 0xFF, 0xFF);
while(currentFile.available() && currentFile.peek() != '"'){ //keep going until the quote.
String lightNumber = String("");
while( currentFile.available() && isDigit( (char)currentFile.peek() ) ) //While next character in file is a digit...
{
lightNumber.concat( (char)currentFile.read() ); //add it to the digit string!
}
Serial.print( F("Setting pixel ") );
Serial.print(lightNumber.c_str());
Serial.println( F(" to on.") );
//Set the pixel at the integer-parsed location to the color (warm white was the name) as defined above.
strip.setPixelColor(lightNumber.toInt(), warm_white);
//Skip to next number or to quote...
while( currentFile.available() && !isDigit( (char)currentFile.peek()) && currentFile.peek() != '"' ){ //read until the next character is a digit
currentFile.read();
}
Serial.println(F("Digit encountered."));
}
// "other white"
//strip.Color(0xFF,0x80,0x20);
//yellow strip.Color(255,100,0);
strip.show();
//close file handle.
currentFile.close();
//delay until next update.
delay(DAY_IN_MILLISECONDS);
}
/*
* Example of how code works.
*
* void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment