// Use if you wamt to force the software SPI subsystem to be used for some reason (generally, you don't)
// #define FASTLED_FORCE_SOFTWARE_SPI
// Use if you want to force non-accelerated pin access (hint: you really don't, it breaks lots of things)
// #define FASTLED_FORCE_SOFTWARE_SPI
// #define FASTLED_FORCE_SOFTWARE_PINS
#include "FastLED.h"
// Set up the fastled stuff
#define NUM_LEDS 90 // How many leds are in the strip?
#define DATA_PIN 4 // Data pin that led data will be written out over
//#define CLOCK_PIN 8 // Clock pin only needed for SPI based chipsets when not using hardware SPI
CRGB leds[NUM_LEDS]; // This is an array of leds. One item for each led in your strip.
// Some colour values
uint8_t goalHue = 64; // normal 'bright yellow' value
uint8_t goalSat; // normal 'warm white' value
uint8_t minSat = 150; // normal 'warm white' value
uint8_t maxSat = 230; // almost yellow
//setup for the PIR-sensor
int pirPin = 3;
int ledState = LOW;
int pirVal = 0;
int pirTimer;
int realTime = millis();
int debug = 0;
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
// LED stuff
delay(1000); // sanity check delay - allows reprogramming if accidently blowing power w/leds
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); // This function sets up the leds and tells the controller about them
// PIR stuff
delay(1000); // needs a little delay to calibrate
pinMode(pirPin, INPUT);
// RTC stuff
Wire.begin();
// 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
// rtc.adjust(DateTime(2019, 3, 6, 20, 12, 0));
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
}
void loop() {
// define serial output
Serial.begin(9600);
Serial.println("Begin vd loop.");
// lets check for movement
pirVal = digitalRead(pirPin);
if (pirVal == HIGH) { // motion detected?
Serial.println("Motion detected. Update timer.");
pirTimer = millis(); // update timer
if (ledState == LOW) { // low state?
Serial.println("New motion. Lights on.");
lightsOn(); // turn on lights
ledState = HIGH; // set high state
delay(1000);
}
if (ledState == HIGH) { // high state?
Serial.println("New motion. Check time. Adjust saturation.");
lightsAdjust(); // adjust saturation for time of day
delay(1000);
}
}
if (pirVal == LOW) { // no motion?
if (ledState == HIGH) { // lights on?
if ((millis() - pirTimer) > 300000) { // timeout?
Serial.println(pirTimer - millis());
lightsOff(); // lights off
ledState = LOW; // set low state
Serial.println("No motion for some time. Lights off.");
delay(1000);
}
}
}
Serial.println("Einde van de loop.");
delay(1000);
}
void timeCheck() { // set appropriate saturation for time of day
DateTime now = rtc.now();
uint8_t theHour = now.hour();
uint8_t theMinute = now.minute();
uint8_t timeCont = theHour * 10 + map(theMinute, 0, 60, 0, 9);
Serial.print("Het is ");
Serial.print(theHour);
Serial.print(":");
Serial.print(theMinute);
if (theHour > 21) {
goalSat = maxSat;
}
if (theHour > 17) {
goalSat = map(timeCont, 180, 209, minSat, maxSat);
}
else if (theHour < 6) {
goalSat = maxSat;
}
else if (theHour < 8) {
goalSat = map(timeCont, 0, 79, maxSat, minSat);
}
else { goalSat = minSat; }
Serial.print("The hour is ");
Serial.println(theHour);
Serial.print("Appropriate saturation (goalSat) level is ");
Serial.println(goalSat);
}
void lightsAdjust() { // adjust HSV-values to approriate saturation
Serial.println("Running timecheck.");
void timeCheck();
Serial.print("Filling array with hue ");
Serial.print(goalHue);
Serial.print(" and sat ");
Serial.println(goalSat);
fill_solid( leds, NUM_LEDS, CHSV(goalHue, goalSat, 255));
FastLED.show();
FastLED.delay(3);
}
void lightsOff() {
fill_solid( leds, NUM_LEDS, CHSV(255, 255, 0)); // go black
FastLED.show();
}
void lightsOn() {
Serial.println("Start of lightsOn function.");
// set up some integers
uint8_t brightVal = 0;
uint8_t brightValDelta = 1;
uint8_t hue = 0;
for(uint8_t i=0; i<255; i++){
// we want to steadily change hue from purple to yellow, but
// because we want 255 instead of around 64 discrete steps
// we remap the hue values to 0-255 and fill the
// entire led array with this HSV-value
fill_solid( leds, NUM_LEDS, CHSV(map(hue, 0, 255, -20, goalHue), 255, 255));
brightVal = brightVal + brightValDelta; // we want to steadily increase the brightness
FastLED.setBrightness(brightVal);
FastLED.show(); // write to led array
hue++; // up the hue
FastLED.delay(2); // smooth and fast lights on
}
smoothTemp();
Serial.println("End of lightsOn function.");
}
void smoothTemp() {
Serial.println("Start of smoothTemp function.");
// set up some integers
uint8_t brightVal = 0;
uint8_t brightValDelta = 1;
uint8_t hue = 0;
timeCheck();
for(uint8_t sat=255; sat > goalSat; sat--) {
Serial.print("Sat level is ");
Serial.println(sat);
Serial.print("goalSat is ");
Serial.println(goalSat);
// we are at the right hue but now we want more brightness
// and the right temperature for time of day
fill_solid( leds, NUM_LEDS, CHSV(goalHue, sat, 255));
// write to led array
FastLED.show();
// wait xx millisecs
FastLED.delay(10);
}
Serial.println("End of smoothTemp function.");
}
Comments