Adding features to a simple clock sketch

Hello,
I have decided to build a simple clock for a school project. Specifically, i plan to build the lite brite clock. If you haven't seen anything about it it can be seen here; http://www.instructables.com/id/Lite-Brite-LED-clock/ So, as you can see, this user has made a nice write up of the construction, but upon further inspection I see that there is no way to set the time other than reprograming the Arduino.

So, my question is, could anyone help me add the code to set the time with 2 momentary switches or something similar?

Below I attached the "wiring diagram"
The author says that you currently change the time by using the line of code about 6 lines down that says "setTime" I have the rest of the code, but I would assume that this is all you would need.

If you've got any more questions let me know. And thanks for the help

#include <Time.h>

int column[]={0,1,2,3,4,5,6,7,8,9,10,11};
int row[] = {14,15,16,17,18};


void setup()
{
  setTime(9,27,0,27,7,2012);

So, my question is, could anyone help me add the code to set the time with 2 momentary switches or something similar?

You need more than two. You need two to move left or right through the series of values - month, day, year, hour, minute, and second - and two to increment or decrement the value of the appropriate variable. Plus, you need one to trigger the "set the time" action and one to trigger the "now, set the time" action. That's 6 by my count. In addition, you'd need some way of knowing what value (month, day. etc.) was being modified, and some way of knowing what the new value will be.

I'm in the middle of something similar, having written a basic clock for ardino as a way of testing a 20x4 LCD.
I concluded that four input buttons < - + > would give a nicer user experience than the minimal + "increase" and > "ok"

I have an extra goal which I wonder whether it would interest you.
Using an extra circuit to go from a MegoOhm safe mains voltage probe to a +-1V ac at an op-amp to arduino compatible +5V while mains is positive and 0V while not, it is possible to trigger an interrupt on pin2 and time the rising zerocrossing of the mains. using a function such as micros().

UK mains has the property of averaging to 50.00 Hz in a 24 hour period, so its average might be useable to keep the internal timekeeping in the arduino about right.

Incidently, such a clock could also be set up to set another digital pin ON while mains is more than average, say 50.1Hz for 40 alarms per day, and/or another pin OFF while mains is slower than average, say 49.9Hz for about 40 alarms per day. That would need at least a relay to switch a demand-managed immersion heater which goes on while there is surplus electricity in the national grid or goes off while there is a shortage of electricity and the power stations are struggling to open the throttle valves.

To have such clock demand-management controllers in use in millions of households would permit more uninterruptable intermittent renewables in the uk generating portfolio because Fdm preferentially uses electricity at the best times according to national F.

For some reason the commercial product suppliers never did this in 20+ years, so getting the standalone clock Fdm controllers out via hobbyists might be the only way.

50 or 60 Hz clock input from Mains
use a photocoupler in an AC circuit to input pin.

50 or 60 Hz clock input from Mains
use a photocoupler in an AC circuit to input pin.

How are you going to get the current time from that?

Clocks have used the mains to keep time for the last 100 years.

Clocks have used the mains to keep time for the last 100 years.

But not to SET the time.

I quite like that " optocoupler" suggestion, as the cost of an opto-isolator is not more than the op-amp, and its isolated side going directly to an arduino d in pin saves a fair bit of complexity.

The opto-isolators which I found need +-10 to +-20mA to fully conduct on their isolated side.
They are four pin devices.
So, putting in a plain diode which must be safely rated to block reverse mains, I have up to 320V and want 20mA.
About 640kOhm would do; call it about a meg-ohm.

So the improved mains probe is
Live-Diode-Megohm-Opto+-Opto-Neutral on the mains side, and
Arduino D_input_pin to 2kOhm to opto and opto to arduino ground on the arduino side.
The first line of that could be hidden inside a BS1362a mains plug, so that is what I'll do.

Anyone want me to post here when I've made one?

@PaulS I refer to 'ad2049q' comments. I did not say anything about setting the time. Yes, yes, I know that was not the original question, so? About 99.9% of answers on this forum do not answer the original question.

Sorry for the confusion, I do not need the date or seconds. All I'll be using is hours and minutes.

Sorry for the confusion, I do not need the date or seconds.

The setTime() function does. So, you do too.

So I cannot set all of the seconds, day, month, and year values to any number, like 1? I would never know of it because the display is only showing hours and minutes

PaulS, I think you are wrong. Many clocks only have 2 buttons to set the time/date. It can be done by:
1 - using 1 button to enter "set mode";
2 - using 1 button to go a unit up;
3 - you don't need to go a unit down, because when you reach the last digit (for example 59 in the minutes) you can re-start from the begin again;
4 - using 1 button to pass to the next "variable" that you want to set;
5 - you don't need a button to go to the previous "variable" because if you get wrong in the previous variable you can "exit set mode" and then restart the whole process;
6 - using 1 button to "exit set mode".

So, you don't need buttons 3 and 5, then you get only 4 buttons. But, the buttons 1, 4 and 6 can be the same button. So, you only need 2 buttons:

  • When you push button 1 you enter the "set mode";
  • When you push button 2 you go up one unit;
  • When you push button 1 you go to the next variable to set;
  • When in the last variable you press button 1 you exit the "set mode";

Just what I was thinking, my watch only has two buttons.

Given your two PBs, if in the first line in your loop you checked for a state where "SET" was actuated then it would branch to a Time Setting routine (a function) or go on with "clock matters"?

Well, after doing some more looking ive found that there is a "adjustTime" function

Apparently you just add a time in seconds after the function and it will add or subtract from the value of the set time.

So how would I make it so when a button was pressed the AdjustTime function would turn on? My plan is to have one button for the minutes, one for hours.

Sorry for any dumb questions, I am totally new to programing and thanks for all the help already

2 button or 4 button setting

This is code spagetti, so if any serious programmers have a better way then please say so.

If hh and mm are global variables,
and boolean set_state is also global,
then the clock can go around its usual clock incrementing loop concurrently to some button-sensitive code inside an
if (set_state)
{
//some setting stuff goes in here
}

optocoupler extra thing

should I start a new thread on this?
I got the R wrong to get 20mA from mains and before I made a non-working very very safe do-nothing probe I figured out that my 20mA optoisolator cannot be safely driven directly off mains without a dangerously hot resistor nearby. I've moved back to wanting a safe megohm probe within a mains plug and a separate battery providing the power on the mains side of the optoisolator. I want to get this right as 20+ years of commercial products never did it, so it has to be a DIY.

Okay, Ive found out what I want to do. Basically all Im trying to do is make it say "if this button is pressed then add one hour"

So, how would I do that? I plan to just use 2 buttons, one for minute, one for hour. All I need is the code.
I am on a tight deadline on this project so the faster the better.

Can you please post the code that you have already? I think is easy if we know what you have.

EDIT: I don't know if you know, but you must put the code inside the "code" tag that you can find in the tollbar.

I had to shorten it some, but everything past the last line is just the same as all the others.

#include <Time.h>

int column[]={0,1,2,3,4,5,6,7,8,9,10,11};
int row[] = {14,15,16,17,18};


void setup()
{
  setTime(9,27,0,27,7,2012);
 
  for (int i=0;i<13;i++){
  pinMode(column[i],OUTPUT);
  digitalWrite(column[i],LOW);
  } 
 
  for (int j=0;j<5;j++){
  pinMode(row[j],OUTPUT);
  digitalWrite(row[j],HIGH);
  } 
}
void loop () {

digitalWrite(column[11],HIGH);
digitalWrite(column[12],HIGH);
digitalWrite(column[11],LOW);
digitalWrite(column[12],LOW);

switch (hourFormat12()) {
    case 1:
      one(0);
      break;
    case 2:
      two(0);
      break;
          case 3:
      three(0);
      break;
          case 4:
      four(0);
      break;
          case 5:
      five(0);
      break;
          case 6:
      six(0);
      break;
          case 7:
      seven(0);
      break;
          case 8:
      eight(0);
      break;
          case 9:
      nine(0);
      break;
          case 10:
      one(-3);
      zero(0);
      break;
          case 11:
      one(-3);
      one(0);
      break;
          case 12:
      one(-3);
      two(0);
      break;

  }

switch ((minute()/10)) {
    case 0:
      zero(3);
      break; 
    case 1:
      one(3);
      break;
    case 2:
      two(3);
      break;
     case 3:
      three(3);
      break;
     case 4:
      four(3);
      break;
     case 5:
      five(3);
      break;
  }

switch (minute()%10) {
     case 0:
      zero(6);
      break; 
  case 1:
      one(6);
      break;
    case 2:
      two(6);
      break;
          case 3:
      three(6);
      break;
          case 4:
      four(6);
      break;
          case 5:
      five(6);
      break;
          case 6:
      six(6);
      break;
          case 7:
      seven(6);
      break;
          case 8:
      eight(6);
      break;
          case 9:
      nine(6);
      break;
      // if nothing else matches, do the default
      // default is optional
  }


}

void allOn(){
  for(int x=0;x<5;x++){
    for(int y=0;y<13;y++){
      digitalWrite(row[x],LOW);
      digitalWrite(column[y],HIGH);
    }
  }
}

void allOff(){
  for(int x=0;x<5;x++){
    for(int y=0;y<13;y++){
      digitalWrite(row[x],HIGH);
      digitalWrite(column[y],LOW);
    }
  }
}

void zero(int x){
  digitalWrite(column[1+x],HIGH);
  digitalWrite(row[0],LOW);
  digitalWrite(column[1+x],LOW);
  digitalWrite(row[0],HIGH);
 
  digitalWrite(column[2+x],HIGH);
  digitalWrite(row[0],LOW);
  digitalWrite(column[2+x],LOW);
  digitalWrite(row[0],HIGH);
 
  digitalWrite(column[3+x],HIGH);
  digitalWrite(row[0],LOW);
  digitalWrite(column[3+x],LOW);
  digitalWrite(row[0],HIGH);
 
  digitalWrite(column[3+x],HIGH);
  digitalWrite(row[1],LOW);
  digitalWrite(column[3+x],LOW);
  digitalWrite(row[1],HIGH);
 
  digitalWrite(column[3+x],HIGH);
  digitalWrite(row[2],LOW);
  digitalWrite(column[3+x],LOW);
  digitalWrite(row[2],HIGH);
 
  digitalWrite(column[3+x],HIGH);
  digitalWrite(row[3],LOW);
  digitalWrite(column[3+x],LOW);
  digitalWrite(row[3],HIGH);
 
  digitalWrite(column[3+x],HIGH);
  digitalWrite(row[4],LOW);
  digitalWrite(column[3+x],LOW);
  digitalWrite(row[4],HIGH);
 
  digitalWrite(column[2+x],HIGH);
  digitalWrite(row[4],LOW);
  digitalWrite(column[2+x],LOW);
  digitalWrite(row[4],HIGH);
 
  digitalWrite(column[1+x],HIGH);
  digitalWrite(row[4],LOW);
  digitalWrite(column[1+x],LOW);
  digitalWrite(row[4],HIGH);
 
  digitalWrite(column[1+x],HIGH);
  digitalWrite(row[3],LOW);
  digitalWrite(column[1+x],LOW);
  digitalWrite(row[3],HIGH);
 

 
 
}/code]