-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadCell.ino
More file actions
122 lines (100 loc) · 3.43 KB
/
LoadCell.ino
File metadata and controls
122 lines (100 loc) · 3.43 KB
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
/* Weight sensor */
int backInputPin = A0;
int frontInputPin = A1;
int frontSensorOffset = loadByte(EEPROM_FRONT_SENSOR);
int backSensorOffset = loadByte(EEPROM_BACK_SENSOR);
int virtualFrontSensor = 50;
int virtualBackSensor = 50;
/* Weight input smoothing */
const int NUMREADINGS = 10; // More = Smoother, Less = Faster
int index = 0; // Index of current reading
int frontValues[NUMREADINGS]; // Analog front readings
int backValues[NUMREADINGS]; // Analog back readings
int frontTotal; // The total of all front values
int backTotal; // The total of all back values
/* Balance */
const float MAX_BALANCE = 0.85; // Balance reaches maximum at 85%
const float FRONT_REDUCER = 0.22; // Reduce front sensor value with n % of back sensor VARIABLE
void initLoadCell() {
performMeasurement();
for (int i = 0; i < NUMREADINGS; i++) {
frontValues[i] = getFrontSensor();
backValues[i] = getBackSensor();
}
}
void performMeasurement() {
int rawFrontValue = analogRead(frontInputPin);
int rawBackValue = analogRead(backInputPin);
// Subtract old reading then add new reading from sensor:
frontTotal = frontTotal - frontValues[index];
backTotal = backTotal - backValues[index];
frontValues[index] = rawFrontValue - frontSensorOffset;
backValues[index] = rawBackValue - backSensorOffset;
frontTotal = frontTotal + frontValues[index];
backTotal = backTotal + backValues[index];
if (++index >= NUMREADINGS) index = 0; // Restart index
}
int getFrontSensor() {
if (isPrototype2()) {
if (frontTotal / NUMREADINGS > 0) {
return frontTotal / NUMREADINGS;
}
} else {
if ((frontTotal / NUMREADINGS) - (getBackSensor() * FRONT_REDUCER) > 0) {
return (frontTotal / NUMREADINGS) - (getBackSensor() * FRONT_REDUCER);
}
}
return 0;
}
int getBackSensor() {
if (backTotal / NUMREADINGS > 0) {
return backTotal / NUMREADINGS;
}
return 0;
}
float calculateBalance(int front, int back, float deadzone, float offset) {
float balance = front - back; // Positive = Front, Negative = Back, 0 = Balanced
float totalWeight = front + back;
float result = balance / (totalWeight * MAX_BALANCE);
if (result - offset < deadzone && result - offset > -deadzone) {
return 0;
}
if (result - offset < 0) {
result = range(result, offset - deadzone, -1, 0, -1);
} else {
result = range(result, offset + deadzone, 1, 0, 1);
}
return constrain(result, -1, 1);
// Return result: 1 = 100% front, -1 = 100% back, 0 = balanced
if(result < 0) return range(result, -deadzone, -1, 0, -1);
return range(result, deadzone, 1, 0, 1);
}
void runCalibration() {
int maxFront = 0;
int maxBack = 0;
for (int i = 0; i < 100; i++) {
int front = analogRead(frontInputPin);
int back = analogRead(backInputPin);
if (front > maxFront) maxFront = front;
if (back > maxBack) maxBack = back;
delay(1);
}
frontSensorOffset = maxFront;
backSensorOffset = maxBack;
saveByte(EEPROM_FRONT_SENSOR, frontSensorOffset);
saveByte(EEPROM_BACK_SENSOR, backSensorOffset);
Serial.print("calibrate=");
Serial.print(frontSensorOffset);
Serial.print(",");
Serial.println(backSensorOffset);
}
void setVirtualWeight(int front, int back) {
virtualFrontSensor = front;
virtualBackSensor = back;
}
int getVirtualFrontSensor() {
return virtualFrontSensor;
}
int getVirtualBackSensor() {
return virtualBackSensor;
}