Skip to content

Instantly share code, notes, and snippets.

@Grippy98
Last active December 12, 2018 03:33
Show Gist options
  • Save Grippy98/a152b82a0630471f52ccb3ed458fd277 to your computer and use it in GitHub Desktop.
Save Grippy98/a152b82a0630471f52ccb3ed458fd277 to your computer and use it in GitHub Desktop.
Updated Nixie Clock Code... Still needs A LOT of cleaning.
/*
Nixie Clock Code by Andrei Aldea
Adapted for use with HV5530 Nixie Driver boards by Johnny Izzard
And the Sodaq_DS3231 library by Sodaq Moja
*/
#include <SPI.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
#define LD 10
#define CLK 13
#define SDO 11
#define PWM 9
#define buttonPin 5
#define plusButton 3
#define minusButton 4
uint32_t old_ts;
#define tube_off 10
#define ind_off 3
uint16_t decimal_lookup[11] = {0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0000}; // decimal_lookup[0]...decimal_lookup[9] outputs corresponding nixie number. decimal_lookup[10] is off.
uint8_t ind_lookup[4] = {0x01, 0x02, 0x03, 0x00}; // ind_lookup[0] is top indicator, ind_lookup[1] is bottom indicator, ind_lookup[2] is both, ind_lookup[3] is off.
uint32_t data1 = 0; // Used to store data to be shifted
uint32_t data2 = 0;
uint32_t data3 = 0;
uint32_t data4 = 0;
uint8_t tube1 = 0; // Value to display on the corresponding tube.
uint8_t tube2 = 0;
uint8_t tube3 = 0;
uint8_t tube4 = 0;
uint8_t tube5 = 0;
uint8_t tube6 = 0;
uint8_t tube7 = 0;
uint8_t tube8 = 0;
uint8_t indicator1 = 0; // Value to display on the corresponding indicator.
uint8_t indicator2 = 0;
uint8_t indicator3 = 0;
uint8_t indicator4 = 0;
int hour0, hour1, minute0, minute1, second0, second1; //to hold time values
int brightness;
int machState = 0; //FSM state number
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 500; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(9600);
while (!Serial) ; //Stupid Arduino 101 Fails to initalize Serial and just skips... because Intel is run by a bunch of geniuses
Wire.begin();
rtc.begin();
pinMode(buttonPin, INPUT);
pinMode(minusButton, INPUT);
pinMode(plusButton, INPUT);
pinMode(LD, OUTPUT); // LD is pin 10 (active high)
pinMode(CLK, OUTPUT); // CLK is pin 13
pinMode(SDO, OUTPUT); // SDO is pin 11
pinMode(PWM, OUTPUT); // PWM (Blank) is pin 9 (active low)
digitalWrite(LD, LOW);
digitalWrite(CLK, LOW);
digitalWrite(SDO, LOW);
digitalWrite(PWM, HIGH);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
/* Reset Values/Blank The Display */
tube1 = tube_off;
tube2 = tube_off;
tube3 = tube_off;
tube4 = tube_off;
tube5 = tube_off;
tube6 = tube_off;
tube7 = tube_off;
tube8 = tube_off;
indicator1 = ind_off;
indicator2 = ind_off;
indicator3 = ind_off;
indicator4 = ind_off;
update_disp();
}
void loop() {
brightness = analogRead(A3);
Serial.println(brightness);
brightness = map(brightness, 0, 1023, 0, 255);
analogWrite(PWM, brightness); //Set Brightness
if (!digitalRead(buttonPin))
{
machState++;
Serial.println(machState);
delay(200); //redo this without delays...
}
if (machState == 0)
{
DateTime now = rtc.now(); //get the current date-time
uint32_t ts = now.getEpoch();
if (old_ts == 0 || old_ts != ts) {
old_ts = ts;
indicator1 = ind_off; // Both indicators on
indicator2 = ind_off;
indicator3 = ind_off;
indicator4 = ind_off;
hour0 = now.hour() / 10;
hour1 = now.hour() % 10;
minute0 = now.minute() / 10;
minute1 = now.minute() % 10;
second0 = now.second() / 10;
second1 = now.second() % 10;
tube8 = hour0;
tube7 = hour1;
tube6 = minute0;
tube5 = minute1;
tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
update_disp();
}
}
else if (machState == 1)
{
tube7 = hour1;
tube6 = minute0;
tube5 = minute1;
tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
hour0++;
delay(500);
}
else if (!digitalRead(plusButton))
{
hour0--;
delay(500);
}
if (hour0 > 2)
{
hour0 = 0;
}
else if (hour0 < 0)
{
hour0 = 2;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube8 == tube_off)
{
tube8 = hour0;
}
else
{
tube8 = tube_off;
}
update_disp();
}
}
else if (machState == 2)
{
tube8 = hour0;
//tube7 = hour1;
tube6 = minute0;
tube5 = minute1;
tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
hour1++;
delay(500);
}
else if (!digitalRead(plusButton))
{
hour1--;
delay(500);
}
if (hour1 > 9)
{
hour1 = 0;
}
else if (hour1 < 0)
{
hour1 = 9;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube7 == tube_off)
{
tube7 = hour1;
}
else
{
tube7 = tube_off;
}
update_disp();
}
}
else if (machState == 3)
{
tube8 = hour0;
tube7 = hour1;
//tube6 = minute0;
tube5 = minute1;
tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
minute0++;
delay(500);
}
else if (!digitalRead(plusButton))
{
minute0--;
delay(500);
}
if (minute0 > 6)
{
minute0 = 0;
}
else if (minute0 < 0)
{
minute0 = 6;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube6 == tube_off)
{
tube6 = minute0;
}
else
{
tube6 = tube_off;
}
update_disp();
}
}
else if (machState == 4)
{
tube8 = hour0;
tube7 = hour1;
tube6 = minute0;
//tube5 = minute1;
tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
minute1++;
delay(500);
}
else if (!digitalRead(plusButton))
{
minute1--;
delay(500);
}
if (minute1 > 9)
{
minute1 = 0;
}
else if (minute1 < 0)
{
minute1 = 9;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube5 == tube_off)
{
tube5 = minute1;
}
else
{
tube5 = tube_off;
}
update_disp();
}
}
else if (machState == 5)
{
tube8 = hour0;
tube7 = hour1;
tube6 = minute0;
tube5 = minute1;
//tube4 = second0;
tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
second0++;
delay(500);
}
else if (!digitalRead(plusButton))
{
second0--;
delay(500);
}
if (second0 > 6)
{
second0 = 0;
}
else if (second0 < 0)
{
second0 = 6;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube4 == tube_off)
{
tube4 = second0;
}
else
{
tube4 = tube_off;
}
update_disp();
}
}
else if (machState == 6)
{
tube8 = hour0;
tube7 = hour1;
tube6 = minute0;
tube5 = minute1;
tube4 = second0;
//tube3 = second1;
tube2 = tube_off;
tube1 = tube_off;
unsigned long currentMillis = millis();
if (!digitalRead(minusButton))
{
second1++;
delay(500);
}
else if (!digitalRead(plusButton))
{
second1--;
delay(500);
}
if (second1 > 9)
{
second1 = 0;
}
else if (second1 < 0)
{
second1 = 9;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (tube3 == tube_off)
{
tube3 = second1;
}
else
{
tube3 = tube_off;
}
update_disp();
}
}
else if (machState == 7)
{
DateTime dt(2018, 11, 20, hour0 * 10 + hour1, minute0 * 10 + minute1, second0 * 10 + second1, 1); //This only works for current year right now... so kinda squirly...
rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above
machState = 0; //Now go back to counting.
}
else
{
machState = 0; //Reset to 0...
}
}
void update_disp(void) {
data1 = 0x00000000;
data1 |= ((uint32_t (decimal_lookup[tube1])) & 0x000003FF);
data1 |= ((uint32_t (decimal_lookup[tube2]) << 10) & 0x000FFC00);
data1 |= ((uint32_t (ind_lookup[indicator1]) << 20) & 0x00300000);
data2 = 0x00000000;
data2 |= ((uint32_t (decimal_lookup[tube3])) & 0x000003FF);
data2 |= ((uint32_t (decimal_lookup[tube4]) << 10) & 0x000FFC00);
data2 |= ((uint32_t (ind_lookup[indicator2]) << 20) & 0x00300000);
data3 = 0x00000000;
data3 |= ((uint32_t (decimal_lookup[tube5])) & 0x000003FF);
data3 |= ((uint32_t (decimal_lookup[tube6]) << 10) & 0x000FFC00);
data3 |= ((uint32_t (ind_lookup[indicator3]) << 20) & 0x00300000);
data4 = 0x00000000;
data4 |= ((uint32_t (decimal_lookup[tube7])) & 0x000003FF);
data4 |= ((uint32_t (decimal_lookup[tube8]) << 10) & 0x000FFC00);
data4 |= ((uint32_t (ind_lookup[indicator4]) << 20) & 0x00300000);
digitalWrite(LD, HIGH);
SPI.transfer(data1 >> 24);
SPI.transfer(data1 >> 16);
SPI.transfer(data1 >> 8);
SPI.transfer(data1);
digitalWrite(LD, LOW);
digitalWrite(LD, HIGH);
digitalWrite(LD, HIGH);
SPI.transfer(data2 >> 24);
SPI.transfer(data2 >> 16);
SPI.transfer(data2 >> 8);
SPI.transfer(data2);
digitalWrite(LD, LOW);
digitalWrite(LD, HIGH);
digitalWrite(LD, HIGH);
SPI.transfer(data3 >> 24);
SPI.transfer(data3 >> 16);
SPI.transfer(data3 >> 8);
SPI.transfer(data3);
digitalWrite(LD, LOW);
digitalWrite(LD, HIGH);
digitalWrite(LD, HIGH);
SPI.transfer(data4 >> 24);
SPI.transfer(data4 >> 16);
SPI.transfer(data4 >> 8);
SPI.transfer(data4);
digitalWrite(LD, LOW);
digitalWrite(LD, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment