Mastoras Inc
Published © GPL3+

DIY Clock With Arduino Nano and PCB

Craft your own Arduino Nano clock! A fun, step-by-step guide for tech wizards and newbies alike. Dive into circuitry, coding, and creativity

BeginnerFull instructions provided3 hours198
DIY Clock With Arduino Nano and PCB

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
7 segment 4 digit display 0.55"
×1
3x push button
×1
Resistor 220 ohm
Resistor 220 ohm
×7

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering Station, 110 V
Soldering Station, 110 V

Story

Read more

Schematics

clcock_fhe5lo20Si.fzz

Code

ClockCode

Arduino
#include <SevSeg.h>

SevSeg sevseg;

byte numDigits = 4;
byte digitPins[] = {4,3,2,5}; // Connect these pins to the digit pins of your 5641AS display
byte segmentPins[] = {12,13,7,9,10,11,6,8}; // Connect these pins to the segment pins of your 5641AS display

// Define the pins for your buttons
const int hourButtonPin = A1; // Change these pins to match your setup
const int minuteButtonPin = A0;
const int brightnessButtonPin = A2;  // Button for cycling brightness

int currentHour = 0;
int currentMinute = 0;
bool segmentDOn = false; // Flag to control segment D blinking

int brightnessLevels[] = {0, 30, 60, 90, 100}; // Adjust brightness levels as needed
int currentBrightnessLevel = 2; // Initial brightness level

unsigned long previousMillis = 0;
const long interval = 1000; // 1000 milliseconds = 1 second
const long minuteInterval = 60000; // 60000 milliseconds = 60 seconds
const long brightnessChangeInterval = 500; // Adjust the interval for brightness change

void setup() {
  bool leadingZeros = true;
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, leadingZeros);

  // Set button pins as INPUT_PULLUP
  pinMode(hourButtonPin, INPUT_PULLUP);
  pinMode(minuteButtonPin, INPUT_PULLUP);
  pinMode(brightnessButtonPin, INPUT_PULLUP);

  // Initialize Serial communication
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();

  // Read the button states and update the time
  if (digitalRead(hourButtonPin) == LOW) {
    currentHour = (currentHour + 1) % 24;
    delay(250); // Debounce the button
  }
  if (digitalRead(minuteButtonPin) == LOW) {
    currentMinute = (currentMinute + 1) % 60;
    delay(250); // Debounce the button
  }

  // Check if a minute has passed
  if (currentMillis - previousMillis >= minuteInterval) {
    // Increment the minute when a minute has passed
    currentMinute = (currentMinute + 1) % 60;
    previousMillis = currentMillis;

    // Check if 60 minutes have passed to increment the hour
    if (currentMinute == 0) {
      currentHour = (currentHour + 1) % 24;
    }
  }

  
    
  sevseg.setNumber(currentHour * 100 + currentMinute, 2); // Display segment D (1) off on the first digit
  
// Cycle through brightness levels if the button is pressed
  if (digitalRead(brightnessButtonPin) == LOW && currentMillis - previousMillis >= brightnessChangeInterval) {
    currentBrightnessLevel = (currentBrightnessLevel + 1) % (sizeof(brightnessLevels) / sizeof(brightnessLevels[0]));
    sevseg.setBrightness(brightnessLevels[currentBrightnessLevel]);
    previousMillis = currentMillis;
    delay(250); // Debounce the button
  }

  sevseg.refreshDisplay();
}

Credits

Mastoras Inc
4 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.