CODE 3.12 | 스마트 온도 알리미 코드
| 01 | int buzzerPin = 11; |
| 02 | int sensorPin = A0; |
| 03 | |
| 04 | void setup() { |
| 05 | Serial.begin(9600); |
| 06 | } |
| 07 | void loop() { |
| 08 | // 1. 온도 측정 및 변환 (이전 시간 복습) |
| 09 | int sensorValue = analogRead(sensorPin); |
| 10 | float voltage = sensorValue * (5.0 / 1023.0); |
| 11 | float temperature = (voltage - 0.5) * 100.0; |
| 12 | |
| 13 | Serial.println(temperature); // 현재 온도 출력 |
| 14 | |
| 15 | // 2. 영하인지 감시하기 |
| 16 | if (temperature < 0) { |
| 17 | // 0도 미만이면 사이렌 소리 발사! (위잉~ 위잉~) |
| 18 | |
| 19 | // 소리가 점점 높아짐 (500Hz -> 1000Hz) |
| 20 | for (int i = 500; i <= 1000; i += 5) { |
| 21 | tone(buzzerPin, i); |
| 22 | delay(5); |
| 23 | } |
| 24 | // 소리가 점점 낮아짐 (1000Hz -> 500Hz) |
| 25 | for (int i = 1000; i >= 500; i -= 5) { |
| 26 | tone(buzzerPin, i); |
| 27 | delay(5); |
| 28 | } |
| 29 | } |
| 30 | else { |
| 31 | // 0도 이상이면 조용히 하기 |
| 32 | noTone(buzzerPin); |
| 33 | } |
| 34 | } |
댓글 없음:
댓글 쓰기