buttons wont change states

I have coded everything and it isn’t giving me any errors and it uploads to the arduino fine, when change the “state” value something actually happens in real life but i cant get the state values to change using buttons ive tried so many things could anyone help me.

#define FLgreenLED 3    //defining which pins the LEDs are connected to 
#define FRgreenLED 4    //LEDs are labelled by their position first then their color eg. FRgreenLED - green LED in the front right corner
#define FLredLED 5
#define FRredLED 6
#define RLredLED 7
#define RRredLED 8

#define RRGB 22         //Red portion of RGB LED
#define GRGB 24         //Green portion of RGB LED
#define BRGB 26         //Blue portion of RGB LED

#define LDR A0

//-----------fading setup-----------//

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
long IntervalMode1 = 6;
long IntervalMode2 = 7;

int brightness = 0;
int fadeAmount = 5;
int RedBrightness = 0;
int GreenBrightness = 255;

//----------button setup 1----------//

#define buttonPin 2
int state = 0;
int old = 0;
int buttonPoll = 0;


//----------button setup 2----------//

#define buttonPin2 9
int state2 = 0;
int old2 = 0;
int buttonPoll2 = 0;


void setup() {
  
  pinMode(FLredLED, OUTPUT);
  pinMode(FRredLED, OUTPUT);
  pinMode(RLredLED, OUTPUT);
  pinMode(RRredLED, OUTPUT);
  pinMode(FLgreenLED, OUTPUT);
  pinMode(FRgreenLED, OUTPUT);

  pinMode(RRGB, OUTPUT);
  pinMode(GRGB, OUTPUT);
  pinMode(BRGB, OUTPUT);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);

  Serial.begin(9600);
  
}

void loop() {

  buttonPoll = digitalRead(buttonPin);
    if (buttonPin == 1){
      delay(50);
      buttonPoll = digitalRead(buttonPin);
      if (buttonPin == 0){
        state = old + 1;
      }
      else
      delay (100);
    }

  buttonPoll2 = digitalRead(buttonPin2);
    if (buttonPin2 == 1){
      delay(50);
      buttonPoll2 = digitalRead(buttonPin2);
      if (buttonPin2 == 0){
        state2 = old2 + 1;
      }
      else
      delay(100);
    }

  
  int val = analogRead(LDR);
  Serial.println(val);

  
switch (state) {
  case 1:
                        if (val<750){
                        digitalWrite(RRGB,HIGH);
                        digitalWrite(GRGB,HIGH);
                        digitalWrite(BRGB,HIGH);
                        }else{
                        digitalWrite(RRGB,LOW);
                        digitalWrite(GRGB,LOW);
                        digitalWrite(BRGB,LOW);
                        } 
             


    break;
  
    case 2:
  
      delay(100);
    
    break;
  
  default:
              switch (state2) {
              case 1:

                        if (val<750){
                        digitalWrite(RRGB,HIGH);
                        digitalWrite(GRGB,HIGH);
                        digitalWrite(BRGB,HIGH);
                        }else{
                        digitalWrite(RRGB,LOW);
                        digitalWrite(GRGB,LOW);
                        digitalWrite(BRGB,LOW);
                        }

                        // set the brightness of pin 9:
                        analogWrite(RLredLED, RedBrightness);
                        analogWrite(RRredLED, RedBrightness);
                        analogWrite(FLredLED, LOW);
                        analogWrite(FRredLED, LOW);
                        // set the brightness of pin 10:
                        analogWrite(FLgreenLED, GreenBrightness);
                        analogWrite(FRgreenLED, GreenBrightness);

                        // change the brightness for next time through the loop:
                        RedBrightness = RedBrightness + fadeAmount;
                        GreenBrightness = GreenBrightness - fadeAmount;
  
                        // reverse the direction of the fading at the ends of the fade: 
                        // and blink the internal LED on pin 13 at the fade end point
                        if (RedBrightness == 0 || RedBrightness == 255) {
                          fadeAmount = -fadeAmount ; 
                        }     

 
                        // wait for 30 milliseconds to see the dimming effect    

                         delay(6); // physiscal button acts up without this line and wont bridge the connection correctly
                    break;
              default:
                        if (val<750){
                        digitalWrite(RRGB,HIGH);
                        digitalWrite(GRGB,HIGH);
                        digitalWrite(BRGB,HIGH);
                        }else{
                        digitalWrite(RRGB,LOW);
                        digitalWrite(GRGB,LOW);
                        digitalWrite(BRGB,LOW);
                        }             

                        unsigned long currentMillis = millis(); // grab current time

                        analogWrite(FLredLED, brightness);// set the brightness of ledPin:
                        analogWrite(FRredLED, brightness);
                        analogWrite(RLredLED, brightness);
                        analogWrite(RRredLED, brightness);
                        digitalWrite(FLgreenLED,LOW);
                        digitalWrite(FRgreenLED,LOW);


                        if (currentMillis - previousMillis >= IntervalMode1){
                            brightness = brightness + fadeAmount;     // change the brightness for next time through the loop:
                            previousMillis = millis();
                          }
 
                        if (brightness <= 0 )
                          { // reverse the direction of the fading at the ends of the fade:
                            brightness = 0;
                            fadeAmount = -fadeAmount;
                          }
                            if (brightness >=255 )
                          { // reverse the direction of the fading at the ends of the fade:
                            brightness = 255;
                            fadeAmount = -fadeAmount;
                          }                    
                    break;
              }
    break;
}
  
}

Have you tested your buttons using a simpler sketch that only includes the buttons?

aarg:
Have you tested your buttons using a simpler sketch that only includes the buttons?

yeah but they dont work with this specific one

  buttonPoll = digitalRead(buttonPin);
  if (buttonPin == 1)

Think hard about what you are testing

Is it the state of the input or the number of the input ?

UKHeliBob:

  buttonPoll = digitalRead(buttonPin);

if (buttonPin == 1)



Think hard about what you are testing

Is it the state of the input or the number of the input ?

the number i guess but i dont know what else to do to be honest

buttonPoll = digitalRead(buttonPin); //read state of buttonPin, assign value to buttonPoll
    if (buttonPin == 1){  // compare pin number of buttonPin with 1
    ...
buttonPoll2 = digitalRead(buttonPin2);  //read state of buttonPin2, assign value to buttonPoll2
    if (buttonPin2 == 1){  //compare pin number of buttonPin2 with 1

Use of better variable names may help avoid this

evanmars:

buttonPoll = digitalRead(buttonPin); //read state of buttonPin, assign value to buttonPoll

if (buttonPin == 1){  // compare pin number of buttonPin with 1
    …
buttonPoll2 = digitalRead(buttonPin2);  //read state of buttonPin2, assign value to buttonPoll2
    if (buttonPin2 == 1){  //compare pin number of buttonPin2 with 1




Use of better variable names may help avoid this

ive tried changing the variable names but it still doesnt work

PedroSantos99:
ive tried changing the variable names but it still doesnt work

That was advice on best practices. Look again at reply #3 and look again at your variable names. That reply explains your problem completely.

You have tested your buttons with a simpler program and they work

Compare how the button state is tested in the working program with how it is tested in yours

aarg:
That was advice on best practices. Look again at reply #3 and look again at your variable names. That reply explains your problem completely.

Im sorry everyone its probably a really stupid mistake from my part but i im not really that good at this and have no clue what all of you are trying to tell me

"state" and "state2" will be 0 when the sketch starts. Whenever a button is pressed and released again whithin 50ms, the respective state will be set to 1 and it stays there until the device is reset because "old" and "old2" is always 0.

Btw., your button code is not ideal, in fact it is really no good because it blocks.

thank you everyone i will work on my button code and try and figure it out

PedroSantos99:
thank you everyone i will work on my button code and try and figure it out

Use arrays for your buttons and your states, or use a library which could make the button handling much simpler.

Use arrays for your buttons and your states

But not until you have found the mistake with

  buttonPoll = digitalRead(buttonPin);
  if (buttonPin == 1)

UKHeliBob:
But not until you have found the mistake with

  buttonPoll = digitalRead(buttonPin);

if (buttonPin == 1)

I looked at that and thought "No, its OK he has the double = sign.. " Then it suddenly hit me, "Doh!"

Good catch!

-jim lee

thank you to everyone I have finally figure it out i a also working on changing on how my buttons work and making them a little bit simpler thank you again.