Advertisement
bitwise_gamgee

Untitled

Jul 7th, 2023
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <DHT.h>
  2.  
  3. // Pins connected to 74HC595
  4. const int latchPin = 8;
  5. const int clockPin = 12;
  6. const int dataPin = 11;
  7.  
  8. // Mapping of digits to segments for a common-anode 7-segment display
  9. const byte digitToSegment[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
  10.  
  11. // DHT setup
  12. #define DHTPIN 2
  13. #define DHTTYPE DHT11
  14. DHT dht(DHTPIN, DHTTYPE);
  15.  
  16. void setup() {
  17.   pinMode(latchPin, OUTPUT);
  18.   pinMode(clockPin, OUTPUT);
  19.   pinMode(dataPin, OUTPUT);
  20.  
  21.   dht.begin();
  22. }
  23.  
  24. void loop() {
  25.   float temp = dht.readTemperature();
  26.  
  27.   int tempInt = (int)temp; // truncate decimal
  28.   int digit1 = tempInt / 10; // tens place
  29.   int digit2 = tempInt % 10; // ones place
  30.  
  31.   digitalWrite(latchPin, LOW);
  32.   shiftOut(dataPin, clockPin, MSBFIRST, digitToSegment[digit1]);
  33.   shiftOut(dataPin, clockPin, MSBFIRST, digitToSegment[digit2]);
  34.   digitalWrite(latchPin, HIGH);
  35.   delay(2000);
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement