forked from bkobkobko/SparkCoreLibraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimentemp.cpp
More file actions
126 lines (106 loc) · 3.07 KB
/
timentemp.cpp
File metadata and controls
126 lines (106 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This #include statement was automatically added by the Spark IDE.
#include "OneWire.h"
// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_GFX.h"
// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_LEDBackpack.h"
// This #include statement was automatically added by the Spark IDE.
#include "glcdfont.h"
// This #include statement was automatically added by the Spark IDE.
#include "SparkTime.h"
// LED Clock with NTP time keeping
UDP UDPClient;
SparkTime rtc;
Adafruit_7segment matrix = Adafruit_7segment();
OneWire one = OneWire(D2);
unsigned long currentTime;
unsigned long lastTime = 0UL;
uint8_t rom[8];
uint8_t resp[9];
int32_t scaledTemp;
void setup() {
rtc.begin(&UDPClient, "north-america.pool.ntp.org");
rtc.setTimeZone(-5); // gmt offset
matrix.begin(0x70);
matrix.print(10000,DEC); // make display show ----
matrix.writeDisplay();
getTemp();
delay(500);
}
void loop() {
currentTime = rtc.now();
if (currentTime != lastTime) {
uint8_t sec = rtc.second(currentTime);
uint8_t modsec = sec%10;
if (modsec>4 && modsec<=6) {
showTemp();
} else {
showTime(currentTime);
}
lastTime = currentTime;
if (sec==25) {
getTemp(); //fetch new temp reading every minute
}
}
}
void showTime(unsigned long currentTime) {
uint8_t min = rtc.minute(currentTime);
uint8_t hour = rtc.hour(currentTime);
uint8_t blink;
if (hour>12) { hour = hour - 12; }
if (hour==0) { hour = 12; }
if (hour>9) {
matrix.writeDigitNum(0, hour/10);
} else {
matrix.writeDigitRaw(0,0);
}
matrix.writeDigitNum(1, hour%10);
matrix.writeDigitNum(3, min/10);
matrix.writeDigitNum(4, min%10);
blink = (currentTime%2)<<1;
if (rtc.hour(currentTime)<12) {
blink |= 0x04;
}
matrix.writeDigitRaw(2, blink);
matrix.writeDisplay();
}
void getTemp() {
// Get the ROM address
one.reset();
one.write(0x33);
one.read_bytes(rom, 8);
// Get the temp
one.reset();
one.write(0x55);
one.write_bytes(rom,8);
one.write(0x44);
delay(10);
one.reset();
one.write(0x55);
one.write_bytes(rom, 8);
one.write(0xBE);
one.read_bytes(resp, 9);
byte MSB = resp[1];
byte LSB = resp[0];
int16_t intTemp = ((MSB << 8) | LSB); //using two's compliment 16-bit
double tempC = ((double)intTemp)/16.0;
double tempF = (( tempC*9.0)/5.0+32.0)*10.0;
scaledTemp = (int32_t)tempF; // 724 means 72.4
}
void showTemp() {
// scaledTemp 746 means 74.6
if (scaledTemp<0) {
matrix.writeDigitRaw(0,0x40); // minus sign
} else {
matrix.writeDigitRaw(0,0);
}
if (scaledTemp<100) {
matrix.writeDigitRaw(1,0);
} else {
matrix.writeDigitNum(1,(scaledTemp/100)%10);
}
matrix.writeDigitRaw(2,0x10); //degree dot
matrix.writeDigitNum(3,(scaledTemp/10)%10);
matrix.writeDigitNum(4,(scaledTemp%10));
matrix.writeDisplay();
}