How to add neopixel effects to this project

Hi, how would one go about adding a few effects to this digital clock code? Maybe via a button press to cycle...

I've tried adding a rainbow effect but can't figure out how to have it display only on the lit pixels.

Many thanks in advance!

#include <DS3231_Simple.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#endif

DS3231_Simple Clock;
DateTime MyDateAndTime;

#define LEDCLOCK_PIN 6
#define COLON_PIN 9

#define LEDCLOCK_COUNT 56
#define COLON_COUNT 2

uint32_t red = 0xFF0000;
uint32_t orange = 0xFF8000;
uint32_t yellow = 0xFFFF00;
uint32_t green = 0x00CC00;
uint32_t blue = 0x0000FF;
uint32_t purple = 0x330066;
uint32_t pink = 0xFF66FF;
uint32_t white = 0xFFFFFF;
uint32_t aqua = 0x66FFFF;
uint32_t gold = 0xCCCC00;
uint32_t black = 0x000000; //off


Adafruit_NeoPixel colonBlink(COLON_COUNT, COLON_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel stripClock(LEDCLOCK_COUNT, LEDCLOCK_PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  Serial.begin(9600);
  Clock.begin();

  stripClock.begin();
  stripClock.show();
  stripClock.setBrightness(100);

  colonBlink.begin();
  colonBlink.show();
  colonBlink.setBrightness(100);   
}

void loop() {
  readTheTime();
  displayTheTime();
  blinkTheColon();
  stripClock.show(); 
}

void readTheTime(){
  MyDateAndTime = Clock.read();

  Serial.println("");
  Serial.print("Time is: ");    Serial.print(MyDateAndTime.Hour);
  Serial.print(":");            Serial.print(MyDateAndTime.Minute);
  Serial.print(":");            Serial.println(MyDateAndTime.Second);
  Serial.print("Date is: 20");  Serial.print(MyDateAndTime.Year);
  Serial.print(":");            Serial.print(MyDateAndTime.Month);
  Serial.print(":");            Serial.println(MyDateAndTime.Day);
}

void displayTheTime(){
  stripClock.clear();
  
  int firstMinuteDigit = MyDateAndTime.Minute % 10;
  displayNumber(firstMinuteDigit, 0, blue);
  
  int secondMinuteDigit = floor(MyDateAndTime.Minute / 10);
  displayNumber(secondMinuteDigit, 14, blue);  

  int firstHourDigit = MyDateAndTime.Hour;
  firstHourDigit = firstHourDigit % 10;
  displayNumber(firstHourDigit, 28, blue);

  int secondHourDigit = MyDateAndTime.Hour;
  secondHourDigit = floor(secondHourDigit / 10);
  displayNumber(secondHourDigit, 42, blue);
}
    
void blinkTheColon(){
  colonBlink.fill(green,(0),2);
  colonBlink.show();
  delay(1000);
  colonBlink.fill(black,(0),2);
  colonBlink.show();
  delay(1000); 
} 

void displayNumber(int digitToDisplay, int offsetBy, uint32_t colourToUse){
  switch (digitToDisplay){
    case 0:
    digitZero(offsetBy,colourToUse);
      break;
    case 1:
      digitOne(offsetBy,colourToUse);
      break;
    case 2:
    digitTwo(offsetBy,colourToUse);
      break;
    case 3:
    digitThree(offsetBy,colourToUse);
      break;
    case 4:
    digitFour(offsetBy,colourToUse);
      break;
    case 5:
    digitFive(offsetBy,colourToUse);
      break;
    case 6:
    digitSix(offsetBy,colourToUse);
      break;
    case 7:
    digitSeven(offsetBy,colourToUse);
      break;
    case 8:
    digitEight(offsetBy,colourToUse);
      break;
    case 9:
    digitNine(offsetBy,colourToUse);
      break;
    default:
     break;
  }
}
void digitZero(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 6);
    stripClock.fill(colour, (8 + offset), 6);     
}

void digitOne(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 2);
    stripClock.fill(colour, (8 + offset), 2);
}

void digitTwo(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 4);
    stripClock.fill(colour, (6 + offset), 2);
    stripClock.fill(colour, (10 + offset), 4);
}

void digitThree(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 4);
    stripClock.fill(colour, (6 + offset), 6);
}

void digitFour(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 2);
    stripClock.fill(colour, (4 + offset), 6);
}

void digitFive(int offset, uint32_t colour){
    stripClock.fill(colour, (2 + offset), 10);
}

void digitSix(int offset, uint32_t colour){
    stripClock.fill(colour, (2 + offset), 12);
}

void digitSeven(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 4);
    stripClock.fill(colour, (8 + offset), 2);
}

void digitEight(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 14);
}

void digitNine(int offset, uint32_t colour){
    stripClock.fill(colour, (0 + offset), 12);
}

If you want just change the color of each number, you have to modify the last value in calling

displayNumber(secondMinuteDigit, 14, blue);  

One approach might be to change the clock code so that instead of lighting the segments to display the time, it does the opposite, by switching off the segments that should not be lit. That way, you can fill the strip with your rainbow or other effect, then call the function to switch off the segments that should not be lit, before updating the strip.

2 Likes