Change to next code with the help of a button

Hi I'm really new to arduino. I've worked with some microcontrollers before but not alot. I've also got some skills in C++ but they are really rusty.

My question is, I'm trying to use an impulse button for my Arduino Nano to change part in my code, but I can't get it to work. Probably my bad though.

I've got 6 LEDs connected individually to seperate pins on the arduino. The code for the patterns of the LEDs are working but i want to press the button and move on to the next type of pattern. I'll post my code here aswell in the hope that someone know how I can make it work!

I know that the button works in itself. Tested with an multimeter and have tested just turning one LED on and off.

(P.S. it's for an ugly christmas sweater)

//test button for arduino to change code-blocks.

int led = 3;           // the PWM pin the LED is attached to
int led2 = 5; 
int led3 = 6; 
int led4 = 9; 
int led5 = 10; 
int led6 = 11; 

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


void setup() {
  pinMode(2, INPUT); //Push Button
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);

}

void loop() {
 int buttonState = digitalRead(2); //Read and store state of push Button
 int buttonNumber = 0;
  if (buttonState = LOW){
    buttonNumber = buttonNumber + 1;
  }

  if (buttonNumber == 0){
    // set the brightness of pin 9:
    analogWrite(led, brightness);
    analogWrite(led2, brightness);
    analogWrite(led3, brightness);
    analogWrite(led4, brightness);
    analogWrite(led5, brightness);
    analogWrite(led6, brightness);

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount;
    }
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
    
  }
  else if (buttonNumber == 1){
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);               // wait for a second
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(100); 
      {digitalWrite(led2, HIGH);
    delay(100);
    digitalWrite(led2, LOW);
    delay(100);}
      {digitalWrite(led3, HIGH);
    delay(100);
    digitalWrite(led3, LOW);
    delay(100);}
      {digitalWrite(led4, HIGH);
    delay(100);
    digitalWrite(led4, LOW);
    delay(100);}
      {digitalWrite(led5, HIGH);
    delay(100);
    digitalWrite(led5, LOW);
    delay(100);}
      {digitalWrite(led6, HIGH);
    delay(100);
    digitalWrite(led6, LOW);
    delay(100);}
    
  }
  else if (buttonNumber == 2){
  digitalWrite(led6, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led, LOW); 

  // doubles skipping
  digitalWrite(led, HIGH);
  digitalWrite(led2, HIGH);
  delay(500);
  digitalWrite(led, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(500);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  delay(500);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(500);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led, HIGH);
  delay(500);
  }
  else if (buttonNumber = 3){
    buttonNumber = 0;
  }
}

  if (buttonState = LOW)Before tackling any other issues fix this.

Should it be HIGH or just "=="? Sorry not really good at this.

= is used to assign a value to a variable
== is used to compare values

Does that help ?

As to whether it should be LOW or HIGH depends on how the input is wired. Do you have a pullup or pulldown resistor in place to keep the input at a known voltage or is it floating in the breeze ? If no such resistor is in place then I suggest that you use INPUT_PULLUP in pinMode() and wire the switch to go to GND when the button is pressed. You may also need to debounce the input.

UKHeliBob:
= is used to assign a value to a variable
== is used to compare values

Does that help ?

As to whether it should be LOW or HIGH depends on how the input is wired. Do you have a pullup or pulldown resistor in place to keep the input at a known voltage or is it floating in the breeze ? If no such resistor is in place then I suggest that you use INPUT_PULLUP in pinMode() and wire the switch to go to GND when the button is pressed. You may also need to debounce the input.

Yes that helps alot!

Well I'm not sure if I have a pull-up or not. I'm trying to figure out if the Arduino Nano has one built in or not. I know that the Uno does but otherwise I'm confused. I'm using pin D2 right now as I though that one the pull-up/pull-down.

But I'll try this INPUT_PULLUP

I'm trying to figure out if the Arduino Nano has one built in or not.

The pullup resistors are built into the AtMeg328 chip that the UNO AND the Nano use.

Okay so I got it kind of working!

I changed the buttonState to "== LOW", and put the "int buttonNumber = 0" in the void setup so it dosent change back to 0 every time the program loops.

So now I can change between the programs but it's flawed. The program dosent change every time I press the button but maby every 3rd time or so. And it can't change back to the first without me pressing the "reset" button on the arduino itself.

Thanks for you help stranger, you saved my Christmas! If you know how I can fix the bugs more help is appriciated!

PaulS:
The pullup resistors are built into the AtMeg328 chip that the UNO AND the Nano use.

That is good to know, thanks! Is it dependent on a certain pin or chan you use it on any pin?

The program dosent change every time I press the button but maby every 3rd time or so

Looking at your program that was predictable because of your extensive use of delay() which prevents the input being read frequently, hence me commenting in reply #1

Before tackling any other issues

So, what to do ?
The solution is to change to using millis() for timing and not to block the free running of the loop() function.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

UKHeliBob:
Looking at your program that was predictable because of your extensive use of delay() which prevents the input being read frequently, hence me commenting in reply #1
So, what to do ?
The solution is to change to using millis() for timing and not to block the free running of the loop() function.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Thank you so much UKHeliBob! I'll try to improve my coding skills beacause this stuff is pretty fun!

Happy holidays man!

Is it dependent on a certain pin or chan you use it on any pin?

All of the digital pins, even those masquerading as analog pins, have pullup resistors.

PaulS:
All of the digital pins, even those masquerading as analog pins, have pullup resistors.

Thanks!