traffic director problems

hello, I was working on a more complicated version and it was not working so i decided to just go simpler and its still not working.

basis for the project:

button 1 turns on the "warning" function. button 2 turns on the "left" function. button 3 turns on the "center" function. button 4 turns on the "right" function.

if i dont include the if statements for the buttons and just have one option the the code works fine and i can change the variable for which pattern flashes.

when its all together it just randomly flashes in an unknown/random pattern and the buttons do nothing.

your help would be greatly appreciated. thank you.

int clockPin = 13; //IC Pin 11, orange Jumper
int dataPin = 12; //IC Pin 14, yellow Jumper
int latchPin = 11; //IC Pin 12, black Jumper

int button1value = 0;
int button2value = 0;
int button3value = 0;
int button4value = 0;

int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;

boolean last1 = LOW;
boolean last2 = LOW;
boolean last3 = LOW;
boolean last4 = LOW;
boolean current1 = LOW;
boolean current2 = LOW;
boolean current3 = LOW;
boolean current4 = LOW;

boolean led1on = false;
boolean led2on = false;
boolean led3on = false;
boolean led4on = false;

byte warning[800] = {
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B10000000, 200,
  B00000001, 200,
  B01000000, 200,
  B00000010, 200,
  B00100000, 200,
  B00000100, 200,
  B00010000, 200,
  B00001000, 200,
  B10000000, 200,
  B00000001, 200,
  B01000000, 200,
  B00000010, 200,
  B00100000, 200,
  B00000100, 200,
  B00010000, 200,
  B00001000, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100
};


byte right[18] = {
  B10000000, 200,
  B11000000, 200,
  B11100000, 200,
  B11110000, 200,
  B11111000, 200,
  B11111100, 200,
  B11111110, 200,
  B11111111, 200,
  B00000000, 200
};

byte left[18] = {
  B00000001, 200,
  B00000011, 200,
  B00000111, 200,
  B00001111, 200,
  B00011111, 200,
  B00111111, 200,
  B01111111, 200,
  B11111111, 200,
  B00000000, 200
};

byte center[18] = {
  B00011000, 255,
  B00011000, 255,
  B00111100, 255,
  B00111100, 255,
  B01111110, 255,
  B01111110, 255,
  B11111111, 255,
  B11111111, 255,
  B00000000, 255
};

int index = 0;
int count = sizeof(warning) / 2;
int count2 = sizeof(right) / 2;
int count3 = sizeof(left) / 2;
int count4 = sizeof(center) / 2;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
}
boolean debounce(boolean last1) {
  boolean current1 = digitalRead(button1);
  if (last1 != current1) {
    delay(5);
    current1 = digitalRead(button1);
  }
  return current1;
}

boolean debounce2(boolean last2) {
  boolean current2 = digitalRead(button2);
  if (last2 != current2) {
    delay(5);
    current2 = digitalRead(button2);
  }
  return current2;
}

boolean debounce3(boolean last3) {
  boolean current3 = digitalRead(button3);
  if (last3 != current3) {
    delay(5);
    current3 = digitalRead(button3);
  }
  return current3;
}

boolean debounce4(boolean last4) {
  boolean current4 = digitalRead(button4);
  if (last4 != current4) {
    delay(5);
    current4 = digitalRead(button4);
  }
  return current4;
}

void loop() {
  current1 = debounce(last1);
  if (last1 == LOW && current1 == HIGH)
  {
    led1on = !led1on;
  }
  last1 = current1;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, warning[index * 2]);
  digitalWrite(latchPin, HIGH);
  delay(warning[(index * 2) + 1]);
  index++;
  if (index >= count) {
    index = 0;
  }


  current4 = debounce4(last4);
  if (last4 == LOW && current4 == HIGH)
  {
    led4on = !led4on;
  }
  last4 = current4;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, right[index * 2]);
  digitalWrite(latchPin, HIGH);
  delay(right[(index * 2) + 1]);
  index++;
  if (index >= count2) {
    index = 0;
  }


  current2 = debounce2(last2);
  if (last2 == LOW && current2 == HIGH)
  {
    led2on = !led2on;
  }
  last2 = current2;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, left[index * 2]);
  digitalWrite(latchPin, HIGH);
  delay(left[(index * 2) + 1]);
  index++;
  if (index >= count3) {
    index = 0;
  }


  current3 = debounce3(last3);
  if (last3 == LOW && current3 == HIGH)
  {
    led3on = !led3on;
  }
  last3 = current3;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, center[index * 2]);
  digitalWrite(latchPin, HIGH);
  delay(center[(index * 2) + 1]);
  index++;
  if (index >= count4) {
    index = 0;
  }
}

This type of problem often indicates that the wiring is wrong. When input pins are improperly wired or improperly set up in the software, they tend to float randomly to HIGH and LOW values. However, you did not provide any wiring information.

Each switch should be wired from an Arduino pin to ground. If you have a recent version of the Arduino IDE, the pinMode for the Arduino pin should be INPUT_PULLUP. LOW means that the switch is closed, and HIGH means that the switch is open.

By the way, whenever you are tempted to suffix variable names with numbers (such as button1, button2, button3, button4), it is time to learn about defining and using arrays.

im super new to this whole thing. i got this far by watching tutorials on youtube and reading the lessons on this site. im currently using a pull down setup for the buttons.

all pieces are wired correctly, ive tested the individual components with simpler programs.

either being by themselves or just something simple. but all wiring has been verified.

Well I notice that these sections follow each other in loop(). Honestly I can't follow your logic because your variable names make my head spin. But are you sure you really want to do this?

  index++;
  if (index >= count) {
    index = 0;
  }

...

  index++;
  if (index >= count2) {
    index = 0;
  }

...

  index++;
  if (index >= count3) {
    index = 0;
  }

...

  index++;
  if (index >= count4) {
    index = 0;
  }

????

that might be the problem, how would i go about separating those bits?

byte warning[800] = {
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B10101010, 200,
  B01010101, 200,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B11110000, 255,
  B00001111, 255,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B11110000, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B00001111, 50,
  B00000000, 50,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10001000, 100,
  B01000100, 100,
  B00100010, 100,
  B00010001, 100,
  B00100010, 100,
  B01000100, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B10000001, 100,
  B01000010, 100,
  B00100100, 100,
  B00011000, 100,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 50,
  B11111111, 50,
  B00000000, 255,
  B10000000, 200,
  B00000001, 200,
  B01000000, 200,
  B00000010, 200,
  B00100000, 200,
  B00000100, 200,
  B00010000, 200,
  B00001000, 200,
  B10000000, 200,
  B00000001, 200,
  B01000000, 200,
  B00000010, 200,
  B00100000, 200,
  B00000100, 200,
  B00010000, 200,
  B00001000, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 200,
  B00110011, 200,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100,
  B11001100, 100,
  B00110011, 100
};


byte right[18] = {
  B10000000, 200,
  B11000000, 200,
  B11100000, 200,
  B11110000, 200,
  B11111000, 200,
  B11111100, 200,
  B11111110, 200,
  B11111111, 200,
  B00000000, 200
};

wow...

I can appreciate all of your efforts to create these arrays, but time would have been better spent if you studied how to use functions, structs and bit shifting.

like i said, im super new, not sure where to go from here. ive literally just placed bits and pieces of info ive picked up from youtube tutorials.

d.raymond: like i said, im super new, not sure where to go from here. ive literally just placed bits and pieces of info ive picked up from youtube tutorials.

you've gone down too far down the rabbit hole, I'm afraid.

I'd start over, make simple non-blocking functions that do the basic animations and learn how to call them sequentially.

i dont want to call them sequentially, i want each button to call its own function.

the warning array could easily be just a few simple functions rather than a huge array that is called in a blocking function... is my point.

but , you've invested a lot of time in this non-functional code, I feel your need to preserve the effort!

its not the effort i need to preserve, if there is a better way to write the code ill take the help, when i found shift registers i liked the binary expression cause im using 8 lights and there are 8 digits, so it lets me physically see which lights are off and on. considering its only 4 arrays tho i guess i could go with a cycle through approach.

more than anything, i just want it to work.

start with the basics....

do this:

  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B10101010, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,
  B01010101, 50,
  B00000000, 50,

using a single non-blocking function and no arrays.... instead use just two constant variables

once you can do that, you can do the rest.

im not sure what your trying to say.

the code you showed is just 3 different binary functions over and over again.

d.raymond: im not sure what your trying to say.

the code you showed is just 3 different binary functions over and over again.

no, that is just your data...

a non-blocking function would look like this:

void flutter(unsigned long interval)
{
  static unsigned long lastMillis = 0;
  static int mask[] = { 0b0, 0b10101010, 0b0, 0b01010101};  // edited typo
  static int idx = 0;
  if(millis() - lastMillis > interval)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, mask[idx++ % 4]);
    lastMillis = millis();
  }
}

that would work for a simple flash back and forth, but i have a more complex flash pattern that is desired.

d.raymond: that would work for a simple flash back and forth, but i have a more complex flash pattern that is desired.

exactly!

build the little functions and chain them.

that is what your data shows you are doing.

learn bit shifting and it will be even easier.

not quite sure how to do that, but i will look into bit shifting.

right here:

B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,
  B10000000, 100,
  B01000000, 100,
  B00100000, 100,
  B00010000, 100,
  B00001000, 100,
  B00000100, 100,
  B00000010, 100,
  B00000001, 100,
  B00000010, 100,
  B00000100, 100,
  B00001000, 100,
  B00010000, 100,
  B00100000, 100,
  B01000000, 100,

classic Knight Rider!!!

void knightRider(unsigned long interval)
{
  static unsigned long lastMillis = 0;
  static int mask = 0b1;
  int direction = 1;
  if(millis() - lastMillis > interval)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, mask);
    if(direction == 1)
    {
      mask <<= 1;
      if(mask = 0b10000000)
      {
        direction = -1;
      }
    }
    else
    {
      mask >>= 1;
      {
        if (mask = 1)
        {
          direction = 1;
        }
      }
    }
    lastMillis = millis();
  }
}