/*
This sketch uses:
- A 74HC595 (sr1) connected to digital pins 8 (DS), 9 (SHCP), and 10 (STCP) to drive a
7-segment display (sevseg1). The segments are mapped as:
Q0 -> A, Q1 -> B, Q2 -> C, Q3 -> D, Q4 -> E, Q5 -> F, Q6 -> G, Q7 -> DP.
- A 10-segment LED bar graph (bargraph1) connected to digital pins 22-31.
- Two LEDs (led1, led2) connected on pins 32 and 33 to indicate even/odd numbers.
Every 2 seconds the counter increments from 0 to 9 and updates all outputs simultaneously.
Note:
The seven-segment display is assumed to be common anode. Therefore, a segment is lit when
its corresponding output from the 74HC595 is LOW. The digit patterns are defined accordingly.
*/
#include <Servo.h> // Included as per allowed packages (though not used in this sketch)
// Pin assignments for the shift register (74HC595)
const int dataPin = 8; // DS
const int clockPin = 9; // SHCP
const int latchPin = 10; // STCP
// LED pins for parity indication
const int ledEvenPin = 32; // led1 will indicate even numbers
const int ledOddPin = 33; // led2 will indicate odd numbers
// Bar graph LED anode pins (corresponding to bargraph1:A1 ... A10)
const int barGraphPins[10] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
// Seven-segment display lookup table for common-anode configuration.
// For common anode, a segment is lit by driving it LOW. Bit order: bit0=A, bit1=B,..., bit6=G, bit7=DP.
// The digits below come from inverting common-cathode codes {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}
byte digitPatterns[10] = {
0xC0, // 0 : 0xC0 = ~0x3F
0xF9, // 1 : ~0x06
0xA4, // 2 : ~0x5B
0xB0, // 3 : ~0x4F
0x99, // 4 : ~0x66
0x92, // 5 : ~0x6D
0x82, // 6 : ~0x7D
0xF8, // 7 : ~0x07
0x80, // 8 : ~0x7F
0x90 // 9 : ~0x6F
};
void setup() {
// Initialize shift register pins
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Initialize LED parity pins
pinMode(ledEvenPin, OUTPUT);
pinMode(ledOddPin, OUTPUT);
// Initialize bar graph LED pins
for (int i = 0; i < 10; i++) {
pinMode(barGraphPins[i], OUTPUT);
digitalWrite(barGraphPins[i], LOW); // Turn off initially
}
// Initially turn off the parity indicator LEDs
digitalWrite(ledEvenPin, LOW);
digitalWrite(ledOddPin, LOW);
// Initialize the seven-segment display to 0 by shifting out the proper pattern.
updateShiftRegister(digitPatterns[0]);
}
void loop() {
for (int number = 0; number < 10; number++) {
// Update the 7-segment display by shifting out digit pattern.
updateShiftRegister(digitPatterns[number]);
// Update the bar graph: light up first "number" segments.
for (int i = 0; i < 10; i++) {
if (i < number) {
digitalWrite(barGraphPins[i], HIGH);
}
else {
digitalWrite(barGraphPins[i], LOW);
}
}
// Update parity LEDs: even --> ledEven on, odd --> ledOdd on.
if (number % 2 == 0) {
digitalWrite(ledEvenPin, HIGH);
digitalWrite(ledOddPin, LOW);
}
else {
digitalWrite(ledEvenPin, LOW);
digitalWrite(ledOddPin, HIGH);
}
// Wait exactly 2 seconds before the next update.
delay(2000);
}
}
// Function to update shift register with a byte of data
// Uses shiftOut() with LSBFIRST so that bit0 is shifted first and lands at Q0.
void updateShiftRegister(byte data) {
digitalWrite(latchPin, LOW); // Begin output transaction by setting latch low
shiftOut(dataPin, clockPin, LSBFIRST, data);
digitalWrite(latchPin, HIGH); // Latch the output; all connected segments update simultaneously
}