seven segment sketch problem


I downloaded a sparkfun sketch which counted 0 to 9 and then back to 0 and so on. As a complete beginner I was really please to see it work. I wanted to moved on to adding 2 buttons to this, so I could control the the numbers up and down. I found a sketch for the buttons and pasted it in. But what happens is the numbers now scroll between 0 and 9 and so on. If I press the up button it still just scrolls but if I press the down button and hold it down the scrolling stops. I hope I have attached the correct file or even attached one. Can someone have a look at the code for me please.

digitdriv.ino (3.98 KB)

Here’s the OPs code.

 Controlling large 7-segment displays
 By: Nathan Seidle
 SparkFun Electronics
 Date: February 25th, 2015
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 The large 7 segment displays can be controlled easily with a TPIC6C594 IC. This code demonstrates how to control
 one display.

 Here's how to hook up the Arduino pins to the Large Digit Driver

 Arduino pin 6 -> CLK (Green on the 6-pin cable)            
 5 -> LAT (Blue)                                           
 7 -> SER on the IN side (Yellow)                          
 5V -> 5V (Orange)
 Power Arduino with 12V and connect to Vin -> 12V (Red)
 GND -> GND (Black)

 There are two connectors on the Large Digit Driver. 'IN' is the input side that should be connected to
 your microcontroller (the Arduino). 'OUT' is the output side that should be connected to the 'IN' of addtional

 Each display will use about 150mA with all segments and decimal point on.

//GPIO declarations
byte segmentClock = 6;
byte segmentLatch = 5;
byte segmentData = 7;
int upButton = 3;     //wire one upButton lead to pin 3, and the most adjacent lead to GND
int downButton = 2;  //wire one downButton lead to pin 2, and the most adjacent lead to GND
int score = 0;        // start score at 0


void setup()
  Serial.println("Large Digit Driver Example");

  pinMode(segmentClock, OUTPUT);
  pinMode(segmentData, OUTPUT);
  pinMode(segmentLatch, OUTPUT);
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);

  digitalWrite(segmentClock, LOW);
  digitalWrite(segmentData, LOW);
  digitalWrite(segmentLatch, LOW);

void loop()
  postNumber(score, false); //Show decimal
  digitalWrite(segmentLatch, LOW);
  digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
    score %= 10; //Reset score after 9
    Serial.println(score); //For debugging
    if(score < 0) score = 9; // go to 9 after 0
    Serial.println(score); //For debugging

//Takes a number and displays 2 numbers. Displays absolute value (no negatives)
void showNumber(float value)
  int number = abs(value); //Remove negative signs and any decimals

  //Serial.print("number: ");

  for (byte x = 0 ; x < 2 ; x++)
    int remainder = number % 10;

    postNumber(remainder, false);

    number /= 10;

  //Latch the current segment data
  digitalWrite(segmentLatch, LOW);
  digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK

//Given a number, or '-', shifts it out to the display
void postNumber(byte number, boolean decimal)
  //    -  A
  //   / / F/B
  //    -  G
  //   / / E/C
  //    -. D/DP

#define a  1<<0
#define b  1<<6
#define c  1<<5
#define d  1<<4
#define e  1<<3
#define f  1<<1
#define g  1<<2
#define dp 1<<7

  byte segments;

  switch (number)
    case 1: segments = b | c; break;
    case 2: segments = a | b | d | e | g; break;
    case 3: segments = a | b | c | d | g; break;
    case 4: segments = f | g | b | c; break;
    case 5: segments = a | f | g | c | d; break;
    case 6: segments = a | f | g | e | c | d; break;
    case 7: segments = a | b | c; break;
    case 8: segments = a | b | c | d | e | f | g; break;
    case 9: segments = a | b | c | d | f | g; break;
    case 0: segments = a | b | c | d | e | f; break;
    case ' ': segments = 0; break;
    case 'c': segments = g | e | d; break;
    case '-': segments = g; break;

  if (decimal) segments |= dp;

  //Clock these bits out to the drivers
  for (byte x = 0 ; x < 8 ; x++)
    digitalWrite(segmentClock, LOW);
    digitalWrite(segmentData, segments & 1 << (7 - x));
    digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK

Show us how you have your buttons wired. The code looks OK, so there may be an issue with your buttons.

Hi. The buttons are connected.

The 2 leads coming from each button 1 goes to pin 2 on the arduino and the other goes to gnd and on the other button 1 goes to pin 3 on the arduino and gnd. Eddie.

The buttons only have 2 pins? Or are they the normal style tac buttons that have 4 pins? If so, use pins that are diagonal from each other.

If that doesn't help you, then post up which buttons you have and a wiring diagram.

The buttons are just normal push buttons with 2 pins. They are normally open type.

Delta_G: and a wiring diagram.

I get so tired of my responses being ignored. .

Hi. Sorry I am not ignoring your responses. Do you want the whole wiring diagram? Arduino to shift register to seven segment. Eddie.

groundsman: Hi. Sorry I am not ignoring your responses. Do you want the whole wiring diagram? Arduino to shift register to seven segment. Eddie.

The whole enchilada.

I hope this is what you want.

Well that all looks OK. My suspicion from your description of the problem, continuously counts going up but stops cold when you press the down button, makes me think that the up button is registering as pressed all the time. So it goes up up up and if you press the down button it stops cold because it is going up-down up-down.

Maybe take a multimeter and check the button to make sure it isn't stuck closed or something.

Hi. Spot on. I checked both buttons and found one had some current flowing though it. It now works perfect. Thank you very much. Can I explain what I am trying to do. I want to learn about Arduinos while I try to build a scoreboard for my Cricket club. My next step is to try with help from people like you to program my Arduino to control 3 sets of numbers 3digit, 2digit and a single digit. Is it possible to do? Whould the coding be hard to write.? If you could give me a bit of help I would be very grateful. Again thank you for your help. Eddie.

That's possible. Not too hard even for a beginner. Have at it and when you hit a roadblock post the code here and ask.