Binary clock help

Hey guys,
I am trying to create a binary clock with 13 leds connected to the digital pins, and 2 switches connected to the analogue pins. I have soldered everything onto a proto board, and I also want to solder my atmega328p onto the board as well. But because this is a time related software, the atmega will be running at 8mhz, instead of 16mhz. Instead of buying some caps and a crystal, could you please help me modify the program so I won’t need a crystal?

int second=0, minute=0, hour=0; 
int munit,hunit,valm=0,valh=0,ledstats,i;

void setup() 
{ 
  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(0, INPUT);
  pinMode(5, INPUT);
}

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(0);
  if (ledstats == LOW)
  {
    for(i=1;i<=13;i++){
    digitalWrite(i, LOW);}
  }
  else 
  {
  if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {  digitalWrite(1, HIGH);} else {  digitalWrite(1,LOW);}
  if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {digitalWrite(2, HIGH);} else {digitalWrite(2,LOW);}
  if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {digitalWrite(3, HIGH);} else {digitalWrite(3,LOW);}
  if(munit == 8 || munit == 9) {digitalWrite(4, HIGH);} else {digitalWrite(4,LOW);}
  
  if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {digitalWrite(5, HIGH);} else {digitalWrite(5,LOW);}
  if(minute >= 20 && minute < 40)  {digitalWrite(6, HIGH);} else {digitalWrite(6,LOW);}
  if(minute >= 40 && minute < 60) {digitalWrite(7, HIGH);} else {digitalWrite(7,LOW);}

  if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {digitalWrite(8, HIGH);} else {digitalWrite(8,LOW);}
  if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {digitalWrite(9, HIGH);} else {digitalWrite(9,LOW);}
  if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {digitalWrite(10, HIGH);} else {digitalWrite(10,LOW);}
  if(hunit == 8 || hunit == 9) {digitalWrite(11, HIGH);} else {digitalWrite(11,LOW);}

  if(hour >= 10 && hour < 20)  {digitalWrite(12, HIGH);} else {digitalWrite(12,LOW);}
  if(hour >= 20 && hour < 24)  {digitalWrite(13, HIGH);} else {digitalWrite(13,LOW);}
  }
  valm = analogRead(0); 
  if(valm<800)
  {
    minute++;
    second=0;
    delay(250);
  }
  valh = analogRead(5);   
  if(valh<800)
  {
    hour++;
    second=0;
    delay(250);
  }
}

I also need help on how to connect the two switches to analogue pins 0 and 5. I am pretty sure that the switches are SPDT.

The price you pay for not using a crystal is an inaccurate clock. Is that really what you want?

But can't you just compensate in the programming?

if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {  digitalWrite(1, HIGH);} else {  digitalWrite(1,LOW);}

Can I recommend the bitwise AND operator '&' to you?

sorry, that made no sense to me

 digitalWrite(1, munint & 1);

But can't you just compensate in the programming?

No.

The pinMode command applies only to digital pins. It is not needed for the analog pins because they are read only pins.

So, the code you have sets digital pin 5 to an input pin. That is going to cause you issues later.

Using digital pins 0 and 1 is to be avoided, since those are the pins used to upload sketches to the board.

The analog pins can be used as digital pins (numbered 14 through 19) if you need more than 11 digital pins, and have space analog pins.

Switches are either on or off/pressed or released. There is nothing analog about them. A switch can't be 40% pressed.

It is not needed for the analog pins because they are read only pins.

They can be used as digital output just like any other pin.

Korman

@Korman:

 [glow]pinMode(5, OUTPUT);[/glow]
  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(0, INPUT);
  [glow]pinMode(5, INPUT);[/glow]

They can be used as digital output just like any other pin.

Not exactly. On a Duemilanove, digital pin 14 and analog pin 0 refer to the same pin, for different purposes. But, you can not perform an analogWrite on pin 14 OR pin 0.

The processor registers support it, even if the Arduino functions might not.

Korman

So if you can't compensate in the software, can you tell me what components I need for connecting a crystal, and where to (Preferably a schematic).

I also need help on how to connect the switches to analogue pins 0 and 5. I have heard of 2 different ways, and I would like to know whats best

http://www.arduino.cc/playground/Learning/Standalone http://www.arduino.cc/en/Main/Standalone

http://www.arduino.cc/en/Tutorial/Pushbutton http://www.arduino.cc/en/Tutorial/Button