Help with Binary Clock program

Hi Folks,

I’m very new to Arduino programming and admittedly not great at programming period.

I have posted the programming i’m using which I got from instructables. What I want to do is add an am/pm LED.

I want PIN13 to be the AM/PM LED.

Any help would be greatly appreciated.

/*
An open-source binary clock for Arduino. 
http://www.instructables.com/id/LED-Binary-Clock-1/
http://www.instructables.com/member/emihackr97/
*/

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() { //set outputs and inputs
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(18, INPUT);
pinMode(14, INPUT);
pinMode(19, INPUT);
digitalWrite(14, HIGH);
digitalWrite(19, HIGH);
digitalWrite(18, 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 >=12) {
  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(18);  // read input value, for setting leds off, but keeping count
  if (ledstats == LOW){
    light=!light;
    delay(250);
  }
    
    if(light== LOW){
  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);}

  }

  valm = digitalRead(14);    // add one minute when pressed
   if(valm== LOW) {
   minute++;
   second=0;
   delay(250);
  }
  
  valh = digitalRead(19);    // add one hour when pressed
   if(valh==LOW) {
   hour++;
   second=0;
   delay(250);
  }


}

See where it says

if (hour >=12) 
{
  hour=0;
  minute = 0; // reset minutes to zero
}

I'm guessing it's a 12 hour clock, reseting at the 12th hour. My quick add would be to declare this at the top: boolean AM = true;

and then modify the code at the 12 hour part here to look like this

if (hour >=12) 
{
  hour=0;
  minute = 0; // reset minutes to zero
  if(AM)
  {
     AM = false;
  }
  else
  {
     AM = true;
  }
}

What this does is alternates the boolean variable "AM" between true and false everytime the hour hits 12. Now you just have to do something with this variable to make pin 13 light up.

Somewhere in your loop add something along this line. if(AM) pin 13 = HIGH; else pin 13 = LOW;

If you gave your clock a preset time, say 10:54 AM, then pin 13 ON would mean AM. If you gave your preset time as 10:54 PM, then pin 13 ON would mean PM. The lighting indication simply depends on how you set your clock.

That makes sense!!

Thanks a lot. I will try this today

That worked great!

Now what I want to do is add minute LEDS. From what I understand I’m going to need to use a shift register because I’m going to need a total of 20 pins

7 for the seconds
7 for the minutes
5 for the hour and 1 for the AM/PM light

I’m not sure how I would program the shift registers?

Here is my code

]


int second=0, minute=0, hour=0; //start the time on 00:00:00
int munit = 0;
int hunit = 0;
int sunit = 0;
int valm=0;
int valh=0;
int ledstats = 0;
int i= 0;
boolean light = 1;
boolean AM = true;


void setup() { //set outputs and inputs
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(18, INPUT);
pinMode(14, INPUT);
pinMode(19, INPUT);
digitalWrite(14, HIGH);
digitalWrite(19, HIGH);
digitalWrite(18, 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 >=12) {
  hour=0;
  minute = 0; // reset minutes to zero
  
 
 if(AM)
  {
     AM = false;
  }
  else
  {
     AM = true;
  }

}



  munit = minute%10; //sets the variable munit and hunit for the unit digits
  hunit = hour%10;
  sunit = second%10;


  ledstats = digitalRead(18);  // read input value, for setting leds off, but keeping count
  if (ledstats == LOW){
    light=!light;
    delay(250);
  }
    
    if(light== LOW){
  for(i=1;i<=13;i++){
  digitalWrite(i, HIGH);}
  
  
  
  } else  {
   
  

  //second units
  if(sunit == 1 || sunit == 3 || sunit == 5 || sunit == 7 || sunit == 9) {  digitalWrite(1, LOW);} else {  digitalWrite(1, HIGH);}
  if(sunit == 2 || sunit == 3 || sunit == 6 || sunit == 7) {digitalWrite(2, LOW);} else {digitalWrite(2,HIGH);}
  if(sunit == 4 || sunit == 5 || sunit == 6 || sunit == 7) {digitalWrite(3, LOW);} else {digitalWrite(3,HIGH);}
  if(sunit == 8 || sunit == 9) {digitalWrite(4, LOW);} else {digitalWrite(4,HIGH);}

  //minutes 
  if((second >= 10 && second < 20) || (second >= 30 && second < 40) || (second >= 50 && second < 60))  {digitalWrite(5, LOW);} else {digitalWrite(5,HIGH);}
  if(second >= 20 && second < 40)  {digitalWrite(6, LOW);} else {digitalWrite(6,HIGH);}
  if(second >= 40 && second < 60) {digitalWrite(7, LOW);} else {digitalWrite(7,HIGH);}
  
    

  //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  )  {digitalWrite(12, LOW);} else {digitalWrite(12,HIGH);}
 
 

  }
  
   if(AM){
   digitalWrite(13, HIGH);}
else{
  digitalWrite(13, LOW);
 }
 


  valm = digitalRead(14);    // add one minute when pressed
   if(valm== LOW) {
   minute++;
   second=0;
   delay(250);
  }
  
  valh = digitalRead(19);    // add one hour when pressed
   if(valh==LOW) {
   hour++;
   second=0;
   delay(250);
  }


  
 
}

7 for the seconds 7 for the minutes 5 for the hour and 1 for the AM/PM light

6 for the seconds, 6 for the minutes, 4 for the hour and one for AM/PM.

Or did you mean a BCD clock? ;)