Hello all,
I created a simple binary clock (following this guide http://www.instructables.com/id/LED-Binary-Clock-1/). It has 4 columns of leds with 2, 4, 3, and 4 leds in each respectively like most of binary clocks i've noticed. It also has 3 momentary switches; one for turning the leds off and two for hour and minute adjustment.
Now i wanted to add some more complexity to it and add a counter for the seconds display which will be a row of 6 leds underneath the hours and minutes leds. For that i planned on using a shift register because i'm already out of i/o pins. So i remembered one of the first projects i did with a shift register and it was a binary counter. with a little modification i got it to count to 60 and i added the code where i thought it should go in my binary clock. but all i actually get is the binary counter working that i added and the minutes start counting in the hours column. Also none of the buttons respond. I can run each one ( the binary clock or brinary counter) individually by just uploading there code and each one works fine so, i'm assuming my wiring is not the problem and its somewhere in my cobbled together code. I've tried a few different ways to embed the binary counter code into it but i'm starting to wonder if i need to include the code that controls the secounds leds into the else function at the end of the code, instead of after, but i'm not sure how to do that. any help of tips would be appreciated.
// 8bit counter
int latchPin= 15; // pin connected to pin 12 on register LATCH
int clockPin= 14; // pin connected to pin 11 on register CLOCK
int dataPin= 16; // pin connected to pin 14 on register DATA
int minbtn= 17;
int hourbtn= 18;
int dsply= 19; //turn leds on/off
int second=0, minute=0, hour=0; //start the time on 00:00:00
int munit = 0;
int hunit = 0;
int valm=0;
int valh=0;
int ledstats = 0;
int i= 0;
boolean light = 1;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(1, OUTPUT);pinMode(2, OUTPUT);pinMode(3, OUTPUT);pinMode(4, OUTPUT);pinMode(5, OUTPUT);
pinMode(6, OUTPUT);pinMode(7, OUTPUT);pinMode(8, OUTPUT);pinMode(9, OUTPUT);pinMode(10, OUTPUT);
pinMode(11, OUTPUT);pinMode(12, OUTPUT);pinMode(13, OUTPUT);
pinMode(minbtn, INPUT);
pinMode(hourbtn, INPUT);
pinMode(dsply, INPUT);
digitalWrite(minbtn, HIGH);
digitalWrite(hourbtn, HIGH);
digitalWrite(dsply, HIGH);
}
void loop() {
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// move forward one second every 1000 milliseconds
if (millis() - lastTick >= 1000) {
lastTick = millis();
second++;
}
// move forward one minute every 60 seconds
if (second >= 60) {
minute++;
second = 0; // reset seconds to zero
}
// move forward one hour every 60 minutes
if (minute >=60) {
hour++;
minute = 0; // reset minutes to zero
}
if (hour >=24) {
hour=0;
minute = 0; // reset minutes to zero
}
munit = minute%10; //sets the variable munit and hunit for the unit digits
hunit = hour%10;
ledstats = digitalRead(dsply); // read input value, for setting leds off, but keeping count
if (ledstats == HIGH){
light=!light;
delay(250);
}
if(light== HIGH){
for(i=1;i<=13;i++){
digitalWrite(i, HIGH);}
} else {
//minutes units
if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) { digitalWrite(1, LOW);} else { digitalWrite(1, HIGH);}
if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {digitalWrite(2, LOW);} else {digitalWrite(2,HIGH);}
if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {digitalWrite(3, LOW);} else {digitalWrite(3,HIGH);}
if(munit == 8 || munit == 9) {digitalWrite(4, LOW);} else {digitalWrite(4,HIGH);}
//minutes
if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60)) {digitalWrite(5, LOW);} else {digitalWrite(5,HIGH);}
if(minute >= 20 && minute < 40) {digitalWrite(6, LOW);} else {digitalWrite(6,HIGH);}
if(minute >= 40 && minute < 60) {digitalWrite(7, LOW);} else {digitalWrite(7,HIGH);}
//hour units
if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {digitalWrite(8, LOW);} else {digitalWrite(8,HIGH);}
if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {digitalWrite(9, LOW);} else {digitalWrite(9,HIGH);}
if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {digitalWrite(10, LOW);} else {digitalWrite(10,HIGH);}
if(hunit == 8 || hunit == 9) {digitalWrite(11, LOW);} else {digitalWrite(11,HIGH);}
//hour
if(hour >= 10 && hour < 20) {digitalWrite(12, LOW);} else {digitalWrite(12,HIGH);}
if(hour >= 20 && hour < 24) {digitalWrite(13, LOW);} else {digitalWrite(13,HIGH);}
}
for (int i = 0; i < 60; i++) { //count from 0 to 255
digitalWrite(latchPin, LOW); //set latch pin to low to allow data flow
shiftOut(i); // sends the next value from the function shitOut
digitalWrite(latchPin, HIGH); //lock latch pin to send data
delay(1000); //delay between bytes that are sent to regisiter
}
valm = digitalRead(minbtn); // add one minute when pressed
if(valm== HIGH) {
minute++;
second=0;
delay(250);
}
valh = digitalRead(hourbtn); // add one hour when pressed
if(valh==HIGH) {
hour++;
second=0;
delay(250);
}
}
void shiftOut(byte dataOut) { //shifts out 8 bits LSB first on rising edge of clock
boolean pinState;
digitalWrite(dataPin, LOW); //clear shift register, ready for sending data
digitalWrite(clockPin, LOW);
for (int i=0; i<=7; i++) { // for every bit in dataOut send out a bit
digitalWrite(clockPin, LOW);
if ( dataOut & (1<<i) ) { // if the value of dataOut and a bitmask are true, set pinState to 1 (HIGH)
pinState = HIGH;
}
else {
pinState = LOW;
}
digitalWrite(dataPin, pinState); //set dataPin to high or low depending on pinState
digitalWrite(clockPin, HIGH);
}
digitalWrite(clockPin, LOW); //stop shifting out data
}
this is my first time posting on a forum to ask for help regarding an issue with C++ programming and the arduino so if i forget some obviously needed info for you to troubleshoot this just let me know.
also i'm not against running everything off shift registers i just don't understand them enough to rewrite the binary clock code to function with them. but i have a suspicion it might be easier that way