buttons for programming wont work

I have been working on this project for 2 weeks and I need some help. I am typing this program for which will read the value of two seperate buttons and a electromagnet, this problem I am facing is that for some odd reason in the serial monitor when I press one button it changes the value for both buttons and not just itself. I am genuinely clueless to why, and i hope this community can help.

int buttonPin1 = 12;
int buttonPin2 = 7;
//int Electromagnet = A4;
int buttonVal1;
int buttonVal2;
int dt = 250;

void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
//pinMode(Electromagnet, OUTPUT);

void loop() {
Serial.print("Button 1 = “);
Serial.print(”’ “);
if(buttonVal1 == HIGH)
//digitalWrite(Electromagnet, LOW);
Serial.print(“mechanism = OFF”);
}if(buttonVal1 == LOW)
//digitalWrite(Electromagnet, HIGH);
Serial.print(“mechanism = ON”);
Serial.print(”, ");
Serial.print("Button 2 = ");



How are the inputs wired ?

Any resistors in place to keep them in a known state at all times or are they floating at an unknown, possibly HIGH, possibly LOW voltage ?

if(buttonVal1 == LOW)You just tested it to see if it was HIGH.
If it’s not HIGH, what else (clue) could it be?

Duplicate topic deleted

I am fairly new to arduino, and i didnt know i needed resistors, do i need resistors? Also thank you for the suggestion about the Highs and Lows, and I converted it to 1 and 0, but the same issue keeps on showing up.

What you need is to be sure that the inputs do not float at an unknown voltage

The easiest way is to use pinMode(pinNum, INPUT_PULLUP) to turn on the internal pullup resistor, wire the input to take it to GND when activated and change the program logic to test for LOW when the input is activated

Sorry but to make sure I understand, I add the "_PULLUP", then I plug in a pull up to my current circuit, then I then put my resistor to ground instead of plugging it to the positive terminal, and it should fix my problem of one button changing both the first and second button value?

Use INPUT_PULLUP in pinMode() Do not use an external resistor Wire the switch to go to GND when closed Test for LOW when you need to know if the switch is closed