Skip to content

Instantly share code, notes, and snippets.

@kmc7468
Last active July 24, 2018 09:22
Show Gist options
  • Save kmc7468/9cb6edc588b451f748dba9f96918309d to your computer and use it in GitHub Desktop.
Save kmc7468/9cb6edc588b451f748dba9f96918309d to your computer and use it in GitHub Desktop.
공기 순환기 모델
/*
* (C) 2018. kmc7468 All rights reserved.
* MIT 라이선스입니다.
* https://gist.github.com/kmc7468/9cb6edc588b451f748dba9f96918309d
*/
#include "DHT.h"
#include <math.h>
//
// 전역 변수
//
bool enable = false; // 공기 순환 기능이 활성화 되어 있는지 여부
int switch_last_state = LOW; // 공기 순환 기능을 꺼고 켜는 스위치의 마지막 입력 값
int switch2_last_state = LOW; // 온도 차 역치 값을 조절하는 스위치의 마지막 입력 값
const int tmp36 = A0; // TMP36(온도감지센서 1)의 번호
int diff = 4; // 모터가 작동할 두 온도감지센서 사이의 차이의 역치 값
DHT dht(3, DHT11); // DHT11(온도감지센서 2)를 다루기 위한 객체
// 시리얼 포트에 데이터를 덤프하지 않으려면 undef 또는 define 전처리문을 주석 처리 하면 됩니다.
//#define DEBUG
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
Serial.println("[INFO] 아두이노가 시작되었습니다.");
#endif
pinMode(2, INPUT); // 공기 순환 기능을 꺼고 켜는 스위치
pinMode(3, INPUT); // 온도감지센서 2 (DHT11)
pinMode(8, OUTPUT); // 공기 순환 기능이 활성화 되어 있는지 여부를 보여주는 LED
pinMode(9, OUTPUT); // 모터를 작동시키는 트랜지스터
pinMode(10, OUTPUT); // 모터가 작동되었는지 보여주는 LED
pinMode(4, OUTPUT); // 온도 차 역치 값이 섭씨 2도임을 보여주는 LED
pinMode(5, OUTPUT); // 위와 같으나, 섭씨 4도
pinMode(6, OUTPUT); // 위와 같으나, 섭씨 6도
pinMode(7, OUTPUT); // 위와 같으나, 섭씨 8도
pinMode(11, INPUT); // 온도 차 역치 값을 조절하는 스위치
digitalWrite(8, LOW);
}
void loop()
{
int switch_state = digitalRead(2);
if (switch_state == HIGH && switch_last_state == LOW)
{
enable = !enable;
}
switch_last_state = switch_state;
digitalWrite(8, (enable ? HIGH : LOW));
int tmp36_val = analogRead(tmp36);
float tmp36_volt = tmp36_val / 1024. * 5.;
float tmp36_tmp = (tmp36_volt - .5) * 100;
#ifdef DEBUG
Serial.print("[DUMP] tmp36_tmp = ");
Serial.println(tmp36_tmp);
#endif
int dht11_tmp = dht.readTemperature();
#ifdef DEBUG
Serial.print("[DUMP] dht11_tmp = ");
Serial.println(dht11_tmp);
#endif
if (enable && fabs(tmp36_tmp - dht11_tmp) >= diff)
{
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
#ifdef DEBUG
Serial.println("[INFO] 모터가 활성화 되었습니다.");
#endif
}
else
{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
#ifdef DEBUG
Serial.println("[INFO] 모터가 비활성화 되었습니다.");
#endif
}
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
if (!enable)
{
return;
}
int switch2_state = digitalRead(11);
if (switch2_state == HIGH && !switch2_last_state)
{
diff += 2;
if (diff > 8)
{
diff = 2;
}
}
switch2_last_state = switch2_state;
switch (diff)
{
case 2:
digitalWrite(4, HIGH);
break;
case 4:
digitalWrite(5, HIGH);
break;
case 6:
digitalWrite(6, HIGH);
break;
case 8:
digitalWrite(7, HIGH);
break;
default:
#ifdef DEBUG
Serial.print("[ERROR] 'diff' 값이 알 수 없는 값입니다: ");
Serial.println(diff);
#endif
break;
}
#ifdef DEBUG
Serial.print("[DUMP] diff = ");
Serial.println(diff);
#endif
}
MIT License
Copyright (c) 2018 kmc7468
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@kmc7468
Copy link
Author

kmc7468 commented Jul 24, 2018

라이브러리 의존성

  • DHT sensor library
  • Adafruit Sensor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment