-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
314 lines (279 loc) · 7.62 KB
/
main.ino
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// with the help from
// http://masteringarduino.blogspot.com/2013/10/fastest-and-smallest-digitalread-and.html
// This speeds up the digitalRead speed by 60 times, and the digitalWrite speed by 28 times.
#define portOfPin(P)\
(((P)>=0&&(P)<8)?&PORTD:(((P)>7&&(P)<14)?&PORTB:&PORTC))
#define ddrOfPin(P)\
(((P)>=0&&(P)<8)?&DDRD:(((P)>7&&(P)<14)?&DDRB:&DDRC))
#define pinOfPin(P)\
(((P)>=0&&(P)<8)?&PIND:(((P)>7&&(P)<14)?&PINB:&PINC))
#define pinIndex(P)((uint8_t)(P>13?P-14:P&7))
#define pinMask(P)((uint8_t)(1<<pinIndex(P)))
#define digitalLow(P) *(portOfPin(P))&=~pinMask(P)
#define digitalHigh(P) *(portOfPin(P))|=pinMask(P)
#define isHigh(P)((*(pinOfPin(P))& pinMask(P))>0)
// pin number constants
const int SER1 = 2;
const int SRCLK1 = 3;
const int RCLK1 = 4;
const int SER2 = 5;
const int SRCLK2 = 6;
const int RCLK2 = 7;
const int SER3 = 8;
const int SRCLK3 = 9;
const int RCLK3 = 10;
const int SER4 = 11;
const int SRCLK4 =12;
const int RCLK4 = 13;
const int TRIGGER0 = A2;
const int TRIGGER1 = A3;
const int TRIGGER2 = A4;
const int TRIGGER3 = A5;
const int BUTTON1 = A0;
const int BUTTON2 = A1;
// The rest time that is used for debugging
const int REST = 0;
// constant byte data that is made easier
// to understand by assigning them to the words
const byte one = 0x60; // 0110 0000
const byte two = 0xDA; // 1101 1010
const byte three = 0xF2; // 1111 0010
const byte four = 0x66; // 0110 0110
const byte five = 0xB6; // 1011 0110
const byte six = 0xBE; // 1011 1110
const byte seven = 0xE0; // 1110 0000
const byte eight = 0xFE; // 1111 1110
const byte nine = 0xF6; // 1111 0110
const byte zero = 0xFC; // 1111 1100
const byte none = 0x00;
const byte C = 0x9C; // 1001 1100
// a global variable that contains three bytes
// for each of the digits.(The last digit is always a 'C')
byte digitList[3];
// sets the pinmodes
void setup() {
pinMode(SER1, OUTPUT);
pinMode(SRCLK1, OUTPUT);
pinMode(RCLK1, OUTPUT);
pinMode(SER2, OUTPUT);
pinMode(SRCLK2, OUTPUT);
pinMode(RCLK2, OUTPUT);
pinMode(SER3, OUTPUT);
pinMode(SRCLK3, OUTPUT);
pinMode(RCLK3, OUTPUT);
pinMode(SER4, OUTPUT);
pinMode(SRCLK4, OUTPUT);
pinMode(RCLK4, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
}
// The recursive loop.
// If the 'past' button is pressed, it will display past temperatures
// If the 'future' button is pressed, it will display future temperatures
// If no button is pressed, it will display current temperatures
void loop() {
if (isHigh(BUTTON1)){
int pastTemp1[8] = {143, 107, 217, 257, 104, 165, 176, 255};
int pastTemp2[8] = {026, 076, 099, 197, 099, 175, 159, 104};
int pastTemp3[8] = {085, 172, 089, 006, 148, 181, 218, 155};
displayAll(pastTemp1, 1);
displayAll(pastTemp2, 2);
displayAll(pastTemp3, 3);
singleDisplay(127);
}
else if (isHigh(BUTTON2)){
int futureTemp1[8] = {156, 119, 226, 268, 121, 176, 187, 267};
int futureTemp2[8] = {049, 091, 113, 213, 111, 190, 169, 121};
int futureTemp3[8] = {100, 182, 099, 021, 159, 187, 232, 168};
displayAll(futureTemp1, 1);
displayAll(futureTemp2, 2);
displayAll(futureTemp3, 3);
singleDisplay(141);
}
else{
int currentTemp1[8] = {186, 153, 253, 292, 160, 205, 210, 302};
int currentTemp2[8] = {096, 125, 145, 245, 139, 220, 193, 157};
int currentTemp3[8] = {140, 208, 132, 067, 188, 213, 261, 191};
displayAll(currentTemp1, 1);
displayAll(currentTemp2, 2);
displayAll(currentTemp3, 3);
singleDisplay(158);
}
}
// A function that recieves a byte and shifts it bit by bit into the shift register
void shift(byte a, int n) {
for (int i = 0; i <= 7; i++){
int binaryValue = bitRead(a, i);
if (n==1){
digitalWrite(SER1, binaryValue);
digitalLow(SRCLK1);
digitalHigh(SRCLK1);
}
else if (n==2){
digitalWrite(SER2, binaryValue);
digitalLow(SRCLK2);
digitalHigh(SRCLK2);
}
else if (n==3){
digitalWrite(SER3, binaryValue);
digitalLow(SRCLK3);
digitalHigh(SRCLK3);
}
else if (n==4){
digitalWrite(SER4, binaryValue);
digitalLow(SRCLK4);
digitalHigh(SRCLK4);
}
}
}
// A trigger function that displays a digit on the seven segment
void fndShow(int n){
if (n==1){
digitalLow(RCLK1);
digitalHigh(RCLK1);
}
else if (n==2){
digitalLow(RCLK2);
digitalHigh(RCLK2);
}
else if (n==3){
digitalLow(RCLK3);
digitalHigh(RCLK3);
}
else if (n==4){
digitalLow(RCLK4);
digitalHigh(RCLK4);
}
}
// A function that recieves an array of integers
// and displays them in order to eight seven segment displays
void displayAll(int inputIntegers[], int n){
byte tempList[8][3];
for (int i=0;i<8;i++){
eachDigit(inputIntegers[i]); // Uses the function 'eachDigit()' to convert
for (int j=0;j<3;j++){ // an integer value into byte data for each digit
tempList[i][j] = digitList[j];
}
}
fndDisplay(tempList, n);
}
// A 2D byte array is given and it processes the seven segment displays
// to light up digit by digit across all the displays
void fndDisplay(byte inputByte[8][3], int n){ //byte arrays
for (int j=0;j<4;j++){
shift(inputByte[j][0],n);
shift(inputByte[j+4][0],n);
shift(68*j,n);
fndShow(n);
NOP(REST);
shift(inputByte[j][1]+1,n);
shift(inputByte[j+4][1]+1,n);
shift(68*j+17,n);
fndShow(n);
NOP(REST);
shift(inputByte[j][2],n);
shift(inputByte[j+4][2],n);
shift(68*j+34,n);
fndShow(n);
NOP(REST);
shift(C,n);
shift(C,n);
shift(68*j+51,n);
fndShow(n);
}
// This part is added in order to fix the error where the last digit
// of the fourth seven segment display appeared brighter than all the others.
shift(none,n);
shift(none,n);
shift(0xFF,n);
fndShow(n);
}
// Used to light up the global temperature display where only one display has to be programmed
void singleDisplay(int inputInteger){
byte tempList[3];
eachDigit(inputInteger);
for (int i=0;i<3;i++){
tempList[i] = digitList[i];
}
shift(tempList[0],4);
digitalHigh(TRIGGER0);
fndShow(4);
digitalLow(TRIGGER0);
NOP(REST);
shift(tempList[1]+1,4);
digitalHigh(TRIGGER1);
fndShow(4);
digitalLow(TRIGGER1);
NOP(REST);
shift(tempList[2],4);
digitalHigh(TRIGGER2);
fndShow(4);
digitalLow(TRIGGER2);
NOP(REST);
shift(C,4);
digitalHigh(TRIGGER3);
fndShow(4);
digitalLow(TRIGGER3);
NOP(REST);
shift(none,4);
digitalHigh(TRIGGER3);
fndShow(4);
digitalLow(TRIGGER3);
NOP(REST);
}
// A function that converts an integer value into a byte value for each digit
void eachDigit(int x){
String s = String(x);
if (x<100){
digitList[0] = none;
digitList[1] = convertDigit(s[0]);
digitList[2] = convertDigit(s[1]);
}
else{
digitList[0] = convertDigit(s[0]);
digitList[1] = convertDigit(s[1]);
digitList[2] = convertDigit(s[2]);
}
}
// seperated from the 'eachDigit()' function to make clean code.
// basically a part of the 'eachDigit()' function
byte convertDigit(char c){
switch (c){
case '1':
return one;
break;
case '2':
return two;
break;
case '3':
return three;
break;
case '4':
return four;
break;
case '5':
return five;
break;
case '6':
return six;
break;
case '7':
return seven;
break;
case '8':
return eight;
break;
case '9':
return nine;
break;
case '0':
return zero;
}
}
// The 'No OPeration' function that is used with the 'REST'
// constant when debugging or delaying the display speed.
void NOP(int val){
for (int i=0;i<val;i++){
__asm__("nop\n\t");
}
}