/*** STUDENT TASK - Fill in missing binary patterns
Define the LED digit patterns, from 0 - 9, A - F.
1 = LED on, 0 = LED off, in this order
74HC595 pin Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7
Mapping to a, b, c, d, e, f, g of Seven-Segment LED
*/
// Common Cathode
byte seven_seg_digits[16] = {
B11111100, // 0: Turns off segments a, b, c, d, e, f (displays 0)
B01100000, // 1: Turns off segments b, c (displays 1)
B11011010, // 2: Turns off segments a, b, g, e, d (displays 2)
B11110010, // 3: Turns off segments a, b, g, c, d (displays 3)
B01100110, // 4: Turns off segments f, g, b, c (displays 4)
B10110110, // 5: Turns off segments a, f, g, c, d (displays 5)
B10111110, // 6: Turns off segments a, f, g, e, d, c (displays 6)
B11100000, // 7: Turns off segments a, b, c (displays 7)
B11111110, // 8: Turns off all segments (displays 8)
B11110110, // 9: Turns off segments a, b, g, f, c, d (displays 9)
B11101110, // A: Turns off segments a, b, g, e, f, c (displays A)
B00111110, // b: Turns off segments g, e, f, d, c (displays b)
B10011100, // C: Turns off segments a, f, e, d (displays C)
B01111010, // d: Turns off segments b, c, d, g, e (displays d)
B10011110, // E: Turns off segments a, f, g, e, d (displays E)
B10001110 // F: Turns off segments a, f, g, e (displays F)
};
// Common Cathode: Each bit represents a single segment
byte single_segments[8] = {
B00000000, // NONE (turn off all segments)
B10000000, // a (turn on segment a)
B01000000, // b (turn on segment b)
B00100000, // c (turn on segment c)
B00010000, // d (turn on segment d)
B00001000, // e (turn on segment e)
B00000100, // f (turn on segment f)
B00000010, // g (turn on segment g)
};
// Common Cathode: Turning on 0 to 7 segments incrementally
byte added_segments[8] = {
B00000000, // NONE (turn off all segments)
B10000000, // 1 - segment a on
B11000000, // 2 - segments a, b on
B11100000, // 3 - segments a, b, c on
B11110000, // 4 - segments a, b, c, d on
B11111000, // 5 - segments a, b, c, d, e on
B11111100, // 6 - segments a, b, c, d, e, f on
B11111110, // 7 - segments a, b, c, d, e, f, g on
};
// Define pins connected to 74HC595 shift register
const int clockPin = 4; // SH-CP pin of 74HC595 (clock pin)
const int latchPin = 3; // ST-CP pin of 74HC595 (latch pin)
const int dataPin = 2; // DS pin of 74HC595 (data pin)
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud rate
// Set the latchPin, clockPin, and dataPin as outputs for 74HC595
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
// The loop function controls displaying the digits 0 - F continuously,
// displaying one digit at a time, followed by a delay, then displaying segments.
void loop() {
// Loop to count from hexadecimal 0 to F (0-15)
for (byte digit = 0; digit < 16; ++digit) {
delay(500); // Wait for half a second
sevenSegWrite(digit); // Display the digit on the seven-segment display
Serial.print("digit: "); // Print to the serial monitor
Serial.println(digit); // Print the current digit
}
delay(1000); // Wait for 1 second before the next section
Serial.println(""); // Print a new line for separation in the serial monitor
// Loop through and display individual segments (a to g) one at a time
for (byte segment = 0; segment < 8; ++segment) {
delay(500); // Wait for half a second
singleSegWrite(segment); // Display a single segment
Serial.print("individual segment: "); // Print to the serial monitor
Serial.println(segment); // Print the current segment number
}
delay(1000); // Wait for 1 second before the next section
Serial.println(""); // Print a new line for separation in the serial monitor
// Loop through and display the added segments (from 1 segment to all segments)
for (byte added = 0; added < 8; ++added) {
delay(500); // Wait for half a second
addedSegWrite(added); // Display the added segments (incremental)
Serial.print("total added segments: "); // Print to the serial monitor
Serial.println(added); // Print the current added segment count
}
delay(1000); // Wait for 1 second before restarting the loop
Serial.println(""); // Print a new line for separation in the serial monitor
}
// Function to write a digit to the seven-segment display
void sevenSegWrite(byte digit) {
digitalWrite(latchPin, LOW); // Pull latch pin LOW before sending data
// Send the bit pattern of the digit to the shift register (LSB first)
shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);
digitalWrite(latchPin, HIGH); // Pull latch pin HIGH after sending data
}
// Function to display one segment at a time (a to g)
void singleSegWrite(byte segment) {
digitalWrite(latchPin, LOW); // Pull latch pin LOW before sending data
// Send the bit pattern for a single segment to the shift register
shiftOut(dataPin, clockPin, LSBFIRST, single_segments[segment]);
digitalWrite(latchPin, HIGH); // Pull latch pin HIGH after sending data
}
// Function to display 1 to 7 segments progressively
void addedSegWrite(byte added) {
digitalWrite(latchPin, LOW); // Pull latch pin LOW before sending data
// Send the bit pattern for the added segments to the shift register
shiftOut(dataPin, clockPin, LSBFIRST, added_segments[added]);
digitalWrite(latchPin, HIGH); // Pull latch pin HIGH after sending data
}