Combining the push button and Shift register programs

I am trying to write a program for a shift register 74HC595 using the Aruino Uno in that every time a button is pushed, the shift register counts and displays a binary number on 8 LEDS. I am doing this in three parts to test the program, first the shift register program and then the push button program. I have not added the count part yet as I haven’t gotten the other two working together properly. The shift register part of the program works. It is the push button that I am having trouble with. By itself the push button program works, in that when I push the button the LED lights up and when I release the button the LED light goes out. But when I combine this program with the shift register program, the LED light only looks at the state of the button when the program is loaded, even though it compiles without errors. So if the Button is pushed when the program loads the LED light is on, if the button is not pushed when the program loads the LED is off.
Here is my sketch, I’ve borrowed from public domain programs and if I don’t credit properly I apologize.
The ledPin=4 is for testing the push button. And theNumber=32 is an arbitrary number to test the shift register.

//  Name    : shiftOutCode, Hello World                                
//  Author  : Carlyn Maw,Tom Igoe, David A. Mellis 
//  Date    : 25 Oct, 2006    
//  Modified: 23 Mar 2010                                 
//  Version : 2.0                                             
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                           

//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
////Pin connected to DS of 74HC595
const int dataPin = 11;
//Button Code
int buttonPin=2;
int ledPin=4;
//variables will change
int buttonState=0;
int timer=100;
int theNumber=32;

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

void loop() {
    // count from 0 to 255 and display the number 
  // on the LEDs
  for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
    // take the latchPin low so 
    // the LEDs don't change while you're sending in bits:
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, theNumber);  

    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:

I’ve been hitting my head on this for a few days. I think that when they say Loop means what it means I am under the impression that the program continues to loop, that is go back to the beginning of the program and check the status of the push button as it seems to do when the program is by itself. For some reason it is not looping back and checking the status of the push button when combined with the shift register program, or does loop not mean what I think it means.

Thank you.

Hello jrschenk

In your for loop …

for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++)

… you have …


While the delay is delaying, no other code gets executed. So you need to think how long it will take your for loop to complete, and where your switch test code is in relation to the for loop.



Wow! Thank you that was such a simple fix. I never looked at that part of the program because it worked, and why fix something that wasn’t broken. But played around with the delay (5) and it works as expected. Thank you. :slight_smile: