/*
7-Segment Display Test
This Arduino sketch demonstrates the use of a 74HC595 shift register to control a 7-segment display.
The display blinks the decimal point four times and then cycles through hexadecimal digits from 0 to F.
Pin Definitions:
- latchPin: Pin connected to the 74HC595 RCLK pin.
- clockPin: Pin connected to the 74HC595 CLK pin.
- dataPin: Pin connected to the 74HC595 DS pin.
- delayTime: Delay time in milliseconds for the main loop.
- blinkRate: Blink rate in milliseconds for the decimal point.
Segment Patterns:
- The segmentPatterns array holds binary patterns for displaying hexadecimal digits 0 to F and blinking the decimal point.
Author: [Nordl3]
Date: [12.3.24]
*/
// Pin Definitions
#define latchPin 10 // 74HC595 RCLK pin
#define clockPin 12 // 74HC595 CLK pin
#define dataPin 11 // 74HC595 DS pin
#define delayTime 1000 // Delay time in milliseconds
#define blinkRate 200 // Decimal Point blinkRate in milliseconds
// Segment patterns for digits 0 to F and decimal point blinking
const byte segmentPatterns[] =
{
B00000011, // 0 (0)
B10011111, // 1 (1)
B00100101, // 2 (2)
B00001101, // 3 (3)
B10011001, // 4 (4)
B01001001, // 5 (5)
B01000001, // 6 (6)
B00011111, // 7 (7)
B00000001, // 8 (8)
B00001001, // 9 (9)
B00010001, // A (10)
B00000001, // B (11)
B01100011, // C (12)
B10000101, // D (13)
B01100001, // E (14)
B01110001, // F (15)
B11111110 // . (16) (Decimal point)
};
void setup()
{
// Initialize pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
blinkLED();
hexCounter();
delay(delayTime); // Wait for X second
}
// Function to blink the decimal point four times
void blinkLED()
{
for (int j = 0; j < 4; j++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, segmentPatterns[16]); // Display decimal point
digitalWrite(latchPin, HIGH);
delay(blinkRate); // Wait for X seconds
clearDisplay(); // Clear display
delay(blinkRate); // Wait for X seconds
}
}
// Function to display hexadecimal characters from 0 to F
void hexCounter()
{
for (int i = 0; i < 16; i++)
{
displayDigit(i); // Display current digit
}
}
// Function to display a digit on the 7-segment display
void displayDigit(int digit)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, segmentPatterns[digit]); // Shift out the segment pattern for the digit
digitalWrite(latchPin, HIGH);
delay(delayTime); // Wait for X second
clearDisplay(); // Clear display
delay(blinkRate); // Wait for X seconds
}
// Function to clear the display (turn off all segments)
void clearDisplay()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, B11111111); // Invert all bits to turn off segments
digitalWrite(latchPin, HIGH);
}