/* **********************************************************************
* ATTiny85_7Seg_DHT22
* 7-SegmentDisplay - v1.0
* Uses (2) Common cathode 7-segment display,
* Uses (2) 74HC595 shift register IC
* Uses ATTiny85
*
* Description:
* This sketch illustrates the controlling of two 7-segment displays with
* two 74HC595 shift register. Charlieplexing is not used. Daisychained
* shift registers push 8 bits at a time from one display to the next.
* For a two digit display this is quite easy to code.
* Sketch uses 5236 bytes (63%) of program storage space. Maximum is 8192 bytes.
* Global variables use 80 bytes (15%) of dynamic memory, leaving 432 bytes for local variables. Maximum is 512 bytes.
*
* See spreadsheet file 74HC595_SegmentDisplay.ods
* Define the LED digit patterns, from 0 - 9
* 1 = LED on, 0 = LED off, common cathode, in this order:
* 74HC595 pin Q7,Q6,Q5,Q4,Q3,Q2,Q1,Q0
* Mapping to dp,g,f,e,d,c,b,a of Seven-Segment LED
* ********************************************************* */
#include <TinyDebug.h>
#include "dht.h"
#define DHTPIN PB3
dht DHT;
/********** Hardware Definitions **********/
int latchPin = PB0; // connect to the ST_CP of 74HC595 (pin 12,latch pin)
int dataPin = PB1; // connect to the DS of 74HC595 (pin 14, data pin)
int clockPin = PB2; // connect to the SH_CP of 74HC595 (pin 11, clock pin)
byte digits[10] = { B00111111, // = 0
B00000110, // = 1
B01011011, // = 2
B01001111, // = 3
B01100110, // = 4
B01101101, // = 5
B01111101, // = 6
B00000111, // = 7
B01111111, // = 8
B01101111 // = 9
};
uint8_t blink[2] = {B1100011,
B1000000
};
byte sevenSegDP = B10000000; // = DP
uint8_t temp[2] = {0, 0};
int tempF = 0;
int hum = 0;
long randNumber;
long randBlink;
/*---------- Function Declaration ----------*/
void updateTemperatureDigits();
void writeDigits(byte r1, byte r2);
void writeBlink(byte r1, byte r2);
void writeSegment(byte segment);
void writeBlank();
void setup() {
Debug.begin();
// Set latchPin, clockPin, dataPin as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
randomSeed(analogRead(A2));
}
void loop() {
int check = DHT.read22(DHTPIN); // 0 = no errors
if (check == DHTLIB_OK) {
tempF = (DHT.temperature * 1.8) + 32; // Fahrenheit
hum = DHT.humidity;
updateTemperatureDigits();
writeDigits(temp[0], temp[1]); // write temp to 7-Segments
}
randNumber = random(1000, 5000);
Debug.print("check: "); Debug.println(check);
Debug.print("tempF: "); Debug.println(tempF);
Debug.print("hum: "); Debug.println(hum);
Debug.print(temp[0]); Debug.println(temp[1]);
Debug.println(randNumber);
delay(randNumber);
/* // count to 99
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
// First 8 bytes 'j' goes into register 1.
// Second 8 bytes 'i' goes into register 1 and pushes first 8 bytes into register 2.
writeDigits(i, j);
delay(700);
}
}
*/
if (tempF > 75) {
writeBlink(0, 0);
randBlink = random(500, 2000);
delay(randBlink);
writeBlink(1, 1);
delay(300);
writeBlink(0, 0);
randBlink = random(500, 2000);
delay(randBlink);
writeBlink(1, 1);
delay(300);
writeBlink(0, 0);
randBlink = random(500, 2000);
delay(randBlink);
writeBlink(1, 1);
delay(300);
writeBlink(0, 0);
randBlink = random(500, 2000);
delay(randBlink);
writeBlank();
delay(500);
}
if(tempF < 69) {
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
writeSegment(z);
delay(100);
}
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
writeSegment(z);
delay(100);
}
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
writeSegment(z);
delay(100);
}
for (int j = 5; j >= 0; j--) {
byte x = 0b00000000 + (1 << j);
writeSegment(x);
delay(100);
}
for (int j = 5; j >= 0; j--) {
byte x = 0b00000000 + (1 << j);
writeSegment(x);
delay(100);
}
for (int j = 5; j >= 0; j--) {
byte x = 0b00000000 + (1 << j);
writeSegment(x);
delay(100);
}
}
// Sensor readings take up to 2 seconds (its a very slow sensor)
delay(2000); // Wait a few seconds between measurements.
} // end of void loop
/*---------- Functions ----------*/
void updateTemperatureDigits() {
temp[0] = ((int)tempF % 100) / 10;
temp[1] = ((int)tempF % 10);
//temp[2] = (int)(tempF * 10) % 10;
//temp[3] = (int)(tempF * 100) % 10;
}
// display a alpha, binary value, or number on the digital segment display
void writeDigits(byte r1, byte r2) {
// r1, r2 = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, digits[r2]);
shiftOut(dataPin, clockPin, MSBFIRST, digits[r1]);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void writeBlink(byte r1, byte r2) {
// r1, r2 = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, blink[r2]);
shiftOut(dataPin, clockPin, MSBFIRST, blink[r1]);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void writeSegment(byte segment) {
// digit = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
// shiftOut(dataPin, clockPin, MSBFIRST, blink[r2]);
shiftOut(dataPin, clockPin, MSBFIRST, segment);
shiftOut(dataPin, clockPin, MSBFIRST, segment);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void writeBlank() {
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}