|
| 1 | +// Graphic thermometer: shows to indicate if temperature |
| 2 | +// goes up of down. It uses DHT22, but probably also |
| 3 | +// works with DHT11. |
| 4 | + |
| 5 | +#include "LedControl.h" //Imports the library |
| 6 | + |
| 7 | +static const int DATA_PIN = 20; |
| 8 | +static const int CLK_PIN = 5; |
| 9 | +static const int CS_PIN = 21; |
| 10 | + |
| 11 | +LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1); |
| 12 | + |
| 13 | +#define brightness 1 //Values from 1 to 15 to set the brightness |
| 14 | + |
| 15 | +#define DHTPIN A3 // A3 is pin 26 (PC3/ADC3/PCINT11) of atmega328 |
| 16 | +#define TIMEOUT 10000 |
| 17 | + |
| 18 | +#define SHIFT_EVERY_MIN 5 |
| 19 | +#define SHIFT_AT_STEPS (24*SHIFT_EVERY_MIN) |
| 20 | + |
| 21 | +uint16_t shiftstep; |
| 22 | +uint8_t last_temp; |
| 23 | +uint16_t base_temp; |
| 24 | + |
| 25 | +void setup() { |
| 26 | + lc.shutdown(0, false); |
| 27 | + lc.setIntensity(0, brightness); |
| 28 | + lc.clearDisplay(0); |
| 29 | + Serial.begin(9600); |
| 30 | + Serial.println("Start"); |
| 31 | + shiftstep = 0; |
| 32 | + base_temp = 0; |
| 33 | +} |
| 34 | + |
| 35 | +uint8_t matrix[8]; |
| 36 | + |
| 37 | +void loop() { |
| 38 | + uint8_t data[5] = { 0, 0, 0, 0, 0 }; |
| 39 | + |
| 40 | + uint8_t mask = 128; |
| 41 | + uint8_t idx = 0; |
| 42 | + |
| 43 | + uint8_t bit = digitalPinToBitMask(DHTPIN); |
| 44 | + uint8_t port = digitalPinToPort(DHTPIN); |
| 45 | + volatile uint8_t *PIR = portInputRegister(port); |
| 46 | + |
| 47 | + // REQUEST SAMPLE |
| 48 | + pinMode(DHTPIN, OUTPUT); |
| 49 | + digitalWrite(DHTPIN, HIGH); // T-be |
| 50 | + delay(250); |
| 51 | + // switch last value on |
| 52 | + if (base_temp != 0) |
| 53 | + { |
| 54 | + matrix[last_temp] |= 1; |
| 55 | + lc.setColumn(0, last_temp, matrix[last_temp]); |
| 56 | + } |
| 57 | + pinMode(DHTPIN, OUTPUT); |
| 58 | + digitalWrite(DHTPIN, LOW); // T-go |
| 59 | + delay(20); |
| 60 | + digitalWrite(DHTPIN, HIGH); |
| 61 | + delayMicroseconds(40); |
| 62 | + pinMode(DHTPIN, INPUT_PULLUP); |
| 63 | + delayMicroseconds(10); |
| 64 | + |
| 65 | + uint32_t t_mid = micros(); |
| 66 | + |
| 67 | + // GET ACKNOWLEDGE or TIMEOUT |
| 68 | + uint16_t loopCntLOW = TIMEOUT; |
| 69 | + while ((*PIR & bit) == LOW ) // T-rel |
| 70 | + { |
| 71 | + if (--loopCntLOW == 0) |
| 72 | + { |
| 73 | + //Serial.println("Low init err"); |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + t_mid = 50; //(10 + micros() - t_mid)*50/80; |
| 79 | + |
| 80 | + uint16_t loopCntHIGH = TIMEOUT; |
| 81 | + while ((*PIR & bit) != LOW ) // T-reh |
| 82 | + { |
| 83 | + if (--loopCntHIGH == 0) |
| 84 | + { |
| 85 | + //Serial.println("Error 2"); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for (uint8_t i = 0; i < 40; i++) |
| 91 | + { |
| 92 | + loopCntLOW = TIMEOUT; |
| 93 | + while ((*PIR & bit) == LOW ) |
| 94 | + { |
| 95 | + if (--loopCntLOW == 0) |
| 96 | + { |
| 97 | + //Serial.print("Error 3, cycle "); Serial.print(i); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + uint32_t t = micros(); |
| 103 | + |
| 104 | + loopCntHIGH = TIMEOUT; |
| 105 | + while ((*PIR & bit) != LOW ) |
| 106 | + { |
| 107 | + if (--loopCntLOW == 0) |
| 108 | + { |
| 109 | + //Serial.print("Error 4, cycle "); Serial.print(i); |
| 110 | + return; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + data[i/8] <<= 1; |
| 115 | + if (micros() - t > t_mid) |
| 116 | + data[i/8] |= 1; |
| 117 | + } |
| 118 | + pinMode(DHTPIN, OUTPUT); |
| 119 | + digitalWrite(DHTPIN, HIGH); |
| 120 | + |
| 121 | +/* |
| 122 | + for (uint8_t i = 0; i < 40; i++) |
| 123 | + { |
| 124 | + if (i % 8 == 0) |
| 125 | + Serial.println(); |
| 126 | + Serial.print(cycles[i]); |
| 127 | + |
| 128 | + if (data[i/8] & (1 << (7-i%8))) |
| 129 | + Serial.print("_ "); |
| 130 | + else |
| 131 | + Serial.print("X "); |
| 132 | + } |
| 133 | + */ |
| 134 | + for (uint8_t i = 0; i < 5; i++) |
| 135 | + { |
| 136 | + Serial.print(data[i]); |
| 137 | + Serial.print(" "); |
| 138 | + } |
| 139 | + Serial.println(); |
| 140 | + if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) { |
| 141 | + Serial.println("Checksum err"); |
| 142 | + return; |
| 143 | + } |
| 144 | + uint16_t temp = ((uint16_t)data[2])*256 + data[3]; |
| 145 | + Serial.print("Temp = "); |
| 146 | + Serial.print(temp); |
| 147 | + Serial.println(); |
| 148 | + |
| 149 | + temp /= 2; |
| 150 | + |
| 151 | + if (base_temp == 0) |
| 152 | + { |
| 153 | + base_temp = temp - 3; |
| 154 | + } |
| 155 | + bool did_move = false; |
| 156 | + |
| 157 | + // move up or down depending on new measurement |
| 158 | + while (temp < base_temp) |
| 159 | + { |
| 160 | + for (uint8_t i = 7; i > 0; i--) |
| 161 | + matrix[i] = matrix[i-1]; |
| 162 | + matrix[0] = 0; |
| 163 | + base_temp--; |
| 164 | + did_move = true; |
| 165 | + } |
| 166 | + while (temp > base_temp + 7) |
| 167 | + { |
| 168 | + for (uint8_t i = 0; i < 7; i++) |
| 169 | + matrix[i] = matrix[i+1]; |
| 170 | + matrix[7] = 0; |
| 171 | + base_temp++; |
| 172 | + did_move = true; |
| 173 | + } |
| 174 | + last_temp = temp - base_temp; |
| 175 | + |
| 176 | + // move left after number of steps, only if did not move up or down |
| 177 | + if (++shiftstep >= SHIFT_AT_STEPS && !did_move) |
| 178 | + { |
| 179 | + for (uint8_t i = 0; i < 8; i++) |
| 180 | + matrix[i] <<= 1; |
| 181 | + shiftstep -= SHIFT_AT_STEPS; |
| 182 | + did_move = true; |
| 183 | + } |
| 184 | + |
| 185 | + matrix[last_temp] |= 1; |
| 186 | + |
| 187 | + // if did not move, move up or down to center graph |
| 188 | + if (!did_move) |
| 189 | + { |
| 190 | + uint8_t top = 0; // empty lines on the top |
| 191 | + for (; top < 7; top++) |
| 192 | + if (matrix[7-top] != 0) |
| 193 | + break; |
| 194 | + uint8_t bottom = 0; // empty lines on the bottom |
| 195 | + for (; bottom < 7; bottom++) |
| 196 | + if (matrix[bottom] != 0) |
| 197 | + break; |
| 198 | + if (bottom >= top + 2) |
| 199 | + { |
| 200 | + for (uint8_t i = 0; i < 7; i++) |
| 201 | + matrix[i] = matrix[i+1]; |
| 202 | + matrix[7] = 0; |
| 203 | + base_temp++; |
| 204 | + last_temp--; |
| 205 | + } |
| 206 | + if (top >= bottom + 2) |
| 207 | + { |
| 208 | + for (uint8_t i = 7; i > 0; i--) |
| 209 | + matrix[i] = matrix[i-1]; |
| 210 | + matrix[0] = 0; |
| 211 | + base_temp--; |
| 212 | + last_temp++; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + // update display |
| 217 | + for (uint8_t i = 0; i < 8; i++) |
| 218 | + lc.setColumn(0, i, matrix[i]); |
| 219 | + |
| 220 | + // blink last temperature for 2 seconds |
| 221 | + for (uint8_t s = 0;;) |
| 222 | + { |
| 223 | + delay(200); |
| 224 | + matrix[last_temp] &= 254; |
| 225 | + lc.setColumn(0, last_temp, matrix[last_temp]); |
| 226 | + if (s++ == 5) |
| 227 | + break; |
| 228 | + delay(200); |
| 229 | + matrix[last_temp] |= 1; |
| 230 | + lc.setColumn(0, last_temp, matrix[last_temp]); |
| 231 | + } |
| 232 | +} |
0 commit comments