I am just getting started with Arduino's. So, please excuse me if I have missed something blatantly obvious. So, I am trying to make my own sort of combination lock using four tactile switches. Write now I am just laying it on a breadboard. I am using an Uno clone and have also tried using a Mega, but I have the same problem both times. I created an integer and set it equal to 1. When a button is pressed, the integer is changed in different ways depending on which button is pressed, and the integer is displayed on the serial monitor. I plan to do a lot more in the future, but I don't want to continue with servos and lock mechanisms when this isn't working now. When I start the program, the program loops through changing the integer and displaying information on the serial monitor when I haven't pressed any buttons. The buttons are not faulty, and I have tested the connections. I have detached everything from the Uno and Mega. There are no wires connected to the boards, but the program keeps on looping as if there are no if statements at all. The code is below.
int num = 1;
int button2 = 0;
int button3 = 0;
int button4 = 0;
int button5 = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}
void loop() {
button2 = digitalRead(2);
button3 = digitalRead(3);
button4 = digitalRead(4);
button5 = digitalRead(5);
if (button2 == HIGH) {
num = num * 7;
delay(250);
Serial.println(num);
}
if (button3 == HIGH) {
num = num * 9;
delay(250);
Serial.println(num);
}
if (button4 == HIGH) {
num = num / 2;
delay(250);
Serial.println(num);
}
if (button5 == HIGH) {
num = num - 1000;
delay(250);
Serial.println(num);
}
}