CODE 7.1 | 기울기 센서 상태 감지하기
| 01 | int tiltPin = 2; // 기울기 센서를 연결한 디지털 핀 |
| 02 | |
| 03 | void setup() { |
| 04 | pinMode(tiltPin, INPUT_PULLUP); // 내부 풀업 저항 사용 |
| 05 | Serial.begin(9600); // 시리얼 모니터 통신 시작 |
| 06 | } |
| 07 | |
| 08 | void loop() { |
| 09 | int tiltState = digitalRead(tiltPin); // 기울기 센서 상태 읽기 |
| 10 | |
| 11 | // 센서가 기울어져 접점이 연결되면 LOW 신호가 입력됨 |
| 12 | if (tiltState == LOW) { |
| 13 | Serial.println("Tilted!"); |
| 14 | } |
| 15 | // 센서가 수평 상태이면 HIGH 신호가 입력됨 |
| 16 | else { |
| 17 | Serial.println("Upright."); |
| 18 | } |
| 19 | |
| 20 | delay(200); // 0.2초 대기 후 다시 측정 |
| 21 | } |
댓글 없음:
댓글 쓰기