Digital Input not reading when used in If condition

Hello. I'm working on a project to build a simple temperature controller. I'm working on button inputs to develop how I can use a set of buttons to navigate a simple menu and change values. I'm using internal pull up resistors in this test set up for wiring simplicity. The program is reading 4 buttons which works fine. I added an if statement to increment a menu position if the first button is pressed. When I run the code with the if statement, button 1 only reads low and no longer works. I'm not sure what I'm missing or overlooking. I've been searching for an answer but haven't found one yet. Thank you for any advice you can offer.

double temp1SP = 68.5;
double temp1Hys = 1.0;
int menuPos = 0;
int button1LastVal = 1;
int button2LastVal = 1;
int button3LastVal = 1;
int button4LastVal = 1;

void setup() {
Serial.begin(9600);  // Start serial connection

// Configure input button pins
pinMode(22, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(28, INPUT_PULLUP);
}

void loop() {

// Read button values into variables
int button1Val = digitalRead(22);
int button2Val = digitalRead(24);
int button3Val = digitalRead(26);
int button4Val = digitalRead(28);

if (button1Val = 0 && button1Val != button1LastVal) { menuPos = menuPos + 1; }

// Print the button values
Serial.print("Button1: ");
Serial.print(button1Val);
Serial.print(" Button2: ");
Serial.print(button2Val);
Serial.print(" Button3: ");
Serial.print(button3Val);
Serial.print(" Button4: ");
Serial.print(button4Val);
Serial.print(" MenuPos: ");
Serial.println(menuPos);

button1LastVal = button1Val;
button2LastVal = button2Val;
button3LastVal = button3Val;
button4LastVal = button4Val;

delay(1000);
}

if (button1 == 0 ...

Thank you so much! I've been staring at that and just not seeing it.

1 Like

Go to the IDE preferences and crank up all verbosity and warnings.

This will let the compiler spill some red ink to ask if you really meant to use assignment = instead of test for equality ==, and quite a number of other things that can, yes, be very hard to see no matter how long you've been at it.

a7

1 Like

Thank you very much! That’s helpful.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.