Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimerOne.h>
- #include <DHT_U.h>
- #include <DHT.h>
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
- DHT dht(2, DHT11);
- bool fahrenheit = true;
- long duration = 2L * 1000L; // millis
- float t = 0.0;
- float h = 0.0;
- void initLCD() {
- lcd.clear();
- lcd.setCursor(0, 0);
- if (fahrenheit)
- lcd.print("Temp 0 *F");
- else
- lcd.print("Temp 0 *C");
- lcd.setCursor(0, 1);
- lcd.print("Humidity 0 %");
- }
- void updateTemp() {
- float newT = dht.readTemperature(false);
- if(!isnan(newT))
- t = newT;
- float newH = dht.readHumidity();
- if(!isnan(newH))
- h = newH;
- }
- void toggleUnits() {
- fahrenheit = !fahrenheit;
- initLCD();
- refreshDisplay();
- }
- void refreshDisplay() {
- printData(fahrenheit ? dht.convertCtoF(t) : t, 0, 11);
- printData(h, 1, 11);
- }
- void setup() {
- lcd.begin(16, 2);
- initLCD();
- dht.begin();
- attachInterrupt(digitalPinToInterrupt(3), toggleUnits, RISING);
- Timer1.initialize(duration * 1000L); // micros
- Timer1.attachInterrupt(refreshDisplay);
- }
- void printData(float d, int r, int o) {
- if(d > 100.0)
- o--;
- lcd.setCursor(o, r);
- lcd.print((int)d);
- }
- void loop() {
- delay(duration);
- updateTemp();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement