filmov
tv
Arduino UNO with 2 extra button and 1 LED
Показать описание
The code turns the LED ON when I press any of the buttons when I take out my finger from the button the LED stops flashing.
Source Code
```
const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin
int button1State, button2State; // variables to hold the pushbutton states
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// if button1 or button 2 are pressed (but not both)
if (((button1State == LOW) && (button2State == HIGH)) || ((button1State == HIGH) && (button2State == LOW)))
{
digitalWrite(ledPin, HIGH); // turn the LED on
}
else
{
digitalWrite(ledPin, LOW); // turn the LED off
}
}
```
Source Code
```
const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin
int button1State, button2State; // variables to hold the pushbutton states
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// if button1 or button 2 are pressed (but not both)
if (((button1State == LOW) && (button2State == HIGH)) || ((button1State == HIGH) && (button2State == LOW)))
{
digitalWrite(ledPin, HIGH); // turn the LED on
}
else
{
digitalWrite(ledPin, LOW); // turn the LED off
}
}
```