Advice for LED Program Needed

So like many posting here I am new to Arduino and coding. I’ve been reading my rear end off and trying to understand how the programs are actually working. I’ve made some decent progress so far, going through Ladyada’s tutorials, the shiftOut, and others. In fact I’ve lashed together as best I could some code from both places to make a program which is starting down the road where I need to go…it works however it’s not working just right and I’m not sure it’s even the best road to go down. I’ve placed my code so far at the bottom. What I want to do is have multiple light patterns to be displayed on a custom LED light bar (think Knight Rider Kit but better), and to have each pattern scroll through one at a time with each press of the button. My code below is a start to this however it doesn’t always listen to the button, I think my problem is that as it’s executing the first light code it’s not watching to see a press of the button. Would a millis() fix this possibly? Don’t know how to code that yet but I could work on it. Or is there a better way I don’t know about?

And I guess the big overall question is is a nested if() statement style like I’m using now in the code below the best way to go about doing what I want to do? Or is there a better structure to use that I can read up on and try to impliment? Eventually when I have all the light programs (about 8 total) all coded the way I want them for effect, I’ll replace the button with a car remote set up
and have the programs scroll through with each press of the remote…but that’s for much later on!

Thanks in advance for any help/advice you may be able to provide! :slight_smile:

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
//Pin connected to buttonswitch
int switchPin = 2;

int val; //intial value
int val2; //second value reading
int buttonState; //state of button

int lightMode = 0; //to start off with

//holders for infromation you’re going to pass to shifting function
byte data;
byte dataArray[15];

void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(switchPin, INPUT);

//Arduino doesn’t seem to have a way to write binary straight into the code
//so these values are in HEX. Decimal would have been fine, too.
dataArray[0] = 0x81; //10000001
dataArray[1] = 0xC3; //11000011
dataArray[2] = 0xE7; //11100111
dataArray[3] = 0xFF; //11111111
dataArray[4] = 0x7E; //01111110
dataArray[5] = 0x3C; //00111100
dataArray[6] = 0x18; //00011000
dataArray[7] = 0x00; //00000000
dataArray[8] = 0x18; //00011000
dataArray[9] = 0x3C; //00111100
dataArray[10] = 0x7E; //01111110
dataArray[11] = 0xFF; //11111111
dataArray[12] = 0xE7; //11100111
dataArray[13] = 0xC3; //11000011
dataArray[14] = 0x81; //10000001

buttonState = digitalRead(switchPin);


void loop() {

val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // if its off
lightMode = 1; // turn lights on!
} else {
if (lightMode == 1) { // if its all-on
lightMode = 2; // make it wave
} else {
if (lightMode == 2) { // if its waving
lightMode = 0; // turn it off
buttonState = val; // save the new state in our variable

if (lightMode == 0) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);

if (lightMode == 1) {
for (int j = 0; j < 15; j++) {
//load the light sequence you want from array
data = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, data);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
if (lightMode == 2) {
for (int j = 0; j < 8; j++) {

//This function uses bitwise math to move the pins up
void lightShiftPinA(int p) {
//defines a local variable
int pin;

//this is line uses a bitwise operator
//shifting a bit left using << is the same
//as multiplying the decimal number by two.
pin = 1<< p;

//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, pin);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low

//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);

//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);

//for each bit in the byte myDataOut?
//This means that %00000001 or “1” will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i–) {
digitalWrite(myClockPin, 0);

//if the value passed to myDataOut and a bitmask result
// true then… so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
else {
pinState= 0;

//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);

//stop shifting
digitalWrite(myClockPin, 0);