CODE 6.8 | 토양 습도 센서로 스마트 화분 만들기
| 01 | int soilPin = A0; // 토양 습도 센서를 연결한 아날로그 핀 |
| 02 | int redPin = 11; // 빨간색 LED를 연결한 디지털 핀 |
| 03 | int bluePin = 10; // 파란색 LED를 연결한 디지털 핀 |
| 04 | int greenPin = 9; // 초록색 LED를 연결한 디지털 핀 |
| 05 | |
| 06 | void setup() { |
| 07 | pinMode(redPin, OUTPUT); // 빨간색 LED 핀을 출력 모드로 설정 |
| 08 | pinMode(bluePin, OUTPUT); // 파란색 LED 핀을 출력 모드로 설정 |
| 09 | pinMode(greenPin, OUTPUT); // 초록색 LED 핀을 출력 모드로 설정 |
| 10 | Serial.begin(9600); // 시리얼 모니터 통신 시작 |
| 11 | } |
| 12 | |
| 13 | void loop() { |
| 14 | int sensorValue = analogRead(soilPin); // 토양 습도 센서 값 읽기 |
| 15 | |
| 16 | // 센서 값(1023~0)을 수분량(0~100%)으로 변환 |
| 17 | int moisturePercent = map(sensorValue, 1023, 0, 0, 100); |
| 18 | |
| 19 | Serial.print("Moisture: "); |
| 20 | Serial.print(moisturePercent); |
| 21 | Serial.println("%"); |
| 22 | |
| 23 | // 수분량이 30% 미만이면 건조 상태로 판단 |
| 24 | if (moisturePercent < 30) { |
| 25 | digitalWrite(redPin, HIGH); // 빨간색 LED 켜기 |
| 26 | digitalWrite(greenPin, LOW); // 초록색 LED 끄기 |
| 27 | digitalWrite(bluePin, LOW); // 파란색 LED 끄기 |
| 28 | } |
| 29 | // 수분량이 30~70%이면 적정 상태로 판단 |
| 30 | else if (moisturePercent >= 30 && moisturePercent <= 70) { |
| 31 | digitalWrite(redPin, LOW); // 빨간색 LED 끄기 |
| 32 | digitalWrite(greenPin, HIGH); // 초록색 LED 켜기 |
| 33 | digitalWrite(bluePin, LOW); // 파란색 LED 끄기 |
| 34 | } |
| 35 | // 수분량이 70%를 초과하면 과습 상태로 판단 |
| 36 | else { |
| 37 | digitalWrite(redPin, LOW); // 빨간색 LED 끄기 |
| 38 | digitalWrite(greenPin, LOW); // 초록색 LED 끄기 |
| 39 | digitalWrite(bluePin, HIGH); // 파란색 LED 켜기 |
| 40 | } |
| 41 | |
| 42 | delay(1000); // 1초 대기 후 다시 측정 |
| 43 | } |
댓글 없음:
댓글 쓰기