페이지

2026년 6월 15일 월요일

5.3.6. 버튼 여러 개 연결하기




CODE 5.12 | 여러 개의 버튼 상태 확인하기
01int button1Pin = 2; // 첫 번째 버튼을 연결한 디지털 핀
02int button2Pin = 3; // 두 번째 버튼을 연결한 디지털 핀
03int button3Pin = 4; // 세 번째 버튼을 연결한 디지털 핀
04 
05void setup() {
06  Serial.begin(9600); // 시리얼 모니터 통신 시작
07 
08  pinMode(button1Pin, INPUT_PULLUP); // 첫 번째 버튼에 내부 풀업 저항
09  pinMode(button2Pin, INPUT_PULLUP); // 두 번째 버튼에 내부 풀업 저항
10  pinMode(button3Pin, INPUT_PULLUP); // 세 번째 버튼에 내부 풀업 저항
11}
12 
13void loop() {
14  // 첫 번째 버튼이 눌리면 LOW 신호가 입력됨
15  if (digitalRead(button1Pin) == LOW) {
16    Serial.println("BTN 1 PRESSED");
17    delay(200); // 0.2초 대기
18  }
19 
20  // 두 번째 버튼이 눌리면 LOW 신호가 입력됨
21  if (digitalRead(button2Pin) == LOW) {
22    Serial.println("BTN 2 PRESSED");
23    delay(200); // 0.2초 대기
24  }
25 
26  // 세 번째 버튼이 눌리면 LOW 신호가 입력됨
27  if (digitalRead(button3Pin) == LOW) {
28    Serial.println("BTN 3 PRESSED");
29    delay(200); // 0.2초 대기
30  }
31}


 

댓글 없음:

댓글 쓰기