#include "SevSegShift.h"
SevSeg sevseg;
const int data_pin = 7; // 74HC165
const int clock_pin = 8; // 74HC165
const int latch_pin = 9; // 74HC165
const int numBits = 8; // Set to 8 * number of shift registers
byte lastButtonStates = 0; // Stores the previous button states
byte currentButtonStates = 0; // Stores the current button states
int buttonPushCounter = 0; // counter for the number of button presses
const int LATCH_PIN = A1; // 74HC595 pin 12
const int DATA_PIN = A0; // 74HC595 pin 14
const int CLOCK_PIN = A2; // 74HC595 pin 11
// Define segment patterns for 7-segment display (assuming COMMON_ANODE)
byte segmentPatterns[] = {
0b11000000, // Digit 0: 0
0b11111001, // Digit 1: 1
0b10100100, // Digit 2: 2
0b10110000, // Digit 3: 3
0b10011001, // Digit 4: 4
0b10010010, // Digit 5: 5
0b10000010, // Digit 6: 6
0b11111000, // Digit 7: 7
0b10000000, // Digit 8: 8
0b10010000 // Digit 9: 9
};
void setup() {
byte numDigits = 1;
byte digitPins[] = {2}; // At least one element needed
byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9}; // A to G
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_ANODE; // Assuming COMMON_ANODE configuration
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
pinMode(data_pin, INPUT_PULLUP);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
buttonPushCounter = 0; // Initialize the counter to 0
Serial.begin(9600);
}
void loop() {
// Step 1: Sample 74HC165
digitalWrite(latch_pin, LOW);
digitalWrite(latch_pin, HIGH);
// Step 2: Shift and Read Button States from 74HC165
currentButtonStates = 0;
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(data_pin);
if (bit == HIGH) {
bitWrite(currentButtonStates, i, 1);
}
digitalWrite(clock_pin, HIGH);
digitalWrite(clock_pin, LOW);
}
// Check for changes in button states and act accordingly
for (int i = 5; i <= 7; i++) {
bool currentBit = bitRead(currentButtonStates, i);
bool lastBit = bitRead(lastButtonStates, i);
if (currentBit != lastBit && currentBit == 1) {
switch (i) {
case 5:
// Yellow button pressed, increment the displayed number by 1
buttonPushCounter++;
if (buttonPushCounter >= 10) {
buttonPushCounter = 0;
}
break;
case 6:
// Red button pressed, decrement the displayed number by 1
buttonPushCounter--;
if (buttonPushCounter < 0) {
buttonPushCounter = 9;
}
break;
case 7:
// Green button pressed, reset the counter to 0
buttonPushCounter = 0;
break;
default:
break;
}
}
}
// Store the current button states for the next iteration
lastButtonStates = currentButtonStates;
// Update the display with the counter value using 74HC595
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, segmentPatterns[buttonPushCounter]);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
delay(50); // Delay for debouncing and to prevent excessive output
}