2 led's and 2 switches

hello forum ! i seem to have run into an issue here. i just started learning arduino and i have run into a problem. This is an iteration of the button example in arduino. (i'm using a node mcu)

int led1pin = 16; 
int led2pin = 5; 
int button1 = 4; 
int button2 = 0; 

int button1State=0;
int button2State=0;

void setup() {
 pinMode(led1pin, OUTPUT);
 pinMode(led2pin, OUTPUT);
 pinMode(button1, INPUT);
 pinMode(button2, INPUT);
}

void loop() {
 button1State=digitalRead(button1); // put your main code here, to run repeatedly:
 if (button1State == 1)
 {
 delay(200);
 digitalWrite(led1pin, HIGH);
 delay(200);
 digitalWrite(led1pin, LOW);
 }
 if (button1State==0)
 {
 digitalWrite(led1pin, LOW); 
 delay(200);
 }
 button2State=digitalRead(button2); // put your main code here, to run repeatedly:
 if (button2State == 1)
 {
 delay(200);
 digitalWrite(led2pin, HIGH);
 delay(200);
 digitalWrite(led2pin, LOW);
 }
 if (button2State==0)
 {
 digitalWrite(led2pin, LOW); 
 delay(200);
 }
}

when i used the same code with one button i have no problems. i push button (toggle switch)
and the led blinks. but when i modify the code for 2 switches and 2 led's things go sideways.
the code compiles but the other led keeps blinking without any input from the button

*updated schematic in thread

Your post was MOVED to its current location as it is more suitable.

Really? I don't think the your schematic switch is connected to a meaningful pin. :roll_eyes:

i'm really sorry, did'nt check the schematic ill update it in a jiffy

image

I'm sure other posters will point out to use the internal pull-up but...
For the time being, one of the switches isn't connected to the GND and isn't working.

EDIT:
The schematic has been edited, not reposted.
But it's still wrong.

I'm surprised you say it worked as expected even with one button: with those pullups, the button will read as 1 normally, so I'd expect either led to blink while its button is not pressed, and go out when it's pressed. But you said it blinked when pressed?

i will use a pullup just used a 10k to avoid confusion in code.

Doesn’t seem to correspond to your schematics

If you make a mistake, please repost without editing the post, as my post will be messed up.

this is the code i used for 1

int ledpin = 5; // D1(gpio5)
int button = 4; //D2(gpio4)
int buttonState=0;
void setup() {
 pinMode(ledpin, OUTPUT);
 pinMode(button, INPUT);
}
void loop() {
 buttonState=digitalRead(button); // put your main code here, to run repeatedly:
 if (buttonState == 1)
 {
 digitalWrite(ledpin, HIGH); 
 delay(200);
 }
 if (buttonState==0)
 {
 digitalWrite(ledpin, LOW); 
 delay(200);
 }
}

image
This is still wrong.

im sorry im really bad at schemtaics

This is a external pull-down connection, which is completely different from the schematic above post... :expressionless:

EDIT:
The recently post has been deleted again.
sry I'm out, because he's creating confusion.

Using the internal pullups makes the circuit so much easier and why would there be any confusion in the code ?

on the wiring diagram resistor is pulldown on schematic it is pull up, so much contradiction

im learning- the part where i am in the turotials does'nt introduce me to pullup's just yet

im sorry im really nervous

Again, #10

It's not strange for beginners to post weird schematics.
If this is your first time, it's natural. so there is nothing embarrassing.
But removing or modified it is not good.
This is because people who read this thread later will not understand what happened.
I think this is a manner of using the forum.