-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseven_segment.h
107 lines (72 loc) · 2.13 KB
/
seven_segment.h
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
/* Seven Segment Display - Four Character Module
All functions for the control of a 4 digit Seven Segment Display Module
*/
#include "TM1637.h"
#define CLK 32//pins definitions for TM1637 and can be changed to other ports
#define DIO 33
TM1637 tm1637(CLK, DIO);
/*
Display Number (4 character 7 Segment Display)
- Takes in any 4 digit number and prints to 4 digit 7 segment display
- Any digit unable to be displayed is shown as "FFFF"
*/
void displayNumber(uint16_t number) {
if (number <= 9999) {
Serial.printf("Number: [%i] \n", number);
byte digits[5]; // Must be length of largest input integer + 1
for (int i = 0; i < 5; i++) {
digits[i] = number % 10;
Serial.printf("output: [%i] \n", digits[i]);
number = number / 10 ;
}
tm1637.display(0, digits[3]);
tm1637.display(1, digits[2]);
tm1637.display(2, digits[1]);
tm1637.display(3, digits[0]);
} else {
tm1637.display(0, 15);
tm1637.display(1, 15);
tm1637.display(2, 15);
tm1637.display(3, 15);
}
}
int animateArray[14] = {9, 8 , 7 , 6 , 5 , 4, 3, 2, 1, 0, 0, 0, 0, 0};
void animationA() {
for (int i = 0; i < 10; i++) {
tm1637.display(0, animateArray[i + 3]);
delay(50);
tm1637.display(1, animateArray[i + 2]);
delay(50);
tm1637.display(2, animateArray[i + 1]);
delay(40);
tm1637.display(3, animateArray[i]);
delay(30);
}
}
void animationB() { // Only really useful for bombcounters
uint16_t number = 9999;
displayNumber(number);
delay(200);
while (number >= 0) {
displayNumber(number);
number--;
}
}
void seven_seg_setup(bool animate = false) {
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
if (animate) {
animationA();
delay(400);
tm1637.clearDisplay();
delay(500);
}
}
uint16_t preNum;
// Loop function runs all the time and only calls the print to display loop if the number has changed
void seven_seg_loop(uint16_t newNumber) {
if (newNumber != preNum) {
displayNumber(newNumber);
preNum = newNumber;
}
}