#include <TM1637.h>
// Library: "TM1637_RT", https://github.com/RobTillaart/TM1637_RT
// C++ code
//bitshift 1
const int SER1 = 53; //DS
const int RCLK1 = 52; //STCP
const int SRCLK1 = 51; //SHCP
//bitshift 2
const int SER2 = 50; //DS
const int RCLK2 = 49; //STCP
const int SRCLK2 = 48; //SHCP
//bitshift 3
const int SER3 = 47; //DS
const int RCLK3 = 46; //STCP
const int SRCLK3 = 45; //SHCP
//bitshift 4
const int SER4 = 44; //DS
const int RCLK4 = 42; //STCP
const int SRCLK4 = 41; //SHCP
//bitshift 5
const int SER5 = 40; //DS
const int RCLK5 = 39; //STCP
const int SRCLK5 = 38; //SHCP
//bitshift 6
const int SER6 = 37; //DS
const int RCLK6 = 36; //STCP
const int SRCLK6 = 35; //SHCP
//bitshift 7
const int SER7 = 34; //DS
const int RCLK7 = 33; //STCP
const int SRCLK7 = 32; //SHCP
//bitshift 8
const int SER8 = 31; //DS
const int RCLK8 = 30; //STCP
const int SRCLK8 = 29; //SHCP
const int ledRowLength = 50;
//test
TM1637 TM1, TM2;
//hour and minute display
const int CLK_hh_mm = 4;
const int DIO_hh_mm = 5;
//second and decisecond display
const int CLK_ss_ds = 2;
const int DIO_ss_ds = 3;
unsigned long previousMillisClock; // for the millis timer of the first display
const unsigned long intervalClock = 200; //microseconds 1000 + 1ms CHANGE TO 200MS
bool colon = true; // for a toggling of the colon in the middle.
const int numbers[]={1,2,4,8,16,32,64,128};
const int SER[] = {SER1,SER2,SER3,SER4,SER5,SER6,SER7,SER8};
const int RCLK[] = {RCLK1, RCLK2, RCLK3, RCLK4, RCLK5, RCLK6, RCLK7, RCLK8};
const int SRCLK[] = {SRCLK1, SRCLK2, SRCLK3, SRCLK4, SRCLK5, SRCLK6, SRCLK7, SRCLK8};
int i;
void setup()
{
Serial.begin(115200);
Serial.println("Type your text for the third display.");
Serial.println("Now displaying \"----\".");
// -----------------------------------------
// First display
// -----------------------------------------
TM1.begin(CLK_hh_mm, DIO_hh_mm, 4); // clockpin, datapin, #digits
TM1.displayClear();
TM1.setBrightness(7); // full brightness, default is 3
// -----------------------------------------
// Second display
// -----------------------------------------
TM2.begin(CLK_ss_ds, DIO_ss_ds, 4); // clockpin, datapin, #digits
TM2.displayClear();
TM2.setBrightness(7);
//LED setup
for(int i=29; i<54; i++){
if(i == 43) continue;
pinMode(i, OUTPUT);
}
}
long counter = 0;
unsigned int centiseconds = 0;
unsigned int hour, minutes, second, decisecond;
long timeLeft = 0;
long t=0;
void loop(){
unsigned long long loopTimer = micros();
for(int j=0; j<8; j++){
for (int i=0; i<8; i++){
if((8*j) + i >= ledRowLength){
break;
}
unsigned long long innerLoopStart = micros();
int number = numbers[i]; //binary number to write the
//if new bitshift registry, write 0 to previous bitshift
if(i==0){
if(j){//if j > 0;
digitalWrite(RCLK[j-1], LOW);
shiftOut(SER[j-1], SRCLK[j-1], MSBFIRST, 0);
digitalWrite(RCLK[j-1], HIGH);
}
}
//If one of the two lights on the 7th bitshift should be lit need to also
// account for the decisecond time the same bitshifter controlls.
if(j==6){
if(centiseconds!=0 && centiseconds < 3){
number += numbers[5+centiseconds];
}
}
//Write to the bitshift to controll LED
digitalWrite(RCLK[j], LOW);
shiftOut(SER[j], SRCLK[j], MSBFIRST, number);
digitalWrite(RCLK[j], HIGH);
//counter code
if(decisecond != (counter/500) % 10){
second = (counter/5000) % 60;
decisecond = (counter/500) % 10;
TM2.displayTime(second, decisecond, colon);
if(minutes != ((counter/300000) % 60)){
hour = (counter/18000000) % 24;
minutes = (counter/300000) % 60;
TM1.displayTime(hour, minutes, colon); // display the numbers
}
}
counter+=1; //might overflow at some point
long innerLoopDuration = micros() - innerLoopStart;
timeLeft = intervalClock - (micros() - innerLoopStart);
//Serial.println(innerLoopDuration);
//delay(10);
//using whileloop to negate wakeuptime
while(timeLeft > 0){
timeLeft = intervalClock - (micros() - innerLoopStart);
}
delay(100);
long iterationTime = micros()-innerLoopStart;
//Serial.println(iterationTime);
}
}
centiseconds += 1;
if(centiseconds>10) centiseconds = 1;
if(centiseconds>2){
digitalWrite(RCLK7, LOW);
shiftOut(SER7, SRCLK7, MSBFIRST, 0);
digitalWrite(RCLK7, HIGH);
digitalWrite(RCLK8, LOW);
shiftOut(SER8, SRCLK8, MSBFIRST, numbers[centiseconds - 3]);
digitalWrite(RCLK8, HIGH);
}
else{
digitalWrite(RCLK8, LOW);
shiftOut(SER8, SRCLK8, MSBFIRST, 0);
digitalWrite(RCLK8, HIGH);
digitalWrite(RCLK7, LOW);
shiftOut(SER7, SRCLK7, MSBFIRST, numbers[5 + centiseconds]);
digitalWrite(RCLK7, HIGH);
}
unsigned long loopTime = micros()-loopTimer;
Serial.println(loopTime);
}