4
\$\begingroup\$

This code is designed for my 30 LED NeoPixel clock, the reason for not using the ds3231 library is due to the fact that I use codebender to download programs to my Arduino, I wanted some critique on things that could be improved and any glaring problems that should be fixed.

/*This code is for a 30 LED NeoPixel Clock, it includes code 
for IR remote control of a solenoid controlled RGB lamp and 
the colors and position of them on the clock along with brightness
Base code for clock with no library comes from: 
https://forum.arduino.cc/index.php?topic=514601.0
*/

#include "Wire.h"
#include <Adafruit_NeoPixel.h>
#include <IRremote.h>
#include <FastLED.h>

//Clock Timing Functions
#define DS3231_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
  return ( (val / 16 * 10) + (val % 16) );
}

//LED Strip Defint
CRGB leds[30];


//Color Values for LEDS
int rMin =0, gMin=255, bMin=0;
int rHour=0, gHour=0, bHour=255;
int rSec=255, gSec=0, bSec=0;
CRGB c1, c2, c3;
CRGB min,sec,hr;
bool onOff, pallete;
int cCount,lCount;

//Values to keep time
int tMin1, tMin2;
int hourNum1, hourNum2, hourNum3;

//IR setup
IRrecv irrecv(2);
decode_results result;

void setup(){
    //Pins for solenoid control
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    
    //Setting starting Colors
    min=CRGB(255,0,0);
    sec=CRGB(0,255,0);
    hr=CRGB(0,0,255);
    
    Wire.begin();
    //Neopixel Declaration
    FastLED.addLeds<NEOPIXEL, 9>(leds, 30).setCorrection( TypicalLEDStrip );
    
 

    FastLED.clear();
    FastLED.show();
    Serial.begin(9600);
    irrecv.enableIRIn();
  
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,17,15,7,18,11,17);

}
//More Time Code
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year){
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}

void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year){
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

void displayTime(){
  
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);


}

//Loop Here
void loop()
{
    //Serial.println(String(hour)+":"+String(minute)+":"+String(second));
    displayTime(); // display the real-time clock data on the Serial Monitor,
    
    
    //If reciever IR translate it, otherwise run through time code
    if (irrecv.decode(&result)) {
            //delay(100);
            Serial.println(result.value);
            remoteCodes(); //Method holding if statement of IR values
            irrecv.resume(); // Receive the next value
            //delay(100);
    }else{
        
        //Call methods to display the time
        displaySeconds();
        displayMinutes();  
        displayHours();
        displayOverlap();
    }
    delay(100);
    //Show updated pattern
    FastLED.show(); 
}

//Code to generate hour number positions and display them
void hourNumbers() {
  if (hour == 1 || hour == 13) {
    hourNum1 = 2;
    hourNum2 = 2;
    hourNum3 = 1;
  } else if (hour == 2 || hour == 14) {
    hourNum1 = 4;
    hourNum2 = 5;
    hourNum3 = 2;
  } else if (hour == 3 || hour == 15) {
    hourNum1 = 7;
    hourNum2 = 7;
    hourNum3 = 3;
  } else if (hour == 4 || hour == 16) {
    hourNum1 = 9;
    hourNum2 = 10;
    hourNum3 = 4;
  } else if (hour == 5 || hour == 17) {
    hourNum1 = 12;
    hourNum2 = 12;
    hourNum3 = 5;
  } else if (hour == 6 || hour == 18) {
    hourNum1 = 14;
    hourNum2 = 15;
    hourNum3 = 6;
  } else if (hour == 7 || hour == 19) {
    hourNum1 = 17;
    hourNum2 = 17;
    hourNum3 = 7;
  } else if (hour == 8 || hour == 20) {
    hourNum1 = 19;
    hourNum2 = 20;
    hourNum3 = 8;
  } else if (hour == 9 || hour == 21) {
    hourNum1 = 22;
    hourNum2 = 22;
    hourNum3 = 9;
  } else if (hour == 10 || hour == 22) {
    hourNum1 = 24;
    hourNum2 = 25;
    hourNum3 = 10;
  } else if (hour == 11 || hour == 23) {
    hourNum1 = 27;
    hourNum2 = 27;
    hourNum3 = 11;
  } else if (hour == 12 || hour == 00) {
    hourNum1 = 29;
    hourNum2 = 0;
    hourNum3 = 12;
  }
}
void displayHours(){
    hourNumbers();
    leds[hourNum1]=CRGB(hr);
    leds[hourNum2]=CRGB(hr);
}

//Code to generate minute number positions and display them
void minuteNumbers() {
  tMin1 = minute / 2;
  if (minute % 2 == 0)
    tMin2 = minute / 2 - 1;
  else
    tMin2 = minute / 2;
  if (tMin2 == -1)
    tMin2 = 29;
}
void displayMinutes(){
    minuteNumbers();
    leds[tMin1]=CRGB(min);
    leds[tMin2]=CRGB(min);
}


//Code that sets overlapping minutes and hours to 
void displayOverlap(){
    if(minute>=30){
        leds[hourNum2+1]=hr;
        if(tMin1==hourNum2+1)
            leds[hourNum2+1]=CRGB(255,255,255);
        else if(tMin2==hourNum2+1)
            leds[hourNum2+1]=CRGB(255,255,255);
    }
    
    if(tMin1==hourNum1)
        leds[tMin1]=CRGB(255,255,255);
    else if(tMin1==hourNum2)
        leds[tMin1]=CRGB(255,255,255);
    else if(tMin2==hourNum1)
        leds[tMin2]=CRGB(255,255,255);
    else if(tMin2==hourNum2)
        leds[tMin2]=CRGB(255,255,255);
}


//Animation run at the hour
void hourAnimation(){
    for (int j = 0; j < hourNum3; j++) {
        for (int i = 0; i < 30; i++) {
          leds[i]= CRGB(255, 255, 255);
          leds[i-3]=CRGB(0,0,0);
          if (i == 0)
            leds[27]==CRGB(0,0,0);
          if (i == 1)
            leds[28]==CRGB(0,0,0);
          if (i == 2)
            leds[29]==CRGB(0,0,0);
           FastLED.show();
          delay(15);
        }
    }
}

//Code to display the seconds
void displaySeconds(){
    if(second<=1){
        for(int i=30; i>second/2; i--){
            Serial.println("A");
            leds[i]=CRGB(0,0,0);
            displayHours();
            displayMinutes();
            displayOverlap();
            delay(35);
            FastLED.show();
        }
    }else{
    
        for(int i=1; i<=second; i++){
            if(i%2!=0){
                leds[i/2]=CRGB(sec);
                leds[i/2].fadeToBlackBy(225);
            }
            leds[i/2-1]=CRGB(sec);
        }
    }
}


//If statement method for IR values recieved
void remoteCodes(){
    if(result.value==551508135){
        //"Mutes" the LEDS
        if(onOff==true){
            onOff=!onOff;
            FastLED.setBrightness(0);
        }else{
            onOff=!onOff;
            FastLED.setBrightness(255);
        }
        Serial.println("Mute");


    }else if(result.value==551485695){
        //Increases Brightess
        if(FastLED.getBrightness()+25>255)
            FastLED.setBrightness(255);
        else
            FastLED.setBrightness(FastLED.getBrightness()+25);
        onOff=false;
        Serial.println("UpBrightness");
    
    }else if(result.value==551518335){
        //Decreases Brightness
        if(FastLED.getBrightness()-25<0){
            FastLED.setBrightness(0);
            onOff=true;
        }else
            FastLED.setBrightness(FastLED.getBrightness()-25);
        
        Serial.println("DownBrightness");
        
    }else if(result.value==551520375){
        //First Color Values
        rSec = (255); gSec = (0); bSec = (0);
        rMin = (0); gMin = (255); bMin = (0);
        rHour = (0); gHour = (0); bHour = (255);
        cCount=0;
        colorSwap();
        Serial.println("1");
        
    }else if(result.value==0000){
        //Second Color Values
        rSec = (235); gSec = (22); bSec = (242);
        rMin = (29); gMin = (242); bMin = (22);
        rHour = (242); gHour = (128); bHour = (22);
        cCount=0;
        colorSwap();
        Serial.println("2");
        
    }else if(result.value==000){
        //Third Color Values
        rSec = (38); gSec = (70); bSec = (83);
        rMin = (233); gMin = (196); bMin = (106);
        rHour = (231); gHour = (111); bHour = (81);
        cCount=0;
        colorSwap();
        Serial.println("3");
        
    }else if(result.value==551495895){
        //Fourth Color Values
        rSec = (0); gSec = (255); bSec = (255);
        rMin = (255); gMin = (0); bMin = (255);
        rHour = (255); gHour = (255); bHour = (0);
        cCount=0;
        colorSwap();
        Serial.println("4");
        
    }else if(result.value==000){
        //Fith Color Values
        rSec = (64); gSec = (93); bSec = (99);
        rMin = (156); gMin = (77); bMin = (60);
        rHour = (105); gHour = (139); bHour = (104);
        cCount=0;
        colorSwap();
        Serial.println("5");
        
    }else if(result.value==551512215){
        //Sixth Color Values
        rHour = (5); gHour = (148); bHour = (33);
        rSec = (148); gSec = (34); bSec = (5);
        rMin = (34); gMin = (5); bMin = (148);
        cCount=0;
        colorSwap();
        Serial.println("6");
        
    }else if(result.value==551536695){
        //Seventh Color Values
        rSec = 86; gSec = 18; bSec = 204;
        rMin = 204; gMin = 192; bMin = 18;
        rHour = 204; gHour = 98; bHour = 18;
        cCount=0;
        colorSwap();
        Serial.println("2");
        
    }else if(result.value==551528535){
        //Eight Color Values
        rSec = 255; gSec = 3; bSec = 192;
        rMin = 3; gMin = 255; bMin = 192;
        rHour = 66; gHour = 255; bHour = 3;
        cCount=0;
        colorSwap();
        Serial.println("5");
        
    }else if(result.value==551504055){
        //Ninth Color Values
        rSec = 5; gSec = 148; bSec = 33;
        rMin = 148; gMin = 34; bMin = 5;
        rHour = 35; gHour = 5; bHour = 148;
        cCount=0;
        colorSwap();
        Serial.println("3");
    
    }else if(result.value==551544345){
        //Unused Button
        cCount=0;
        Serial.println("PIC");
        
    }else if(result.value==551487735){
        //Changes Solenoid Controlled Lamp
        Serial.println("0");
        lampSwap();
        
    }else if(result.value==551550720){
        //Swaps the Colors Around the Clock
        colorSwap();
        Serial.println("-");
        
    }else if(result.value==551540775){
        //Alt Color Swap
        colorSwap();
        Serial.println("info");
        
    }else if(result.value==551544855){
        //7th
        rHour=random(0,255); gHour=random(0,255); bHour=random(0,255);
        Serial.println("7");
        cCount=0;
        colorSwap();
    }else if(result.value==551491815){
        //8th
        rMin=random(0,255); gMin=random(0,255); bMin=random(0,255); 
        Serial.println("8");
        cCount=0;
        colorSwap();
    }else if(result.value==551524455){
        //9th
        rSec=random(0,255); gSec=random(0,255); bSec=random(0,255);
        Serial.println("9");
        cCount=0;
        colorSwap();
    }
    
    
    //Serial.println(" is "+String()+" gMin is "+String(gMin)+"  is "+String());
    
}

//Code to cycle through lamp colors
void lampSwap(){
    switch(lCount){
            case 0:{
                lCount+=1;
                digitalWrite(10,HIGH);
                digitalWrite(11,HIGH);
                break;
                
            }
            case 1:{
                lCount+=1;
                digitalWrite(10,HIGH);
                digitalWrite(11,LOW);
                break;
            }
            case 2:{
                lCount+=1;
                digitalWrite(10,LOW);
                digitalWrite(11,HIGH);
                break;
            }
            case 3:{
                lCount=0;
                digitalWrite(10,LOW);
                digitalWrite(11,LOW);
                break;
            }
        }
}


//Code to cycle through Hour-Min-Second-Colors
void colorSwap(){
    FastLED.clear();
    Serial.println("ColorSwapped");
    Serial.println(cCount);
    Serial.println("="+String(rMin)+" GMIN="+String(gMin)+" ="+String(bMin));
    switch(cCount){
        case 0:{
            Serial.println("SUCCESS1");
            sec=CRGB(rSec,gSec,bSec);
            min=CRGB(rMin,gMin,bMin);
            hr=CRGB(rHour,gHour,bHour);
            break;
            
        }
        case 1:{
            Serial.println("SUCCESS2");
            hr=CRGB(rSec,gSec,bSec);
            sec=CRGB(rMin,gMin,bMin);
            min=CRGB(rHour,gHour,bHour);
            break;
        }
        case 2:{
            Serial.println("SUCCESS3");
            min=CRGB(rSec,gSec,bSec);
            hr=CRGB(rMin,gMin,bMin);
            sec=CRGB(rHour,gHour,bHour);
            break;
        }
        case 3:{
            Serial.println("SUCCESS4");
            hr=CRGB(rSec,gSec,bSec);
            sec=CRGB(rMin,gMin,bMin);
            min=CRGB(rHour,gHour,bHour);
            break;
            
        }
        case 4:{
            Serial.println("SUCCESS5");
            sec=CRGB(rSec,gSec,bSec);
            hr=CRGB(rMin,gMin,bMin);
            min=CRGB(rHour,gHour,bHour);
            break;
        }
        case 5:{
            Serial.println("SUCCESS6");
            min=CRGB(rSec,gSec,bSec);
            sec=CRGB(rMin,gMin,bMin);
            hr=CRGB(rHour,gHour,bHour);
            break;
        }
    }
    
    if(cCount==5)
        cCount=0;
    else
        cCount+=1;
    //Serial.println("="+String()+" GMIN="+String(gMin)+" ="+String());
    
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.