WS2812b 7 segment clock

Wondering if anyone would be able to help me with some code for a project i have started but now found that im slightly out of me depth for.

Ive designed and 3D printed my own 7 segment clock.

i have 4 parts with 7 segments in each part. each segment has 2 LEDs.

i cannot find any tutorial for a simple RTC with WS2812b LEDs for a 7 segment clock.

if anyone has any code for this or could even point me in the right direction that would be much appreciated.

many thanks
Ben

Included a small Oled and bluetooth, but you can skip that:

#include <Adafruit_NeoPixel.h>
#include <DS3231.h>
#include <Wire.h>
#include <EEPROM.h> 
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include <SoftwareSerial.h>

#define Mode      2    
#define Min       3     
#define Plus      4     
#define BT_TxD    6
#define BT_RxD    7
#define stripPIN  9
#define dotPIN   10

#define blauw 0x00001F
#define zwart 0x000000
#define rood 0x1F0000
#define stripCOUNT 28
#define dotCOUNT    2

SSD1306AsciiAvrI2c oled;
DS3231  Clock;
Adafruit_NeoPixel strip(stripCOUNT, stripPIN, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel dot(dotCOUNT, dotPIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial BlueTooth(6, 7);   // (TXD, RXD) of HC-06

int i=0,j=0,k=0;
byte minuten,uren,seconden,datum,maand,jaar,maxdagen;
byte Digit_1,Digit_2,Digit_3,Digit_4,mode=0,flash=0,KleurNummer,helderheid=255;
bool h12,PM,century=false,schrikkeljaar,ShowDate=true,regenboog=false,Ambient=false,puntjes;
int delayval = 500; // delay for half a second
uint32_t DisplayColor;
float CurvePower = 2.5;
unsigned long previousMillisA = 0;     // will store last time Flash
const long intervalA = 250;           // interval at which to invert Flash
unsigned long previousMillisB = 0;     // will store last time LED was updated
const long intervalB = 1000;           // interval at which to blink (milliseconds)
unsigned long previousMillisC = 0;     // Button SetMode last time read
const long intervalC = 500;           // interval at which to read SetMode button
char input;

String SetText = "RUNNING.....",data,segment;


void setup() {
   Serial.begin(9600);
//   BlueTooth.begin(9600);  
   Wire.setClock(400000L);
   oled.begin(&Adafruit128x32,0x3C);
   oled.setFont(Adafruit5x7);
   pinMode(stripPIN, OUTPUT);
   pinMode(dotPIN, OUTPUT);
   pinMode(BT_RxD, OUTPUT);       //Bluetooth RxD PIN
   pinMode(BT_TxD, INPUT_PULLUP); //Bluetooth TxD PIN
   pinMode(Mode, INPUT_PULLUP); 
   pinMode(Plus, INPUT_PULLUP); 
   pinMode(Min, INPUT_PULLUP); 
   BlueTooth.begin(9600);  
   Wire.begin();
   strip.begin();dot.begin();
   strip.show();dot.show();
//   EEPROM.write(1, 255);
   KleurNummer= EEPROM.read(0);
   helderheid=EEPROM.read(1);
   regenboog=EEPROM.read(2);
   ShowDate=EEPROM.read(3);
   Ambient=EEPROM.read(4);
   if(Clock.getYear()%4==0)schrikkeljaar=true;
   welkom();
   }

void loop() {
  unsigned long currentMillis = millis();
  if (BlueTooth.available())
  {
    input=(BlueTooth.read());
    if (input != '#') {
      data += input;
    }
    else{
      String message_id = String(data.substring(0,1)); //gets only D or T
      data.remove(0,1); //data becomes "50" since '!' is not added to data
      if(message_id == "D"){
        segment = String(data.substring(0,2));datum=segment.toInt();
        segment= String(data.substring(2,4));maand=segment.toInt();
        segment= String(data.substring(6,8));jaar=segment.toInt();
        data="";
        Clock.setDate(datum); Clock.setMonth(maand);Clock.setYear(jaar);
//        Serial.print(datum);Serial.print("-");Serial.print(maand);Serial.print("-");Serial.println(jaar);
      }
      if(message_id== "T"){
        segment = String(data.substring(0,2));uren=segment.toInt();
        segment = String(data.substring(2,4));minuten=segment.toInt();
        data="";
        Clock.setMinute(minuten); Clock.setHour(uren);
//        Serial.print(uren);Serial.print(":");Serial.println(minuten);
      }
    }
  }
//Interval 1000ms
  if (currentMillis - previousMillisB >= intervalB) {
    previousMillisB = currentMillis;
    datum=Clock.getDate();
    maand=Clock.getMonth(century);
    jaar=Clock.getYear();
    puntjes=!puntjes;
    if(puntjes)dot.fill(DisplayColor,0,2);else dot.fill(0,0,2);
    dot.show();
  }
/**********************************************/
//Interval 500ms
  if (currentMillis - previousMillisC >= intervalC) {
    previousMillisC = currentMillis;
      if((seconden<2)&& ShowDate){
    Digit_1= int(datum/10);
    Digit_2= datum%10;
    Digit_3= int(maand/10);
    Digit_4= maand%10;
  } else {
    Digit_1= int(uren/10);
    Digit_2= uren%10;
    Digit_3= int(minuten/10);
    Digit_4= minuten%10;
  }
  
//  DisplayColor=Spectrum(KleurNummer);
//  strip.clear();
  Segment();
  strip.show();
  }
/**********************************************/
//Interval 250ms
  if (currentMillis - previousMillisA >= intervalA) {
    previousMillisA = currentMillis;
   ReadDS3231();
   ButtonsSet();
    if(regenboog){k=k+1; if(k>255)k=0;KleurNummer=k;} //KleurNummer=KleurNummer&0x7F;}
    DisplayColor=Spectrum(KleurNummer);
  }
/**********************************************/

   if(!digitalRead(Mode))delay(10); 
   if(!digitalRead(Mode)) mode = mode + 1; 
   if(mode > 10)mode = 0;
   while(!digitalRead(Mode)){} 
   strip.clear();
   printOLED();
}

void ReadDS3231(){
  uren=Clock.getHour(h12, PM);
  minuten=Clock.getMinute(); 
  seconden=Clock.getSecond();
}

void welkom(){
    DisplayColor=Spectrum(KleurNummer);

  oled.clear();
  oled.set2X();
  oled.println(" JUMBO LED");
  oled.set1X();
  oled.println("   made in Fryslan ");
  oled.println("   by  TechGraphix ");
  strip.clear();strip.setPixelColor(5,DisplayColor);strip.setPixelColor(12,DisplayColor);strip.setPixelColor(19,DisplayColor);strip.setPixelColor(26,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(0,DisplayColor);strip.setPixelColor(7,DisplayColor);strip.setPixelColor(14,DisplayColor);strip.setPixelColor(21,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(2,DisplayColor);strip.setPixelColor(9,DisplayColor);strip.setPixelColor(16,DisplayColor);strip.setPixelColor(23,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(4,DisplayColor);strip.setPixelColor(3,DisplayColor);strip.setPixelColor(27,DisplayColor);strip.setPixelColor(22,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(1,DisplayColor);strip.setPixelColor(6,DisplayColor);strip.setPixelColor(25,DisplayColor);strip.setPixelColor(24,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(10,DisplayColor);strip.setPixelColor(11,DisplayColor);strip.setPixelColor(20,DisplayColor);strip.setPixelColor(15,DisplayColor);strip.show();
  delay(500);strip.clear();
  strip.setPixelColor(8,DisplayColor);strip.setPixelColor(13,DisplayColor);strip.setPixelColor(18,DisplayColor);strip.setPixelColor(17,DisplayColor);strip.show();
  delay(500);strip.clear();strip.show();oled.clear();
  }

void printOLED(){
  switch (mode){
    case 0: printtijd();printdatum();break;
    case 1: printtijd();oled.println("SET HOURS ");break;
    case 2: printtijd();oled.println("SET MINUTS");break;
    case 3: printtijd();oled.println("RESET SECS");break;
    case 6: oled.setCursor(0,0);printdatum();oled.println(" SET DATE ");break;
    case 5: oled.setCursor(0,0);printdatum();oled.println("SET MONTH ");break;
    case 4: oled.setCursor(0,0);printdatum();oled.println(" SET YEAR ");break;
    case 7: oled.setCursor(0,0);oled.println("RAINBOW   ");if(regenboog)oled.print("CYCLE  ON");else oled.print("CYCLE OFF");break;
    case 8: oled.setCursor(0,0);oled.println("SET COLOR ");oled.print("    ");oled.print(KleurNummer);oled.println("    ");break;
    case 9: oled.setCursor(0,0);oled.println("DISP. DATE");if(ShowDate){oled.println("SHOW DATE");} else oled.println("ONLY TIME");break;
    case 10: oled.setCursor(0,0);oled.println(" AMBIENT  ");if(Ambient){oled.println(" AMBI- ON");} else oled.println(" AMBI-OFF");break;

  }
}
void printdatum(){
    oled.set2X();
    oled.print(" ");
    if(datum<10)oled.print("0");
    oled.print(datum);
    oled.print("-");
    if((maand)<10)oled.print("0");
    oled.print(maand);
    oled.print("-");
    if((jaar)<10)oled.print("0");
    oled.println(jaar);
}
void printtijd() {
    oled.setCursor(0,0);
    oled.set2X();
    oled.print(" ");
    if(uren<10)oled.print("0");
    oled.print(uren);
    oled.print(":");
    if((minuten)<10)oled.print("0");
    oled.print(minuten);
    oled.print(":");
    if((seconden)<10)oled.print("0");
    oled.println(seconden);
}   

void Segment(){
  switch(Digit_1){
    case 0 : nul(0);break;
    case 1 : een(0);break;
    case 2 : twee(0);break;
    case 3 : drie(0);break;
    default : nulll(0);break;  
  }
  switch(Digit_2){
    case 0 : nul(1);break;
    case 1 : een(1);break;
    case 2 : twee(1);break;
    case 3 : drie(1);break;
    case 4 : vier(1);break;
    case 5 : vijf(1);break;
    case 6 : zes(1);break;
    case 7 : zeven(1);break;
    case 8 : acht(1);break;
    case 9 : negen(1);break;
    default : nulll(1);  break;  
  }
  switch(Digit_3){
    case 0 : nul(2);break;
    case 1 : een(2);break;
    case 2 : twee(2);break;
    case 3 : drie(2);break;
    case 4 : vier(2);break;
    case 5 : vijf(2);break;
    default : nulll(2);  break;  
  }   
  switch(Digit_4)
  {
    case 0 : nul(3);break;
    case 1 : een(3);break;
    case 2 : twee(3);break;
    case 3 : drie(3);break;
    case 4 : vier(3);break;
    case 5 : vijf(3);break;
    case 6 : zes(3);break;
    case 7 : zeven(3);break;
    case 8 : acht(3);break;
    case 9 : negen(3);break;
    default : nulll(3);break;  
  }
}

void nul(int a){
   i=a*7;
   strip.setPixelColor(i,zwart);
   strip.fill(DisplayColor,i+1,6);
}
void een(int a){
   i=a*7;
   strip.setPixelColor(i,zwart);
   strip.setPixelColor(i+1,DisplayColor);
   strip.fill(zwart,i+2,4);
   strip.setPixelColor(i+6,DisplayColor);
}
void twee(int a){
   i=a*7;
   strip.setPixelColor(i,DisplayColor);
   strip.setPixelColor(i+1,zwart);
   strip.fill(DisplayColor,i+2,2);
   strip.setPixelColor(i+4,zwart);
   strip.fill(DisplayColor,i+5,2);
}
void drie(int a){
   i=a*7;
   strip.fill(DisplayColor,i,3);
   strip.fill(zwart,i+3,2);
   strip.fill(DisplayColor,i+5,2);
}
void vier(int a){
   i=a*7;
   strip.fill(DisplayColor,i,2);
   strip.fill(zwart,i+2,2);
   strip.setPixelColor(i+4,DisplayColor);
   strip.setPixelColor(i+5,zwart);
   strip.setPixelColor(i+6,DisplayColor);
}
void vijf(int a){
   i=a*7;
   strip.fill(DisplayColor,i,3);
   strip.setPixelColor(i+3,zwart);
   strip.fill(DisplayColor,i+4,2);
   strip.setPixelColor(i+6,zwart);
}
void zes(int a){
   i=a*7;
   strip.fill(DisplayColor,i,6);
   strip.setPixelColor(i+6,zwart);
}
void zeven(int a){
   i=a*7;
   strip.setPixelColor(i,zwart);
   strip.setPixelColor(i+1,DisplayColor);
   strip.fill(zwart,i+2,3);
   strip.fill(DisplayColor,i+5,2);
}
void acht(int a){
   i=a*7;
   strip.fill(DisplayColor,i,7);
}
void negen(int a){
   i=a*7;
   strip.fill(DisplayColor,i,3);
   strip.setPixelColor(i+3,zwart);
    strip.fill(DisplayColor,i+4,3);
}
void nulll(int a){
   i=a*7;
   strip.fill(zwart,i,7);
}

uint32_t Spectrum(uint8_t SpectrumPos) {
  byte rx,gx,bx;
  
  if(SpectrumPos < 85) {
    rx=255 - SpectrumPos * 3; gx=0; bx=SpectrumPos * 3;
    return Color(rx, gx,bx );
  }
  if(SpectrumPos < 170) {
    SpectrumPos -= 170;
    rx=0; gx=SpectrumPos * 3; bx= 255 - SpectrumPos * 3;
    return Color(rx,gx ,bx );
  }
  SpectrumPos -= 170;
   rx=SpectrumPos * 3; gx=255 - SpectrumPos * 3; bx= 0;
   return Color(rx ,gx, bx);
}

uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

void ButtonsSet(){
  int tempset; //void
  switch(mode)
  {
    case 0:
    if(!digitalRead(Plus) && (!flash)){ 
        helderheid=helderheid+1;
        if(helderheid>254)helderheid=254; 
     }
      if(!digitalRead(Min) && (!flash)){ 
        helderheid=helderheid-1;
        if(helderheid <1)helderheid=1; 
      }
     strip.setBrightness(helderheid);dot.setBrightness(helderheid); 
     EEPROM.write(1, helderheid);
      break;
       
    case 1: 
     strip.setBrightness(helderheid); 
      if(!digitalRead(Plus) && (!flash)){ 
        tempset = Clock.getHour(h12, PM); 
        tempset = tempset + 1; 
        if(tempset > 23){tempset = 0;} 
       Clock.setHour(tempset);
      }
      if(!digitalRead(Min) && (!flash)){ 
        tempset = Clock.getHour(h12, PM); 
        if (tempset == 0){tempset = 23;} else tempset = tempset - 1; 
      Clock.setHour(tempset);
      }
      break;

    case 2: 
      strip.setBrightness(helderheid); 
      if(!digitalRead(Plus) && (!flash)){ 
        tempset =Clock.getMinute(); 
        tempset = tempset + 1; 
        if(tempset > 59){tempset = 0;} 
       Clock.setMinute(tempset);
      }
      if(!digitalRead(Min) && (!flash)){ 
        tempset =Clock.getMinute(); 
        if (tempset == 0){tempset = 59;}else tempset = tempset - 1; 
        Clock.setMinute(tempset); 
      }
      break;
      
    case 3: 
        strip.setBrightness(helderheid); 
        if(!digitalRead(Plus) && (!flash)){ 
        Clock.setSecond(0);}
      if(!digitalRead(Min) && (!flash)){ 
        Clock.setSecond(0);
        }
//      EEPROM.write(0,KleurNummer);  
      break;
 case 4: 
      strip.setBrightness(helderheid); 
      if(!digitalRead(Plus) && (!flash)){ 
        tempset =Clock.getYear(); 
        tempset = tempset + 1; 
        if(tempset > 99){tempset = 0;} 
       Clock.setYear(tempset);
      }
      if(!digitalRead(Min) && (!flash)){ 
        tempset =Clock.getYear(); 
        if (tempset < 0){tempset = 99;}else tempset = tempset - 1; 
        Clock.setYear(tempset); 
      }
      schrikkeljaar=false;
      if(tempset%4==0)schrikkeljaar=true;
      break; 
  
case 5: 
      strip.setBrightness(helderheid); 
      if(!digitalRead(Plus) && (!flash)){ 
        tempset =Clock.getMonth(century); 
        tempset = tempset + 1; 
        if(tempset > 12){tempset = 1;} 
       Clock.setMonth(tempset);
      }
      if(!digitalRead(Min) && (!flash)){ 
        tempset =Clock.getMonth(century); 
        tempset = tempset - 1;
        if (tempset <1 ){tempset = 12;}
        Clock.setMonth(tempset); 
      }
      switch(tempset){
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxdagen = 31; break;
        case 2: maxdagen= 28; if(schrikkeljaar)maxdagen++; break;
        case 4: case 6: case 9: case 11: maxdagen = 30; break;
      }
//      Serial.println(maxdagen);
      break;
 case 6: 
      strip.setBrightness(helderheid); 
      if(!digitalRead(Plus) && (!flash)){ 
        tempset =Clock.getDate(); 
        tempset = tempset + 1; 
        if(tempset > maxdagen){tempset = 1;} 
       Clock.setDate(tempset);
      }
      if(!digitalRead(Min) && (!flash)){ 
        tempset =Clock.getDate(); 
        tempset = tempset - 1; 
        if (tempset < 1)tempset = maxdagen; 
        Clock.setDate(tempset); 
      }
      break;

case 7:
     if(!digitalRead(Plus) && (!flash)){
     regenboog=true;
     EEPROM.write(2, regenboog);
     }
     if(!digitalRead(Min) && (!flash)){
     regenboog=false;
     EEPROM.write(2, regenboog);
     }
     break;


case 8: if(regenboog){ mode=9;break;}

        strip.setBrightness(helderheid); 
        if(!digitalRead(Plus) && (!flash)){ 
        KleurNummer = KleurNummer + 1; 
        if(KleurNummer > 255){KleurNummer = 0;} 
        DisplayColor=Spectrum(KleurNummer);
      }
      if(!digitalRead(Min) && (!flash)){ 
        if (KleurNummer == 0){KleurNummer = 255;}else KleurNummer = KleurNummer - 1; 
        DisplayColor = Spectrum(KleurNummer);
        }
      EEPROM.write(0,KleurNummer);  
      break;
      
   
   case 9: 
     if(!digitalRead(Plus) && (!flash)){
     ShowDate=true;
     EEPROM.write(3, ShowDate);
     }
     if(!digitalRead(Min) && (!flash)){
     ShowDate=false;
     EEPROM.write(3, ShowDate);
     }
     break;

     case 10:
     if(!digitalRead(Plus) && (!flash)){
     Ambient=true;
     EEPROM.write(4, Ambient);
     }
     if(!digitalRead(Min) && (!flash)){
     Ambient=false;
     EEPROM.write(4, Ambient);
     }
     break;
   }
}

A Neopixel tutorial would be a great starting point, e.g. Adafruit.

1 Like

Thanks for your quick response.

When you say “skip that” would that require me to comment out anything that doesn’t apply to my project? Referring to the Bluetooth module?

schrikkeljaar

leap year, haha.

a7

Correct... absorb what is usefull.. You will find i define every number to light up specific parts of the digit.. You have to puzzle out which leds in that sequence of every digit are appropriate for you.. Best to keep the same sequence in every digit and preferably a-b-c-d-e-f-g

@alto777 yes, i wrote this partially in dutch, not knowing than anyone else would be interested..

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.