페이지

2026년 4월 22일 수요일

3.6.3. 스마트 온도 알리미 만들기






CODE 3.12 | 스마트 온도 알리미 코드
01int buzzerPin = 11;
02int sensorPin = A0;
03 
04void setup() {
05  Serial.begin(9600);
06}
07void 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}

 

댓글 없음:

댓글 쓰기