filmov
tv
Control LED by two Push buttons

Показать описание
By using the Arduino board and simple circuit that contains two pushbuttons, an LED, and any other basic components you think you need. The LED should turn on when either the first button or the second button is pressed.
code:
// initialize the constants
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 13;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbuttons pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the pushbuttons value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if ((buttonState1 == HIGH) || ( buttonState2 == HIGH) ) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
code:
// initialize the constants
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 13;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbuttons pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the pushbuttons value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if ((buttonState1 == HIGH) || ( buttonState2 == HIGH) ) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}