Day2 02_button
2024. 1. 18. 19:42ㆍarduino
- 버튼을 연결해 led 제어(풀업저항 때문에 평소엔 1 누르면 0)
const int buttonPin = 2;
const int ledPin = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int Value = digitalRead(buttonPin);
Serial.println(Value);
if (Value == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
- 똑같은 코드이지만 while문을 걸어 안정적이게 led 상태가 바뀜
const int buttonPin = 2;
const int ledPin = 10;
int ledState = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int Value = digitalRead(buttonPin);
Serial.println(Value);
if (Value == LOW) {
ledState = !ledState;
while (!digitalRead(buttonPin));
}
digitalWrite(ledPin, ledState);
}
'arduino' 카테고리의 다른 글
Day2 04_sonic (0) | 2024.01.18 |
---|---|
Day2 03_sensor_led (0) | 2024.01.18 |
Day2 01_python_rgb (0) | 2024.01.18 |
Day1 08_rgb_SerialInput (0) | 2024.01.17 |
Day1 07_analogWrite (0) | 2024.01.17 |