Components and supplies
1
Power supply 5V/2,5 A
1
PLA Filament
1
MiniUSB B - A cable
1
Transparent PETG filament
1
Universal board 50 x 70 mm
1
Set of wire
1
Arduino Nano R3
1
RGB LED strip WS1812B
1
RTC module DS3231
Project description
Code
Segment clock
arduino
1#include <FastLED.h> 2#include <DS3231.h> 3#include <Wire.h> 4 5#define NUM_LEDS 30 6#define DATA_PIN 2 7 8CRGB leds[NUM_LEDS]; 9DS3231 rtc; 10 11const int TOTAL_SEGMENTS = 4; // Total amount of segments 12const int LEDS_PER_SEGMENT = 14; // Amount of LEDs per segment 13const int DISPLAY_SEGMENT[] = {0, 14, 14 * 2 + 2, 14 * 3 + 2}; // LED starting position of each segment 14const int DISPLAY_NUMBER[][14] = { // True: Lit, False: Not lit 15 {true, true, false, false, true, true, true, true, true, true, true, true, true, true}, // 0 16 {false, false, false, false, false, false, false, false, true, true, true, true, false, false}, // 1 17 {true, true, true, true, false, false, true, true, true, true, false, false, true, true}, // 2 18 {false, false, true, true, false, false, true, true, true, true, true, true, true, true}, // 3 19 {false, false, true, true, true, true, false, false, true, true, true, true, false, false}, // 4 20 {false, false, true, true, true, true, true, true, false, false, true, true, true, true}, // 5 21 {true, true, true, true, true, true, true, true, false, false, true, true, true, true}, // 6 22 {false, false, false, false, false, false, true, true, true, true, true, true, false, false}, // 7 23 {true, true, true, true, true, true, true, true, true, true, true, true, true, true}, // 8 24 {false, false, true, true, true, true, true, true, true, true, true, true, true, true}, // 9 25}; 26 27int red = 255; 28int green = 0; 29int blue = 0; 30char currentFade = 'r'; 31 32void setup() { 33 Serial.begin(9600); 34 Serial.println("Starting execution"); 35 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); 36 FastLED.setBrightness(40); // Lower brightness 37 38 Wire.begin(); 39 40 // The following lines can be uncommented to set the date and time 41rtc.setHour(21); // Set the hour to 15 (24hr format) 42rtc.setMinute(26); // Set the minute to 12 43} 44 45void loop() { 46 bool h12, pm; 47 int hour = rtc.getHour(h12, pm); // Get the hour 48 int minute = rtc.getMinute(); // Get the minute 49 50 int hourFirstDigit = hour / 10; // Take the first digit 51 int hourSecondDigit = hour % 10; // Take the second digit 52 53 int minuteFirstDigit = minute / 10; // Take the first digit 54 int minuteSecondDigit = minute % 10; // Take the second digit 55 56 int totalDelay = 0; 57 while (totalDelay < 10000) { // 10 seconds 58 if (currentFade == 'r') { // If red is currently fading 59 red--; 60 green++; 61 currentFade = red <= 0 ? 'g' : 'r'; // Change currentFade if necessary 62 } else if (currentFade == 'g') { // If green is currently fading 63 green--; 64 blue++; 65 currentFade = green <= 0 ? 'b' : 'g'; // Change currentFade if necessary 66 } else { // If blue is currently fading 67 blue--; 68 red++; 69 currentFade = blue <= 0 ? 'r' : 'b'; // Change currentFade if necessary 70 } 71 FastLED.clear(); // Clear the LEDs 72 displayNumber(3, hourFirstDigit); 73 displayNumber(2, hourSecondDigit); 74 displayNumber(1, minuteFirstDigit); 75 displayNumber(0, minuteSecondDigit); 76 leds[14 * 2].setRGB(red, green, blue); // Light the dots 77 leds[14 * 2 + 1].setRGB(red, green, blue); 78 FastLED.show(); // Show the current LEDs 79 delay(10); 80 totalDelay += 10; 81 } 82} 83 84void displayNumber(int segment, int number) { 85 for (int j = 0; j < LEDS_PER_SEGMENT; j++) { // Loop over each LED of said segment 86 if (DISPLAY_NUMBER[number][j]) { // If this LED should be on 87 leds[DISPLAY_SEGMENT[segment] + j].setRGB(red, green, blue); // Turn it on 88 } 89 } 90} 91
Comments
Only logged in users can leave comments