Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
gitsmol
Created October 7, 2019

LED-lighting base project

Simple project that uses a homemade PCB to enable LED-lighting projectes with Arduino Nano, an RTC, PIR-module, 3 POTs and 3 LED-strips.

Work in progress67
LED-lighting base project

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
WS2812 Addressable LED Strip
Digilent WS2812 Addressable LED Strip
×3
Potentiometer 1 kohm
×3
USB Connector, Micro USB Type B
USB Connector, Micro USB Type B
×1
USB Connector, USB Type A
USB Connector, USB Type A
×3

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

FastLED Schema

Code

LED Motioncontrol with color fade

Arduino
// 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.");  

  }

Credits

gitsmol
1 project • 0 followers

Comments