I am triying to push a button for each led but for some reason the code doesen't work, any help would really be appreciated, this is the code I wrote so far:
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 13;
const int ledPin2 = 12;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH) {
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}else{
digitalWrite(ledPin,LOW);
}
if (buttonState2 == HIGH) {
digitalWrite(ledPin2, LOW);
delay(2000);
digitalWrite(ledPin2,LOW);
}else{
digitalWrite(ledPin2,LOW);
}
}
tr3.0.ino (716 Bytes)
Uh, maybe the magic is in the wiring that we cannot see.
Hi there!
Could you please post a schematic of the wiring you are using for the buttons?
Delta_G thanks for your feedback but now it just stays lit all the time
this is the simulator I used:
232
June 6, 2018, 2:14pm
6
vaj4088:
Uh, maybe the magic is in the wiring that we cannot see.
You are correct, we need wiring diagram of build-in LED on pin 13.
In the meantime - emulate ONE button function - thus eliminating possible hardware problems
void loop(){
// emulate button
buttonState1 = ~buttonState1; // flip state
if(buttonState1)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
delay(2000); // LED on /off time
}
When you get that going , add second "button" and your LED.
After that replace the emulated values with reading the buttons.
One easy step at a time.
Best of luck to you.