Pages: [1]   Go Down
Author Topic: Trouble with adding shift register to my binary clock  (Read 624 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
// 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


« Last Edit: July 15, 2012, 12:01:55 am by MCFan » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
  }
The 1 second delay in the seconds loop is killing the rest of your clock. It HAS to go. You need to restructure this code to show the value in second. There should be no loop involved, and most certainly no delay().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see what you mean now about that delay negating the rest of my loop. So i changed that and added a 'seconds' variable and i've got it to update with the rest of the leds now. It looks like everything is working now. thx for your tip it got me goin in the right direction.

Code:
// Binary Clock

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;
int secondCount = 0;


byte data;
byte dataArray[60];

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);

    dataArray[0]  = 0x00;
    dataArray[1]  = 0x01;
    dataArray[2]  = 0x02;
    dataArray[3]  = 0x03;
    dataArray[4]  = 0x04;
    dataArray[5]  = 0x05;
    dataArray[6]  = 0x06;
    dataArray[7]  = 0x07;
    dataArray[8]  = 0x08;
    dataArray[9]  = 0x09;
    dataArray[10] = 0x10;
    dataArray[11] = 0x11;
    dataArray[12] = 0x12;
    dataArray[13] = 0x13;
    dataArray[14] = 0x14;
    dataArray[15] = 0x15;
    dataArray[16] = 0x16;
    dataArray[17] = 0x17;
    dataArray[18] = 0x18;
    dataArray[19] = 0x19;
    dataArray[20] = 0x20;
    dataArray[21] = 0x21;
    dataArray[22] = 0x22;
    dataArray[23] = 0x23;
    dataArray[24] = 0x24;
    dataArray[25] = 0x25;
    dataArray[26] = 0x26;
    dataArray[27] = 0x27;
    dataArray[28] = 0x28;
    dataArray[29] = 0x29;
    dataArray[30] = 0x30;
    dataArray[31] = 0x31;
    dataArray[32] = 0x32;
    dataArray[33] = 0x33;
    dataArray[34] = 0x34;
    dataArray[35] = 0x35;
    dataArray[36] = 0x36;
    dataArray[37] = 0x37;
    dataArray[38] = 0x38;
    dataArray[39] = 0x39;
    dataArray[40] = 0x40;
    dataArray[41] = 0x41;
    dataArray[42] = 0x42;
    dataArray[43] = 0x43;
    dataArray[44] = 0x44;
    dataArray[45] = 0x45;
    dataArray[46] = 0x46;
    dataArray[47] = 0x47;
    dataArray[48] = 0x48;
    dataArray[49] = 0x49;
    dataArray[50] = 0x50;
    dataArray[51] = 0x51;
    dataArray[52] = 0x52;
    dataArray[53] = 0x53;
    dataArray[54] = 0x54;
    dataArray[55] = 0x55;
    dataArray[56] = 0x56;
    dataArray[57] = 0x57;
    dataArray[58] = 0x58;
    dataArray[59] = 0x59;

}

void loop() {
 
static unsigned long lastTick = 0;

if (millis() - lastTick >= 1000) {
  lastTick = millis();
  second++;

}
  if (second >= 60) {
  minute++;
  second = 0;
}

if (minute >=60) {
  hour++;
  minute = 0;
}

if (hour >=24) {
  hour=0;
  minute=0;
}

  munit = minute%10;
  hunit = hour%10;

  ledstats = digitalRead(dsply); 
  if (ledstats == HIGH){
    light=!light;
    delay(250);
  }
 
    if(light== HIGH){
  for(i=1;i<=13;i++){
  digitalWrite(i, HIGH);}
 
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
 
  } else  {
   
  setSeconds(second);

  //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);}
  }
 

  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(int dataPin, int clockPin, byte dataOut) {
 
  boolean pinState;
 
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);

  for (i=0; i<=6; i++)  {
    digitalWrite(clockPin, LOW);

    if ( dataOut & (1<<i) ) {
      pinState= HIGH;
    } else {
      pinState= LOW;
    }
    digitalWrite(dataPin, pinState);
    digitalWrite(clockPin, HIGH);
    digitalWrite(dataPin, LOW);
  }
  digitalWrite(clockPin, LOW);
}


void setSeconds(int second) {
    data = dataArray[second];
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, data);   
    digitalWrite(latchPin, HIGH);
}
Logged

Pages: [1]   Go Up
Jump to: