2020년 7월 30일 목요일

dht 7segment temp



#include "DHT.h"

#define DHTPIN 13     // Digital pin connected to the DHT sensor

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);


//a,b,c,d,e,f,g 상태값
byte segValue[10][7] = { //애노드형
  {0, 0, 0, 0, 0, 0, 1}, //0
  {1, 0, 0, 1, 1, 1, 1}, //1
  {0, 0, 1, 0, 0, 1, 0}, //2
  {0, 0, 0, 0, 1, 1, 0}, //3
  {1, 0, 0, 1, 1, 0, 0}, //4
  {0, 1, 0, 0, 1, 0, 0}, //5
  {0, 1, 0, 0, 0, 0, 0}, //6
  {0, 0, 0, 1, 1, 1, 1}, //7
  {0, 0, 0, 0, 0, 0, 0}, //8
  {0, 0, 0, 0, 1, 0, 0} //9
};

byte segPin[8] = {4, 5, 6, 7, 8, 9, 10, 11}; //사용핀{a,b,c,d,e,f,g,dp} 순서대로임
byte digitPin[2] = {2, 3}; //segment 위치 핀

unsigned long readTime = 0; //현재시간
int d1 = 0; //1의 자리
int d2 = 0; //10의 자리
int change = 0;  //온도와 습도 교차 출력

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 2000;

void setup() {
  //Serial.begin(9600);
  //Serial.println(F("DHTxx test!"));

  dht.begin();

  for (int i = 0; i < 10; i++) {
    pinMode(segPin[i], OUTPUT);
  }
  pinMode(digitPin[0], OUTPUT);
  pinMode(digitPin[1], OUTPUT);
  digitalWrite(digitPin[0], LOW);
  digitalWrite(digitPin[1], LOW);
  pinMode(12, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    if (change == 0)
    {
      change = 1;
    }
    else if (change == 1)
    {
      change = 0;
    }
  }

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  //float h = dht.readHumidity();
  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  //float t = dht.readTemperature();
  int t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  if (change == 0)
  {
    d1 = h % 10; //1의 자리
    d2 = (h / 10) % 10; //10의 자리
    digitalWrite(12, HIGH);
  }
  if (change == 1)
  {
    d1 = t % 10; //1의 자리
    d2 = (t / 10) % 10; //10의 자리
    digitalWrite(12, LOW);
  }
  segOutput(1, d1, 1); //1의 자리
  segOutput(0, d2, 1); //10의 자리
}

//LED 초기화
void segClear() {
  for (int i = 0; i < 8; i++) {
    digitalWrite(segPin[i], HIGH);
  }
}

//LED 출력
void segOutput(int d, int Number, int dp) {
  segClear();
  digitalWrite(digitPin[d], HIGH);
  for (int i = 0; i < 7; i++) {
    digitalWrite(segPin[i], segValue[Number][i]);
  }
  digitalWrite(segPin[7], dp);
  delayMicroseconds(1000);
  digitalWrite(digitPin[d], LOW);
}