Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Temperature and Humidity Sensor
- #include "DHT.h"
- #include <Arduino.h>
- #include <U8x8lib.h>
- #define DHTPIN 3 // what pin we're connected to
- #define DHTTYPE DHT11 // DHT 11
- #define CELSIUS 0
- #define FAHRENHEIT 1
- #define BUTTON_INTERVAL 100
- const int buttonPin = 2;
- int buttonState = 0;
- int tempUnit = CELSIUS;
- DHT dht(DHTPIN, DHTTYPE);
- U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
- void setup(void) {
- Serial.begin(9600);
- Serial.println("DHTxx test!");
- dht.begin();
- u8x8.begin();
- u8x8.setPowerSave(0);
- u8x8.setFlipMode(1);
- attachInterrupt(digitalPinToInterrupt(buttonPin), changeUnit, RISING);
- }
- void print_temp(void) {
- float temp = dht.readTemperature();
- u8x8.setCursor(0, 33);
- u8x8.print("Temp:");
- if (tempUnit == CELSIUS)
- {
- u8x8.print(temp);
- u8x8.print("C");
- }
- else
- {
- u8x8.print(temp * 1.8 + 32);
- u8x8.print("F");
- }
- }
- void changeUnit(void) {
- static unsigned long last_press = 0;
- static unsigned long timer;
- timer = millis();
- if (timer - last_press > BUTTON_INTERVAL)
- {
- if (tempUnit == CELSIUS)
- tempUnit = FAHRENHEIT;
- else
- tempUnit = CELSIUS;
- last_press = millis();
- }
- }
- void loop(void) {
- buttonState = digitalRead(buttonPin);
- float humi;
- humi = dht.readHumidity();
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- print_temp();
- u8x8.setCursor(0,50);
- u8x8.print("Humidity:");
- u8x8.print(humi);
- u8x8.print("%");
- u8x8.refreshDisplay();
- delay(200);
- }
Add Comment
Please, Sign In to add comment