-
Notifications
You must be signed in to change notification settings - Fork 4
/
tm1638-gauge.ino
155 lines (133 loc) · 4.48 KB
/
tm1638-gauge.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
// Original version by TronicGr (Thanos) 4-26-2012 for X-sim3
#include <TM1638.h> //can be downloaded from http://code.google.com/p/tm1638-library/
// define a module on data pin 5, clock pin 4 and strobe pin 3
TM1638 module(5, 4, 3);
void setup() {
//Create Serial Object
Serial.begin(115200);
// initialize the screen:
module.clearDisplay(); //clears the display from garbage if any
String name = "DiRT"; //sets a custom logo start up banner
module.setDisplayToString(name); //prints the banner
delay(1500); //small delay 1.5 sec
module.clearDisplay(); //clears the display
}
word rpm_led_set [10] = {
0b00000000 | 0b00000000<< 8,
0b00000001 | 0b00000000<< 8,
0b00000011 | 0b00000000<< 8,
0b00000111 | 0b00000000<< 8,
0b00001111 | 0b00000000<< 8,
0b00011111 | 0b00000000<< 8,
0b00111111 | 0b00000000<< 8,
0b00111111 | 0b01000000<< 8,
0b00111111 | 0b11000000<< 8,
0b00000000 | 0b11111111<< 8,
};
float led_ranges[10] = {
0.0,
0.15,
0.31, //16
0.45, //14
0.57, //12
0.69, //10
0.78, //09
0.85, //07
0.9, //05
0.95, //05
};
//blink variables
byte blink_interval = 50;
boolean blink_on = false;
unsigned long current_millis = 0;
unsigned long previous_millis = 0;
unsigned long update_millis = 0;
void loop() {
int i;
char bufferArray[20]; // holds all serial data into a array
unsigned int rpm; //holds the rpm data (0-65535 size)
unsigned int rpmleds = 0; //holds the 8 leds values
unsigned int rpmmax;
float rpm_percent = 0;
signed int carspeed; //holds the speed data (0-65535 size)
byte gear; // holds gear value data
byte d1; // high byte temp variable
byte d2; // low byte temp variable
byte rpmdata = 0; // marker that new data are available
byte speeddata = 0; // marker that new data are available
byte geardata = 0; // marker that new data are available
long time = millis();
if (Serial.available() >= 10) { //if 10 bytes available in the Serial buffer...
for (i=0; i<10; i++) { // for each byte
bufferArray[i] = Serial.read(); // put into array
}
update_millis = time;
} else {
if (time - update_millis > 10000) {
module.clearDisplay();
module.setLEDs(rpm_led_set[0]);
}
return;
}
if (bufferArray[0] == 'R' ) { // if new bytes have been recieved
d1 = bufferArray[1]; // store high byte of rpm
d2 = bufferArray[2]; // store low byte of rpm
rpm = ((d1<<8) + d2); // concatenate bytes (shift 8 bits)
d1 = bufferArray[3]; // store high byte of rpm
d2 = bufferArray[4]; // store low byte of rpm
rpmmax = ((d1<<8) + d2);
if (rpm && rpmmax && rpmmax > 0) {
rpmdata=1; // we got new data!
}
}
if (bufferArray[5] == 'S' ) {
d1 = bufferArray[6]; // store high byte of speed
d2 = bufferArray[7]; //arduino string store low byte of speed
carspeed = ((d1<<8) + d2); // concatenate bytes (shift 8 bits)
speeddata=1;
}
if (bufferArray[8] == 'G' ) {
gear = bufferArray[9]; // retrieves the single byte of gear
geardata=1;
}
if (speeddata == 1) {
char speed[20];
sprintf(speed, "%3d", carspeed);
module.setDisplayToString(speed);
speeddata=0;
}
if (geardata == 1) {
char* neutral = "n"; // sets the character for neutral
char* reverse = "r"; // sets the character for reverse
if (gear >= 1 and gear <10 ){
module.setDisplayDigit(gear, 7, false);
} else if (gear == 0){
module.setDisplayToString(neutral, 0, 7);
} else if (gear == 10){
module.setDisplayToString(reverse, 0, 7);
}
geardata=0;
}
if (rpmdata == 1) {
rpm_percent = rpm / (float)rpmmax;
for(unsigned int a = 9; a >= 0; a--) {
if (led_ranges[a] <= rpm_percent) {
rpmleds = a;
break;
}
}
if (rpmleds == 9){
current_millis = time;
if (current_millis - previous_millis > blink_interval)
{
previous_millis = current_millis;
blink_on = !blink_on;
}
if (!blink_on) {
rpmleds = 0;
}
}
module.setLEDs(rpm_led_set[rpmleds]);
rpmdata=0;
}
}