Go Down

Topic: Neopixel RTC clock (Read 872 times) previous topic - next topic

thmmax

Hey,

I would like to use a Neopixel 24 ring to display seconds from a RTC. I have the Teensy 2++ and the DS1307 Real Time Clock board. I was trying to use this code https://learn.adafruit.com/neopixel-ring-clock/code-and-clock-faces from a GPS clock and adapt it for the RTC.

Anyway I'm kind of stuck - I don't know exactly where and how to read the time from the RTC...after days of trying, that's my attempt:
Code: [Select]

//NeoPixel clock with FLORA based on Kevin Alford's NeoPixel ring clock face
//https://github.com/zeroeth/time_loop
//http://www.youtube.com/watch?v=b-mROp-ZKuk
//modified by Becky Stern for Adafruit Industries
#include <Adafruit_NeoPixel.h>
#include <Time.h>
#include "RTClib.h"
#include <SoftwareSerial.h>
#include <Wire.h>

RTC_DS1307 RTC;

#define PIN 17

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);

#define outerTOP_LED                1

int outertopLED = outerTOP_LED;

float fLat = 0.0;
float fLon = 0.0;

uint32_t milli_color  = strip.Color ( 44,  21,  0);
uint32_t second_color = strip.Color ( 255, 0,  0);
uint32_t second1_color = strip.Color ( 44, 42,  0);
uint32_t off_color    = strip.Color (  0,  0,  0);

/* CLOCK */
class ClockPositions
{
public:

  uint8_t milli;
  uint8_t currentsecond;

  ClockPositions ();
  void update    ();
};


ClockPositions::ClockPositions()
{
  /*
  milli = 0;
   second = 0;
   minute = 30;
   hour = 6;
   */
  //DateTime(__DATE__, __TIME__);

}


void ClockPositions::update()
{

  if (! RTC.isrunning())
  {
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  currentsecond = outertopLED + map ((second() % 60), 0, 60, 0, 23);
  if (currentsecond > 24) {
    currentsecond = currentsecond-24;
  };
  milli  = map ((millis() %  1000), 0,  1000, 0, 24);
}

int getDecimalTime()
{
  DateTime now = RTC.now();
  int decimalTime = now.hour() * 100 + now.minute() + now.second();
  ;
  return decimalTime;
}

/* CLOCK VIEW */

class ClockSegments
{
public:
  ClockPositions    &positions;
  Adafruit_NeoPixel &strip;

  ClockSegments (Adafruit_NeoPixel&, ClockPositions&);
  void draw  ();
  void clear ();
  void add_color (uint8_t position, uint32_t color);
  uint32_t blend (uint32_t color1, uint32_t color2);
};


ClockSegments::ClockSegments (Adafruit_NeoPixel& n_strip, ClockPositions& n_positions):
strip (n_strip), positions (n_positions)
{
}


void ClockSegments::draw()
{
  clear();

  add_color (positions.currentsecond     % 24, second_color);
  add_color ((positions.currentsecond+1) % 24, second_color);
  add_color ((positions.currentsecond+2) % 24, second_color);


  add_color (positions.milli     % 24,  milli_color);
  add_color ((positions.milli+1) % 24,  milli_color);
  add_color ((positions.milli+2) % 24,  milli_color);

  strip.show ();
}


void ClockSegments::add_color (uint8_t position, uint32_t color)
{
  uint32_t blended_color = blend (strip.getPixelColor (position), color);

  /* Gamma mapping */
  uint8_t r,b,g;

  r = (uint8_t)(blended_color >> 16),
  g = (uint8_t)(blended_color >>  8),
  b = (uint8_t)(blended_color >>  0);

  strip.setPixelColor (position, blended_color);
}


uint32_t ClockSegments::blend (uint32_t color1, uint32_t color2)
{
  uint8_t r1,g1,b1;
  uint8_t r2,g2,b2;
  uint8_t r3,g3,b3;

  r1 = (uint8_t)(color1 >> 16),
  g1 = (uint8_t)(color1 >>  8),
  b1 = (uint8_t)(color1 >>  0);

  r2 = (uint8_t)(color2 >> 16),
  g2 = (uint8_t)(color2 >>  8),
  b2 = (uint8_t)(color2 >>  0);


  return strip.Color (constrain (r1+r2, 0, 255), constrain (g1+g2, 0, 255), constrain (b1+b2, 0, 255));
}


void ClockSegments::clear ()
{
  for(uint16_t i=0; i<strip.numPixels (); i++) {
    strip.setPixelColor (i, off_color);
  }
}

/* SIMPLE MIXER */
// add rgb and clamp

/* APP */
ClockPositions positions;
ClockSegments  segments(strip, positions);

void setup ()
{

  Wire.begin();
  RTC.begin();

  strip.begin ();
  strip.show (); // Initialize all pixels to 'off'
}


void loop ()

  positions.update ();
  segments.draw (); 
}




// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint32_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// 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) {
  if(WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}


Hopefully there's a solution 8)
Max

Go Up