// Nixie Klok
// Jos van Dijken
// 30 Augustus 2012
#include <Bounce.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
Bounce HoursButton = Bounce( 13,10 );
Bounce MinutButton = Bounce( 12,10 );
long previousMillis = 0;
long interval = 60000;
int hours = 0;
int minutes = 0;
int seconds = 0;
int days;
int months;
int years;
void setup()
{
pinMode(0,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(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
Wire.begin();
RTC.begin();
StartUpRoutine();
WriteNixies();
}
void loop()
{
HoursButton.update();
MinutButton.update();
HoursButton.rebounce(500);
MinutButton.rebounce(500);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
WriteNixies();
}
if(MinutButton.fallingEdge())
{
DateTime now = RTC.now();
seconds = now.second();
minutes = now.minute();
hours = now.hour();
minutes = minutes+1;
if(minutes >59)
{
minutes = 0;
}
RTC.adjust(DateTime(years,months,days,hours,minutes,seconds));
WriteNixies();
}
if(HoursButton.fallingEdge())
{
DateTime now = RTC.now();
seconds = now.second();
minutes = now.minute();
hours = now.hour();
hours = hours+1;
if(hours >23)
{
hours=0;
}
RTC.adjust(DateTime(years,months,days,hours,minutes,seconds));
WriteNixies();
}
}
void WriteNixies()
{
DateTime now = RTC.now();
seconds = now.second();
minutes = now.minute();
hours = now.hour();
int hours_t = hours/10;
int hours_e = hours-(hours_t*10);
int minutes_t = minutes/10;
int minutes_e = minutes-(minutes_t*10);
int seconds_t = seconds/10;
int seconds_e = seconds-(seconds_t*10);
digitalWrite(A2,( 1&minutes_e)!=0?HIGH:LOW);
digitalWrite(A3,( 2&minutes_e)!=0?HIGH:LOW);
digitalWrite(0,( 4&minutes_e)!=0?HIGH:LOW);
digitalWrite(1,( 8&minutes_e)!=0?HIGH:LOW);
digitalWrite(A0,( 1&minutes_t)!=0?HIGH:LOW);
digitalWrite(A1,( 2&minutes_t)!=0?HIGH:LOW);
digitalWrite(2,( 4&minutes_t)!=0?HIGH:LOW);
digitalWrite(3,( 8&minutes_t)!=0?HIGH:LOW);
digitalWrite(11,( 1&hours_e)!=0?HIGH:LOW);
digitalWrite(5,( 2&hours_e)!=0?HIGH:LOW);
digitalWrite(4,( 4&hours_e)!=0?HIGH:LOW);
digitalWrite(6,( 8&hours_e)!=0?HIGH:LOW);
digitalWrite(9,( 1&hours_t)!=0?HIGH:LOW);
digitalWrite(10,( 2&hours_t)!=0?HIGH:LOW);
digitalWrite(7,( 4&hours_t)!=0?HIGH:LOW);
digitalWrite(8,( 8&hours_t)!=0?HIGH:LOW);
}
void StartUpRoutine()
{
DateTime now = RTC.now();
for(int i=9; i>=0; i--)
{
digitalWrite(A2,( 1&i)!=0?HIGH:LOW);
digitalWrite(A3,( 2&i)!=0?HIGH:LOW);
digitalWrite(0,( 4&i)!=0?HIGH:LOW);
digitalWrite(1,( 8&i)!=0?HIGH:LOW);
digitalWrite(A0,( 1&i)!=0?HIGH:LOW);
digitalWrite(A1,( 2&i)!=0?HIGH:LOW);
digitalWrite(2,( 4&i)!=0?HIGH:LOW);
digitalWrite(3,( 8&i)!=0?HIGH:LOW);
digitalWrite(11,( 1&i)!=0?HIGH:LOW);
digitalWrite(5,( 2&i)!=0?HIGH:LOW);
digitalWrite(4,( 4&i)!=0?HIGH:LOW);
digitalWrite(6,( 8&i)!=0?HIGH:LOW);
digitalWrite(9,( 1&i)!=0?HIGH:LOW);
digitalWrite(10,( 2&i)!=0?HIGH:LOW);
digitalWrite(7,( 4&i)!=0?HIGH:LOW);
digitalWrite(8,( 8&i)!=0?HIGH:LOW);
delay(100);
}
}