Button state not working properly

I am making a little police light flasher with 2x5 LED's and my Arduino. I have some different pieces of code for the different flashing modes. I put them together in a single and was hoping to figure out how to cycle between them with a little momantary push-button switch.

This is the code I have written:

It only runs program nr 2 and 3 in loop with about one second interval and nothing happens when i press the button..

Here is my setup:

Is there something wrong in my code or setup? Or maybe both?

If all the lights operate correctly, there is nothing wrong with the setup.

Is there something wrong with the code? Yes.

Each time loop is called, the state of the button is read. The digitalRead function returns HIGH or LOW, which you store in buttonState.

Then, you compare buttonState to 0. Since HIGH is 1 and LOW is 0, this comparison may be true or false, depending on whether the button state is HIGH when pressed or HIGH when released. Without knowing how the button is wired (the pullup resistor is not enabled, so you need an external resistor), and without knowing whether you pressed the button, or not, I can't say if the comparison should be, and is, true or false.

If it is, you run the first block of code. Otherwise you don't, and buttonState stays 0 or 1.

Then, you have another comparison of buttonState to 1. Again, this might or might not be true.

What you really need to do is write a simple sketch with just the switch and an LED connected. Turn the LED on when the button is pressed, and off when the button is released.

When that works, decide what you want to have happen. It seems like you would want to read the button press state, and increment a mode variable, from 0 to 1 to 2 to 0 to 1 to 2 to 0, incrementing each time the button was pressed.

To do this, you'll need to keep track of the previous button state (pressed or released) so you increment only once when the button is pressed, regardless of how long the button is held down. There is a button library that will make this easy for you. It has a uniquePress method that will return true only once when the button is pressed.

When you can move between modes (states) correctly, call the appropriate function for that mode. Move the long list of digitalWrite and delay function calls into separate functions, called LightMode1, LightMode2, LightMode3 for instance. The call LightMode2 when mode is 2.