7 segment display with shift register(74HC595) and press button

Hello everyone!

i'm here tonight asking for your help after 4hours of trying but in vain! i did 0 progress after doing all the cables on the breadboard :confused:

i'm trying to use a counter on a 7 segment display (linked to a shift register 74HC595 ) using a pressbutton ; where ever i press the button , i want the number (0-9) to add +1 .

I was able to do that without the Shift by linking the 7SegDisplay to the Arduino Uni directly , but this is just a small part of a long school project.

I'm in a "Burn out" state right now ; so i can't think of anything :confused:

Thanks !

You've used Google ?

There may be better examples but this might get you started:

6v6gt:
You've used Google ?

There may be better examples but this might get you started:
Using a 74HC595 to connect an Arduino to a 7-segment LED display - Anything Arduino episode 9 - YouTube

I was able to do that , but my main problem is that i couldn't connect the Display to the button :confused:

You don't connect the display to the button. You use the button to increment a counter which you define in the sketch. The counter starts at 0. The first button push sets it to 1. The second button push sets it to 2. etc. etc. When it gets to 9, the following button push sets it back to 0.

The counter can be an int or a byte.

It is the value of this counter which you then use to determine what segments on the LED have to be lit.
Then you push out to the shift register values which indicate which segments should be lit.

6v6gt:
You don't connect the display to the button. You use the button to increment a counter which you define in the sketch. The counter starts at 0. The first button push sets it to 1. The second button push sets it to 2. etc. etc. When it gets to 9, the following button push sets it back to 0.

The counter can be an int or a byte.

It is the value of this counter which you then use to determine what segments on the LED have to be lit.
Then you push out to the shift register values which indicate which segments should be lit.

Do you have a code i can use as a reference please , it's 1AM here and as i said i'm burned out , can't think of anything + i just started Arduino 7h ago ...
and thanks again for responding

const byte latchPin = 5;  // Pin connected to Pin 5 of 74HC595 (Latch)
const byte dataPin  = 6;   // Pin connected to Pin 6 of 74HC595 (Data)
const byte clockPin = 7;   // Pin connected to Pin 7 of 74HC595 (Clock)

const byte pushbutton = 12;     // pushbutton connected to pin 12 for increment by 1



const byte Digit = 8;     // transistor for ones digit of  connected to pin 8



int prevState = 0;
int currState = 1;


int counterFirst = 0;           // initialise first counter as zero

byte numberOfDigits = 4;

const byte numbers[10] =   // Describe each digit in terms of display segments  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
{
  B11111100, //0
  B01100000, //1
  B11011010, //2
  B11110010, //3
  B01100110, //4
  B10110110, //5
  B10111110, //6
  B11100000, //7
  B11111110, //8
  B11110110, //9
};

void setup()
{
  pinMode(latchPin, OUTPUT);   // set shift registers pins to output
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  pinMode(pushbutton, INPUT);         
  pinMode(Digit, OUTPUT);

  
}

void loop()
{
 
  currState = digitalRead(pushbutton);
  if (prevState != currState)           // has the state changed from HIGH to LOW or vice versa 
  {                                         
    prevState = !prevState;             //invert
    if (currState == HIGH && counterFirst <= 9)  // If the button was pressed AND not reached 9
        counterFirst++;                                     // increment the counter by one

    if(counterFirst > 9) counterFirst = 0;                 //if counter is greater than 9, set it to 0

  }
  
  
  
  showNumber( (100 * counterFirst));           // display the current digit 
}

void showNumber(int number)
{
  

  for(byte i = 0; i < numberOfDigits; i++)
  {
   
    digitalWrite(Digit, LOW ) ;   // switch off Digit

    digitalWrite(latchPin, LOW);   // Set latchPin LOW while clocking bits in to the register

    shiftOut(dataPin, clockPin, LSBFIRST, numbers[number % 10]);
    number /= 10;

   digitalWrite(latchPin, HIGH);     //set latchPin to high to lock and send data

     digitalWrite(Digit, ( i == 0 ) ) ; // switch on/off Digit1   
 
     delay(5);
  }
}

This is what i was so far able to get from doing some researches , i gotta go to the school laboratory to test it tho , do you think this is gonna work ?

You are using a 4 digit display but are counting only from 0 to 9 ?

byte numberOfDigits = 4;

The following code can be tested in isolation by adding Serial.print() statements to see what it is doing.
As a crude debounce, it might need a small delay() in the outer “if” block.
You have a pull down resistor (say 10k) on the button pin, assuming from your code the button is wired between Vcc and the pin ?

  currState = digitalRead(pushbutton);
  if (prevState != currState)           // has the state changed from HIGH to LOW or vice versa
  {
    prevState = !prevState;             //invert
    if (currState == HIGH && counterFirst <= 9)  // If the button was pressed AND not reached 9
      counterFirst++;                                     // increment the counter by one

    if (counterFirst > 9) counterFirst = 0;                //if counter is greater than 9, set it to 0

  }

why the multiplication by 100 ?

showNumber( (100 * counterFirst));           // display the current digit

The rest appears to be standard code.

You are using a 4 digit display but are counting only from 0 to 9 ?

why the multiplication by 100 ?

I was basing my code on a 4 digit display , and i tried to change the code to a 1 digit display . i changed that ! thanks

I’ll go to the laboratory right now and see ^^ thanks a lot