//note LED dp is not connected in this circuit and the LED initializes at 0
int latchPin = 2; //STCP of the 74HC595 is connected to digitalpin 2, setting the output pin numbers
int dataPin = 3; //DS of the 74HC595 is connected to digitalpin 3, setting the output pin numbers
int clockPin = 4; //SHCP of the 74HC595 is connected to digitalpin 4, setting the output pin numbers
const int upbuttonPin = 15; //initializing pin A2 as the decrement pushbutton, setting the input pin numbers
const int downbuttonPin = 16; //initializing pin A1 as the increment pushbutton, setting the input pin numbers
int number = 0; //for tracking the current number displayed on the 7-segment, initializing "number", first number on LED screen will be "n"
int lastupbuttonState = LOW; //initializing the previous state of the up button
int lastdownbuttonState = LOW; //initializing the previous state of the down button
//note for binary number array, 0b00000000 corresponds to bits 0b 0,0,0,0,0,0,0,0 which corresponds to LED#s 0b dp,f,g,e,d,c,b,a
byte numbersArray[7] = {
//0b00111111, //LED #0 , for counting purposes in the code this is 0
0b01010100, //LED #n , for counting purposes in the code this is 0
0b00000110, //LED #1 , for counting purposes in the code this is 1
0b01011011, //LED #2
0b01001111, //LED #3
0b01100110, //LED #4
0b01101101, //LED #5
0b01111101 //LED #6 , for counting purposes in the code this is 6
};
void setup() {
//this will set the digital pins as outputs to the 74HC595 and therefore make the 74HC595 pins output as well
pinMode(latchPin, OUTPUT); //when held low the data does not display
pinMode(dataPin, OUTPUT); //datapin
pinMode(clockPin, OUTPUT); //clock
//this will set the A pins as inputs for the arduino when the button's connected are pressed and it receives a signal
pinMode(upbuttonPin, INPUT); //increment button set as input
pinMode(downbuttonPin, INPUT); //decrement button set as input
updateDisplay(number);
}
void loop() {
//for reading the state of the uppushbutton and downpushbutton value:
bool upbuttonState = digitalRead(upbuttonPin);
bool downbuttonState = digitalRead(downbuttonPin);
//check for up button state transitions (check if the up button is pressed)
if (upbuttonState == HIGH && lastupbuttonState == LOW) {
if(downbuttonState == LOW) {
number != 6 ? number += 1: number;
delay(250);
updateDisplay(number);
}
if (downbuttonState == HIGH){
delay(250);
number = 0; //change number to n, there will be no other counting
updateDisplay(number);
delay(250); //delay for debounce
}
}
if (downbuttonState == HIGH && lastdownbuttonState == LOW) {
if(upbuttonState == LOW) {
number != 0 ? number -= 1: number;
delay(250);
updateDisplay(number);
}
if (upbuttonState == HIGH){
delay(250);
number = 0; //change number to n, there will be no other counting
updateDisplay(number);
delay(250); //delay for debounce
}
}
//remember the last button state
lastupbuttonState = upbuttonState;
lastdownbuttonState = downbuttonState;
}
void updateDisplay(int num) {
//This is to make sure that the number is within the range 0-6 and include all the numbers in the array
num = num % 7;
//This will hold the latch low, load the bits in the shift register, then open the latch high to send of the bits. Changes the LEDs
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, numbersArray[num]);
digitalWrite(latchPin, HIGH);
}