7-segment and shiftout code acting odd

The code below counts from 0 to 9 just fine and loops continuously. However, I want to control the increment count with a button press. When I comment out the FOR loop and put the btnWhite read in place, the display segments don’t light properly. They light with each button press, but don’t resemble any number or letter unless of course you speak Klingon.

int x = 10;
int y = 10;
int buttonWhitePin = 2;
int clockPin = 12;
int dataPin = 11;
int dimmIn = 9;
byte data;
byte dataArray[11];

void setup() {
  pinMode(buttonWhitePin, INPUT);
  pinMode(dimmIn, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  dataArray[0] = 0x3F; //00111111 - 0
  dataArray[1] = 0x06; //00000110 - 1
  dataArray[2] = 0x5B; //01011011 - 2
  dataArray[3] = 0x4F; //01001111 - 3
  dataArray[4] = 0x66; //01100110 - 4
  dataArray[5] = 0x6D; //01101101 - 5
  dataArray[6] = 0x7D; //01111101 - 6
  dataArray[7] = 0x07; //00000111 - 7
  dataArray[8] = 0x7F; //01111111 - 8
  dataArray[9] = 0x67; //01100111 - 9
  dataArray[10] = 0x80; //decimal point
}

void loop() {
  int btnWhite = digitalRead(buttonWhitePin);

  for (int x = 0; x < 10; x++) {
//    if (btnWhite == LOW) {
      data = dataArray[x];
      digitalWrite(clockPin, HIGH);    
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(clockPin, LOW);
      digitalWrite(buttonWhitePin, HIGH);
      if (x == 9) x = -1;
      delay(300);
//    }
  }
  
}

If you comment out the for loop, x never changes, so the display should never change. Try posting the code after you've made your modifications. I get the feeling though, that you're going to want to take a look at the StateChangeDetection example.

My bad, there should be a line below the delay.

X++;

darrenmathews: My bad, there should be a line below the delay.

X++;

How about you post two seperate code blocks: one with the working code, and one with the non-working code. Neither should have any commented out code.

Code below counts from 0-9 and loops just fine. All segments display numbers as expected.

void loop() {
  for (int x = 0; x < 10; x++) {
      data = dataArray[x];
      digitalWrite(clockPin, HIGH);    
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(clockPin, LOW);
      delay(300);
  }
}

Code below recognizes button presses, but the display segments don’t resemble readable numbers.

void loop() {
  int btnWhite = digitalRead(buttonWhitePin);
    if (btnWhite == LOW) {
      data = dataArray[x];
      digitalWrite(clockPin, HIGH);    
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(clockPin, LOW);
      digitalWrite(buttonWhitePin, HIGH);
      if (x == 9) x = -1;
      delay(300);
      x++;
  }  
}
      digitalWrite(buttonWhitePin, HIGH);

Why are you setting the internal pull-up before the button is pressed? This should be placed after the switch pin's pinMode is set.

Try adding some debug prints. I would start by verifying that your x variables is correct. If so, I would then go check to make sure dataArray[x] is returning the correct sequence.

I modified the two code samples. I meant to remove the btnWhite in the FOR LOOP version. I set the btn to HIGH after the button is checked and the output is shifted.

I think I might have just stumbled onto some logic issues but won't be able to test until I get back to my shop at home. The very first time through X = 10 and would only display the decimal point(byte code from the array). I have the decimal points turned on via a dip switch on the back of the display so it essentially blanks all segments.

It then just gets incremented and from that point on. There is never a corresponding array value that is valid. My bad!

I need to move this line of code: if (x == 9) x = -1; to before the button press check IF statement and change it to read

if (x >= 9) x = 1;

Thank you!

darrenmathews: I set the btn to HIGH after the button is checked and the output is shifted.

Why? The button is an input, why are you trying to set it as if it's an output?

Using a digitalWrite with HIGH on an input pin enables the pullup resistor. This should be done in setup, not in a loop.

I'm a bit new, but if I understand correctly then, by pressing the button it then sets the pin state to HIGH. Is that correct?

In that case, I wouldn't need this line of code at all. Right?

digitalWrite(buttonWhitePin, HIGH);

darrenmathews: I'm a bit new, but if I understand correctly then, by pressing the button it then sets the pin state to HIGH. Is that correct?

Depends on how it's wired.

In that case, I wouldn't need this line of code at all. Right?

Depends on how it's wired, and whether or not you are currently using a pull-up resistor.

Lets make sure you have the switch wired correctly. Read this link on input pins, and decide if you want to use the internal pull-up resistor and tie the switch to ground OR use an external pull-down resistor and tie the switch to high.