Components and supplies
Arduino R3
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
Arduino Code
cpp
// Nixie LED Clock // By: Newson's Electronics //
1// Nixie LED Clock 2// By: Newson's Electronics 3// Date: Dec 29,2024 4 5 6// used two libraries 7#include <Adafruit_NeoPixel.h> 8#include <RtcDS1302.h> 9 10// Define debounce delay (in milliseconds) 11#define DEBOUNCE_DELAY 200 12 13 14unsigned long lastDebounceTime = 0; // Last time a button was pressed 15bool button1State = false; // State of button 1 16bool button4State = false; // State of button 4 17 18ThreeWire myWire(9, 10, 8); // IO, SCLK, CE 19RtcDS1302<ThreeWire> Rtc(myWire); 20 21 22#define LED_PIN 6 23// How many NeoPixels are attached to the Arduino? 24#define LED_COUNT 130 25 26int h1, h2 = 0; // h1= hour ones, h2= hour tens (digits) 27int m1, m2 = 0; // m1= minutes ones, m2=minutes tens 28int s = 0; // seconds 29int dots = 0; 30int year, month, day, hours, minutes, seconds = 0; 31int brightness = 125; 32int fadeSpeed = 256; 33bool changeTime = 0; 34bool fadeColour = 0; 35uint16_t hue = 10; // Hue value for color cycling (0-65535) 36uint8_t red = 255; // Maximum value for uint8_t 37uint8_t green = 0; // Minimum value 38uint8_t blue = 128; // Mid-range value 39 40 41//Setup the neopixel 42Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 43 44uint32_t color = strip.Color(255, 255, 255); // Initialize white color 45uint32_t color2 = strip.Color(255, 255, 255); // Initialize temp color variable 46 47 48 49// used to test the leds to see if they all turn on 50void test() { 51 for (int i = 0; i <= 130; i++) { 52 53 strip.setPixelColor(i, strip.Color(255, 255, 255)); 54 strip.show(); 55 delay(100); // Off 56 } 57} 58 59void clearDisplay() { 60 for (int i = 0; i <= 130; i++) { 61 strip.setPixelColor(i, strip.Color(0, 0, 0)); // Off 62 } 63} 64 65 66void displayTime(int h2, int h1, int m2, int m1) { 67 clearDisplay(); 68 69 // M1 Digits 70 strip.setPixelColor(0 + m1, color); 71 strip.setPixelColor(19 - m1, color); 72 strip.setPixelColor(20 + m1, color); 73 // M2 Digits 74 strip.setPixelColor(39 - m2, color); 75 strip.setPixelColor(40 + m2, color); 76 strip.setPixelColor(59 - m2, color); 77 // Centre dots 78 79 // moving down the centre line 80 //s=seconds%10; 81 //strip.setPixelColor(60+s, strip.Color(255, 255, 255)); 82 83 84 85 86 // centre led 87 strip.setPixelColor(64, color); 88 89 // Flashing Centre led 90 /* 91 if (s % 2 == 0) { 92 strip.setPixelColor(64, color); 93 } 94 95 if (s % 2 > 0) { 96 strip.setPixelColor(64, strip.Color(0, 0, 0)); 97 } 98*/ 99 100 // H1 Digits 101 strip.setPixelColor(79 - h1, color); 102 strip.setPixelColor(80 + h1, color); 103 strip.setPixelColor(99 - h1, color); 104 105 // H2 Digits 106 if (h2!=0||changeTime==1) // don't display 0 in front 107 { 108 strip.setPixelColor(100 + h2, color); 109 strip.setPixelColor(119 - h2, color); 110 strip.setPixelColor(120 + h2, color); 111 } 112 113 strip.show(); 114} 115 116 117 118void setup() { 119 Serial.begin(9600); 120 121 //Clock setup 122 Serial.print("compiled: "); 123 Serial.print(__DATE__); 124 Serial.println(__TIME__); 125 126 Rtc.Begin(); 127 128if (!Rtc.IsDateTimeValid()) { 129 Serial.println("RTC lost power, setting the time!"); 130 Rtc.SetIsRunning(true); 131 Rtc.SetIsWriteProtected(false); 132 RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); 133 Rtc.SetDateTime(compiled); // Set RTC to compile time 134} else { 135 // RTC is valid, no need to set time 136 Serial.println("RTC time is valid."); 137} 138 139 140 // Get the compile time 141 142 143 // Check if the RTC time is valid 144 145 146 // Check the battery status 147 148 149 // RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); 150 // printDateTime(compiled); 151 // Serial.println(); 152// Rtc.SetIsRunning(true); 153 // Rtc.SetIsWriteProtected(false); 154 //Rtc.SetDateTime(compiled); 155 156 157 //clock setup done 158 159 160 161 162 163 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) 164 165 clearDisplay(); 166 delay(1000); 167 168 169 strip.setBrightness(brightness); // Set BRIGHTNESS to about 1/5 (max = 255) 170 //LED_COUNT= 10; 171 //strip.begin(); 172 173 //setup the buttons () 174 pinMode(2, INPUT_PULLUP); 175 pinMode(3, INPUT_PULLUP); 176 pinMode(4, INPUT_PULLUP); 177 pinMode(5, INPUT_PULLUP); 178} 179 180 181// loop() function -- runs repeatedly as long as board is on --------------- 182 183 184void debounceButtons() { 185 // Check for both buttons pressed 186 if (digitalRead(2) == LOW && digitalRead(5) == LOW) { 187 // If debounce delay has passed 188 if (millis() - lastDebounceTime > DEBOUNCE_DELAY) { 189 changeTime = !changeTime; // Toggle the changeTime state 190 191 // Save the current color before changing 192 color2 = color; 193 194 if (changeTime == 1) { 195 color = strip.Color(255, 0, 0); // Change to red for time setting mode 196 Rtc.SetIsWriteProtected(false); 197 Rtc.SetIsRunning(false); 198 } 199 200 if (changeTime == 0) { 201 color = strip.Color(255, 255, 255); // Change back to white 202 Rtc.SetIsWriteProtected(true); 203 Rtc.SetIsRunning(true); 204 } 205 206 // Update the display 207 displayTime(h2, h1, m2, m1); 208 strip.show(); 209 210 // Save the current time of the debounce to avoid multiple triggers 211 lastDebounceTime = millis(); 212 } 213 } 214} 215 216 217void updateTime() { 218 // Create a new RtcDateTime object with updated minutes 219 RtcDateTime updatedTime( 220 year, month, day, 221 hours, minutes, seconds); 222 223 // Set the updated time back to the RTC 224 Rtc.SetIsWriteProtected(false); 225 Rtc.SetDateTime(updatedTime); 226 delay(200); 227 Rtc.SetIsWriteProtected(true); 228} 229 230 231void loop() { 232 233 RtcDateTime now = Rtc.GetDateTime(); 234 printDateTime(now); 235 Serial.println(); 236 //int minutes=dt.Second(); 237 238 239// Buttons 1 and 4 press both at same time to enter time setting mode (RED mode) 240 int button1 = digitalRead(2); // brightness increase 241 int button2 = digitalRead(3); // brightness decrease 242 int button3 = digitalRead(4); // fadeColour speed increase 243 int button4 = digitalRead(5); // fadeColour speed decrease 244 245 246 // print out the value of buttons note 1 == not pressed: 247 Serial.print(button1); 248 Serial.print(button2); 249 Serial.print(button3); 250 Serial.print(button4); 251 Serial.print(": "); 252 253 Serial.println(color); 254 255//ChangeTime setting mode 256 if (button1 == 0 && button4 == 0) { 257 258 259 //if (millis() - lastDebounceTime > DEBOUNCE_DELAY) { 260 changeTime = !changeTime; // Toggle the changeTime state 261 //} 262 263 264 265 // changeTime = !changeTime; 266//delay(200); 267 268 // save the current colour 269 color2 = color; 270 271 if (changeTime == 1) 272 {color = strip.Color(255, 0, 0); // turn the lights to red change time. 273displayTime(h2, h1, m2, m1); 274Rtc.SetIsWriteProtected(false); 275 Rtc.SetIsRunning(false); 276 277 } 278 if (changeTime == 0) 279 { 280 //color = color2; // update to the previous colour 281 color = strip.Color(255, 255, 255); 282 Rtc.SetIsWriteProtected(true); 283 Rtc.SetIsRunning(true); 284 button1 = 1; 285 button2 = 1; 286 button3 = 1; 287 button4 = 1; 288 //delay(1000); 289 } 290 displayTime(h2, h1, m2, m1); 291 strip.show(); 292 293 294 while (button1 == 0 && button4 == 0) { 295 button1 = digitalRead(2); 296 button4 = digitalRead(5); 297 } 298 // avoid button debounce 299 300 301 delay(500); 302 button1 = 1; 303 button2 = 1; 304 button3 = 1; 305 button4 = 1; 306 } 307 308 309 310 // update time 311 if (changeTime == 1) { 312 313 314 if (button1 == 0) { 315 m1 += 1; 316 if (m1 == 10) { 317 m1 = 0; 318 } 319 minutes = m2 * 10 + m1; 320 updateTime(); 321 } 322 323 if (button2 == 0) { 324 m2 += 1; 325 if (m2 == 6) { 326 m2 = 0; 327 } 328 minutes = m2 * 10 + m1; 329 updateTime(); 330 } 331 332 333 if (button3 == 0) { 334 h1 += 1; 335 if (h1 == 10 || h1 > 3 && h2 == 2) { 336 if (h2 != 0) h1 = 0; 337 if (h2 == 0) h1 = 1; 338 } 339 hours = h2 * 10 + h1; 340 updateTime(); 341 } 342 if (button4 == 0) { 343 h2 += 1; 344 if (h2 == 3) { 345 if (h1 != 0) h2 = 0; 346 if (h1 == 0) h2 = 1; 347 } 348 hours = h2 * 10 + h1; 349 updateTime(); 350 } 351 } 352 353 354 355 356 357 358 359 if (changeTime == 0) { 360 361 if (button1 == 0 && brightness < 250) { 362 brightness += 5; 363 strip.setBrightness(brightness); 364 Serial.println(brightness); 365 } 366 367 if (button2 == 0 && brightness > 5) { 368 brightness -= 5; 369 strip.setBrightness(brightness); 370 Serial.println(brightness); 371 } 372 373 374 375 if (fadeColour == 1) { 376 377 hue += fadeSpeed; // Increment hue to cycle colors 378 379 if (hue > 65535) hue = 0; // Wrap around hue value 380 if (hue < 0) hue = 65535; 381 color = strip.ColorHSV(hue); // Get RGB from HSV 382 } 383 384 if (button3 == 0) { 385 386 // 387 fadeSpeed += 10; 388 fadeColour = 1; 389 } 390 391 if (button4 == 0) { 392 393 fadeSpeed -= 50; 394 fadeColour = 1; 395 if (fadeSpeed < 0) fadeSpeed = 0; 396 397 } 398 } 399 400 m1 = minutes % 10; 401 m2 = minutes / 10; 402 h1 = hours % 10; 403 h2 = hours / 10; 404 s = seconds % 10; 405 406 407 408 displayTime(h2, h1, m2, m1); 409} 410 411#define countof(a) (sizeof(a) / sizeof(a[0])) 412 413void printDateTime(const RtcDateTime& dt) { 414 char datestring[26]; 415 month = dt.Month(); 416 year = dt.Year(); 417 day = dt.Day(); 418 hours = dt.Hour(); 419 minutes = dt.Minute(); 420 seconds = dt.Second(); 421 422 snprintf_P(datestring, 423 countof(datestring), 424 PSTR("%02u/%02u/%04u %02u:%02u:%02u"), 425 dt.Month(), 426 dt.Day(), 427 dt.Year(), 428 dt.Hour(), 429 dt.Minute(), 430 dt.Second()); 431 Serial.print(datestring); 432} 433 434// Function to read a register 435// Function to read a register from DS1302
Downloadable files
CAD file
Use 3mm board and 2MM acrylic.
nixie Clock CAD Files.dxf
Comments
Only logged in users can leave comments