-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ino
240 lines (214 loc) · 4.91 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
/*
Horizontal Thrust Measuring Stage (https://github.com/langonginc/Thrust)
by Jiayi Wu (@langonginc)
- The Aviation Club of the Second High School Attached to Beijing Normal University
Last modified: 2024/04/28
* HX711
Arduino pin 2 -> HX711 SCK (CLK)
3 -> DT (D_OUT)
5V -> VCC
GND -> GND
* SD
5V -> 5V
GND -> GND
CS -> 4
MOSI -> 11
SCK -> 13
MISO -> 12
*/
#include <HX711.h>
// #include <SPI.h>
// #include <SD.h>
#include <Wire.h>
#include <TM1650.h>
#define calibration_factor -11181
#define zero_factor 8421804
#define SAMPLE_RATE 50
#define DOUT 3
#define CLK 2
#define CHIP_SELECT 53
#define LED LED_BUILTIN
HX711 scale;
// File dataFile;
TM1650 display;
enum Status {
Ready,
Activating,
Init,
Working,
Done
} status;
bool LedStatus;
/*
* Error List
* 1: SD card init failed - *Removed
* 2: File cannot be read (e.g. the file is read-only)
*/
void error (int id) {
String str = String("E") + String(id);
if (getK(id) == 1) {
str = str + String(" ");
} else if (getK(id) == 2) {
str = str + String(" ");
}
display.displayString(str.c_str());
}
void setup() {
status = Init;
/* System Setup */
Serial.begin(9600);
Serial.println("Starting...");
pinMode(LED, OUTPUT);
LedStatus = false;
/* Display Setup */
Wire.begin(); //Join the bus as master
display.init();
display.displayOn();
display.setBrightnessGradually(0);
/* HX711 Setup */
scale.begin(DOUT, CLK);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Reset the scale to 0
// scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor
/* SD Setup */
/* SD card was dropped in version 1.3
if (!SD.begin(CHIP_SELECT)) {
Serial.println(F("SD card initialization failure. Exit..."));
error(1);
delay(1000);
exit(1);
return;
}
dataFile = SD.open("data.txt", FILE_WRITE);
if (!dataFile) {
Serial.println(F("Data file cannot be open. Exit..."));
error(2);
delay(1000);
exit(1);
return;
}
*/
Serial.println("Started~");
status = Ready;
display.displayString("8888");
}
inline void HiLed () {
if (!LedStatus) {
digitalWrite(LED, HIGH);
LedStatus = true;
}
}
inline void LoLed () {
if (LedStatus) {
digitalWrite(LED, LOW);
LedStatus = false;
}
}
inline int getK (int val) {
if (val < 0) return 0;
if (0 <= val && val < 10) return 1;
if (10 <= val && val < 100) return 2;
if (100 <= val && val < 1000) return 3;
if (1000 <= val && val < 10000) return 4;
}
inline String getDisplayStr (int value) {
String originNum = String(value);
for (int i = 0; i < 4 - getK(value); i ++) {
originNum = String("0") + originNum;
}
return originNum;
}
// Default as value (g).
// if value > 9999 -> output as XX.XX (kg)
inline void displayValue (int value) {
if (0 <= value && value < 10000) {
display.displayString(getDisplayStr(value).c_str());
} else if (value < 1000000) {
display.displayString(getDisplayStr(value / 10).c_str());
display.setDot(0, true);
} else {
error(3);
}
}
inline void writeData (unsigned long t, double f) {
/* SD card part was dropped in version 1.3
dataFile.print(t);
dataFile.print(", ");
dataFile.print(f);
dataFile.print(", ");
*/
Serial.print(t);
Serial.print(", ");
Serial.print(f);
Serial.print(", ");
}
unsigned long startTime = 0;
unsigned long countTime = 0;
void loop() {
unsigned long t = millis();
double value = scale.get_units() * 100;
int valInt = int(value);
if (false) { // DEBUG -> set it to true !!!
Serial.print(status);
Serial.print(" ");
Serial.println(value);
}
switch (status) {
case Ready:
HiLed();
if (valInt > 10) {
LoLed();
status = Activating;
break;
}
delay(100);
break;
case Activating:
if (valInt <= 0) {
status = Working;
display.displayString("3 ");
HiLed();
delay(1000);
LoLed();
delay(200);
display.displayString("2 ");
HiLed();
delay(1000);
LoLed();
delay(200);
display.displayString("1 ");
HiLed();
delay(1000);
LoLed();
display.displayString(" 0");
} else {
display.displayString("0000");
}
break;
case Working:
if (valInt > 0) {
if (startTime == 0) {
HiLed();
startTime = t;
countTime = t;
}
if (t - countTime >= SAMPLE_RATE) {
countTime = t;
writeData(t - startTime, value);
displayValue(valInt);
}
}
if (valInt < -1000) {
LoLed();
// dataFile.println("");
// dataFile.close();
display.displayString("8888");
Serial.println("<-");
Serial.println("Exit with code 0.");
exit(0);
}
break;
case Done:
break;
}
}