-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSevSegThree.cpp
292 lines (274 loc) · 7.09 KB
/
SevSegThree.cpp
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
/*
Modified by Sam Wedge February 2013 samwedge.co.uk
Adapted from original code (SevSeg V2.1) by Dean Reading, 2012 ([email protected])
Modification from the original code is as follows:
- A reduction from 4 digits to 3
- No decimal point is used in this code
- Requires 10 pins to be used instead of 12
- Numerical range is from -99 to 999
*/
#include "SevSegThree.h"
SevSegThree::SevSegThree()
{
//Initial values
number=888;
}
//Begin
/*******************************************************************************************/
//Set pin modes and turns all displays off
void SevSegThree::Begin(boolean mode_in,byte D1, byte D2, byte D3, byte S1, byte S2, byte S3, byte S4, byte S5, byte S6, byte S7){
//Assign input values to variables
//Mode sets what the digit pins must be set at for it to be turned on. 0 for common cathode, 1 for common anode
mode=mode_in;
if (mode==1){
DigitOn=HIGH;
DigitOff=LOW;
SegOn=LOW;
SegOff=HIGH;
}
else {
DigitOn=LOW;
DigitOff=HIGH;
SegOn=HIGH;
SegOff=LOW;
}
DigitPins[0]=D1;
DigitPins[1]=D2;
DigitPins[2]=D3;
SegmentPins[0]=S1;
SegmentPins[1]=S2;
SegmentPins[2]=S3;
SegmentPins[3]=S4;
SegmentPins[4]=S5;
SegmentPins[5]=S6;
SegmentPins[6]=S7;
//Set Pin Modes as outputs
for (byte digit=0;digit<3;digit++) {
pinMode(DigitPins[digit], OUTPUT);
}
for (byte seg=0;seg<7;seg++) {
pinMode(SegmentPins[seg], OUTPUT);
}
//Turn Everything Off
//Set all digit pins off. Low for common anode, high for common cathode
for (byte digit=0;digit<3;digit++) {
digitalWrite(DigitPins[digit], DigitOff);
}
//Set all segment pins off. High for common anode, low for common cathode
for (byte seg=0;seg<7;seg++) {
digitalWrite(SegmentPins[seg], SegOff);
}
}
//Refresh Display
/*******************************************************************************************/
//Cycles through each segment and turns on the correct digits for each.
//Leaves everything off
void SevSegThree::PrintOutput(){
for (byte seg=0;seg<7;seg++) {
//Turn the relevant segment on
digitalWrite(SegmentPins[seg],SegOn);
//For each digit, turn relevant digits on
for (byte digit=0;digit<3;digit++){
if (lights[digit][seg]==1) {
digitalWrite(DigitPins[digit],DigitOn);
}
//delay(200); //Uncomment this to see it in slow motion
}
//Turn all digits off
for (byte digit=0;digit<3;digit++){
digitalWrite(DigitPins[digit],DigitOff);
}
//Turn the relevant segment off
digitalWrite(SegmentPins[seg],SegOff);
}
}
//New Number
/*******************************************************************************************/
//Function to update the number displayed
void SevSegThree::NewNum(int number_in)
{
//Feed the inputs into the library's variables
number=number_in;
FindNums();
CreateArray();
}
//Find Digits (Nums)
/*******************************************************************************************/
//Function to find the three individual digits to be displayed from the variable 'number'
void SevSegThree::FindNums() {
//If the number received is negative, set the flag and make the number positive
if (number<0) {
negative=1;
number=number*-1;
}
else {
negative=0;
}
//If the number is out of range, just display '---'
if ((negative==0 && number>999) || (negative==1 && number>99)) {
nums[0]=21;
nums[1]=21;
nums[2]=21;
}
else{
//Find the three digits
int total=number;
if (negative==0) {
nums[0]=number/100;
total=total-nums[0]*100;
}
else {
nums[0]=21;
}
nums[1]=total/10;
total=total-nums[1]*10;
nums[2]=total;
//If there are zeros, set them to 20 which means a blank
//However, don't cut out significant zeros
if (negative==0) {
if (nums[0]==0){
nums[0]=20;
if (nums[1]==0) {
nums[1]=20;
if (nums[2]==0) {
nums[2]=20;
}
}
}
}
else {
if (nums[1]==0) {
nums[1]=20;
if (nums[2]==0) {
nums[2]=20;//FIXME: Might need to remove this line
}
}
}
}
}
//Create Array
/*******************************************************************************************/
//From the numbers found, says which LEDs need to be turned on
void SevSegThree::CreateArray() {
for (byte digit=0;digit<3;digit++) {
switch (nums[digit]){
case 0: // 0
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=1;
lights[digit][5]=1;
lights[digit][6]=0;
break;
case 1: // 1
lights[digit][0]=0;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=0;
lights[digit][4]=0;
lights[digit][5]=0;
lights[digit][6]=0;
break;
case 2: // 2
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=0;
lights[digit][3]=1;
lights[digit][4]=1;
lights[digit][5]=0;
lights[digit][6]=1;
break;
case 3: // 3
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=0;
lights[digit][5]=0;
lights[digit][6]=1;
break;
case 4: // 4
lights[digit][0]=0;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=0;
lights[digit][4]=0;
lights[digit][5]=1;
lights[digit][6]=1;
break;
case 5: // 5
lights[digit][0]=1;
lights[digit][1]=0;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=0;
lights[digit][5]=1;
lights[digit][6]=1;
break;
case 6: // 6
lights[digit][0]=1;
lights[digit][1]=0;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=1;
lights[digit][5]=1;
lights[digit][6]=1;
break;
case 7: // 7
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=0;
lights[digit][4]=0;
lights[digit][5]=0;
lights[digit][6]=0;
break;
case 8: // 8
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=1;
lights[digit][5]=1;
lights[digit][6]=1;
break;
case 9: // 9
lights[digit][0]=1;
lights[digit][1]=1;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=0;
lights[digit][5]=1;
lights[digit][6]=1;
break;
case 20:// BLANK
lights[digit][0]=0;
lights[digit][1]=0;
lights[digit][2]=0;
lights[digit][3]=0;
lights[digit][4]=0;
lights[digit][5]=0;
lights[digit][6]=0;
break;
case 21: // -
lights[digit][0]=0;
lights[digit][1]=0;
lights[digit][2]=0;
lights[digit][3]=0;
lights[digit][4]=0;
lights[digit][5]=0;
lights[digit][6]=1;
break;
default:
lights[digit][0]=0;
lights[digit][1]=0;
lights[digit][2]=1;
lights[digit][3]=1;
lights[digit][4]=1;
lights[digit][5]=0;
lights[digit][6]=1;
break;
}
}
}