Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LED strip clock. Uses Ws2812 LEDs for segments in an 7 segment.
- // timetoarray simplified with only one send digit to LED routine (as a function extern,
- // with an array of segment positions and direct RTC to array routine.
- // a bunch of commented out Serial.print statemenrt were removed.
- // Original project and code from: https://www.instructables.com/Big-auto-dim-room-clock-using-arduino-and-WS2811/
- // thanks to Reddut user /u/piensa as well.
- //#include <Time.h>
- //#include <TimeLib.h>
- //#include <DS3232RTC.h>
- //#include <Time.h>
- #include <Wire.h>
- #include "FastLED.h"
- #define NUM_LEDS 60 // Number of LED controles (remember I have 3 leds / controller)
- #define COLOR_ORDER RGB // Define color order for your strip
- #define DATA_PIN 6 // Data pin for led comunication
- //BID/DEC conversions
- #define bcd2dec(bcd_in) (bcd_in >> 4) * 10 + (bcd_in & 0x0f)
- #define dec2bcd(dec_in) ((dec_in / 10) << 4) + (dec_in % 10)
- const byte sensorPin = 3; // light sensor pin
- const byte brightnessLow = 64; // Low brightness value
- const byte brightnessHigh = 255; // High brightness value
- CRGB leds[NUM_LEDS]; // Define LEDs strip
- byte digits[10][7] = {{0, 1, 1, 1, 1, 1, 1}, // Digit 0
- {0, 1, 0, 0, 0, 0, 1}, // Digit 1
- {1, 1, 1, 0, 1, 1, 0}, // Digit 2
- {1, 1, 1, 0, 0, 1, 1}, // Digit 3
- {1, 1, 0, 1, 0, 0, 1}, // Digit 4
- {1, 0, 1, 1, 0, 1, 1}, // Digit 5
- {1, 0, 1, 1, 1, 1, 1}, // Digit 6
- {0, 1, 1, 0, 0, 0, 1}, // Digit 7
- {1, 1, 1, 1, 1, 1, 1}, // Digit 8
- {1, 1, 1, 1, 0, 1, 1}
- }; // Digit 9 | 2D Array for numbers on 7 segment
- byte cursorIndex[6] = {37, 30, 22, 15, 7, 0};// digit LED 0 positions, seconds>hours
- byte colons[3] = {14,29,44}; // colon LED positions.
- byte dateoffset=4;// date offset. No. of RTC registers to shift, to show date.
- bool Dot = true; //Dot state
- bool DST = false; //DST state
- int ledColor = 0x00FF00; // Color used (in hex)
- int colonColor = 0x0000FF; // Color used (in hex)
- int ledOff = 0x000000; // LED "off" color used (in hex)
- byte RTC_time[0x07]; // holds the array from the DS3231 registers
- int counter;
- void setup() {
- // Serial.begin(9600);
- Wire.begin();
- LEDS.addLeds<WS2812, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set LED strip type
- LEDS.setBrightness(64); // Set initial brightness
- pinMode(2, INPUT_PULLUP); // Define DST adjust button pin
- pinMode(4, INPUT_PULLUP); // Define Minutes adjust button pin
- pinMode(5, INPUT_PULLUP); // Define Hours adjust button pin
- }
- // Check Light sensor and set brightness accordingly
- void BrightnessCheck() {
- int sensorValue = digitalRead(sensorPin); // Read sensor
- if (sensorValue == 0) {
- LEDS.setBrightness(brightnessHigh);
- }
- else {
- LEDS.setBrightness(brightnessLow);
- }
- };
- // Convert time to array needed for display
- void TimeToArray(bool showdate) {
- getRTCdatetime();
- Dot = RTC_time[0] % 2;
- if (DST) { // if DST is true then add one hour
- RTC_time[2] = (RTC_time[2] + 1) % 24; // modulo, because 23 hours+DST is 0 hours, not 24 hours.
- }
- //light/dark colons
- if (Dot) {
- leds[colons[0]] = colonColor;
- leds[colons[1]] = colonColor;
- }
- else {
- leds[colons[0]] = ledOff;
- leds[colons[1]] = ledOff;
- }
- for (byte index = 0; index < 3; index++) {
- byte index2 = index * 2;
- // only place boolean multiplication used here
- // 4 added to index value if showdate true/1, 0 if false.
- byte digit = RTC_time[index+(dateoffset*showdate)] % 10;
- digit_set(cursorIndex[index2], digit);
- digit = (RTC_time[index+(dateoffset*showdate)] / 10) % 10;
- digit_set(cursorIndex[index2 + 1], digit);
- }
- // added because I am using 60LED strip, so why not.
- digit_set(cursorIndex[0]+15, counter%10);
- digit_set(cursorIndex[0]+8, (counter/10)%10);
- leds[colons[2]] = colonColor;
- }
- // set LEDs for cursor point, to that digit
- void digit_set(byte cursor, byte digit) {
- for (int k = 0; k < 7; k++) {
- if (digits[digit][k] == 1) {
- leds[k + cursor] = ledColor;
- }
- else {
- leds[k + cursor] = ledOff;
- }
- }
- }
- void DSTcheck() {
- int buttonDST = digitalRead(2);
- if (buttonDST == LOW) {
- if (DST) {
- DST = false;
- }
- else if (!DST) {
- DST = true;
- };
- delay(500);
- };
- }
- void TimeAdjust() {
- int buttonH = digitalRead(5);
- int buttonM = digitalRead(4);
- if (buttonH == LOW || buttonM == LOW) {
- delay(500);
- // getRTCdatetime();
- if (buttonH == LOW) {
- if (RTC_time[2] == 23) {
- RTC_time[2] = 0;
- }
- else {
- RTC_time[2] += 1;
- }
- }
- else {
- if (RTC_time[1] == 59) {
- RTC_time[1] = 0;
- }
- else {
- RTC_time[1] += 1;
- };
- };
- setRTCdatetime();
- }
- }
- void loop() // Main loop
- {
- // a lot of stuff disabled, not needed for my experiment.
- // BrightnessCheck(); // Check brightness
- // DSTcheck(); // Check DST
- // TimeAdjust(); // Check to see if time is geting modified
- TimeToArray(0); // Get leds array with required configuration (RTC registers 0,1,2)
- // TimeToArray(1); to show date (RTC registers 4,5,6)
- // striptest();
- FastLED.show(); // Display leds array
- counter++; //for the even extra digits I added.
- delay(200);// don't need to rush this
- }
- // I2C direct RTC to array. Got from Internet someplace. Sorry who wrote it.
- //Not too different from one I wrote anyways.
- void getRTCdatetime() {
- Wire.beginTransmission (0x68);
- // Set device to start read reg 0
- Wire.write (0x00);
- Wire.endTransmission ();
- // request 7 bytes from the DS3231 and release the I2C bus
- Wire.requestFrom(0x68, 0x07, true);
- byte idx = 0;
- // read the first seven bytes from the DS3231 module into the array
- while (Wire.available()) {
- byte input = Wire.read();
- RTC_time[idx] = bcd2dec(input); // read each byte from the register in the array
- idx++;
- }
- }
- //setting RTC from the array, except seconds are zeroed.
- void setRTCdatetime() {
- RTC_time[0] = 0; // set seconds to zero
- Wire.beginTransmission (0x68);
- // Set device to start read reg 0
- Wire.write (0x00);
- for (int idx = 0; idx < 7; idx++) {
- Wire.write (dec2bcd(RTC_time[idx]));
- }
- Wire.endTransmission ();
- }
- //test routine, to test digit positions from cursorIndex[] array
- void striptest() {
- for (byte i = 0; i < 6; i++) {
- // clear strip
- for (byte sled = 0; sled < 60; sled++) {
- leds[sled] = 0;
- }
- digit_set(cursorIndex[i], 8);//8, because all LEDs on.
- // set cursor LEDs after, so that the above doesn't overwrite them
- leds[colons[0]] = colonColor;
- leds[colons[1]] = colonColor;
- // show strip
- FastLED.show(); // Display leds array
- delay(750);
- }// close for i loop
- }
Advertisement
Add Comment
Please, Sign In to add comment