forked from Airtripper/load-cell-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_cell_test.ino
139 lines (112 loc) · 6.02 KB
/
load_cell_test.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
/*
file: load_cell_test.ino
The MIT License (MIT)
Copyright (c) 2013 Mark Heywood
Load Cell Calibration & Processing output
written by Mark Heywood
Website: http://airtripper.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
_________________________________
ABOUT
This software takes an analog reading from a load cell circuit attached to the arduino.
Each new analog reading is put into a buffer of many readings where the newest
analog reading replaces the oldest analog reading. All the readings in the buffer
are added together and then divided by the number of readings held in the buffer to
yield an average analog value. The buffer is used as a method of smoothing the analog
readings taken from the arduino analog pin.
The software has two modes of operation where one operation is used for calibration and
testing while the other is used for outputting to a graphing application such as
Processing.
*/
// Set the software mode and set the analog pin
int calibrate = 1; // 0 = Output to Processing Application, 1 = Calibration mode
int analogPin = 0; // Arduino analog pin to read
// LOAD CELL CALIBRATION
// Measure test loads on good scales first, enter the values for loadLow &
// loadHigh, put test loads on hacked scale & enter analogue values for analogLow & analogHigh.
// loadLow can be default 0 (no test load), just enter analogue reading for analogLow.
// Low end of the test load values
static long loadLow = 0; // measured low end load in grammes from good scales
static int analogLow = 64; // analog reading from load cell for low end test load
// High end of the test load values
static long loadHigh = 5000; // measured high end load in grammes from good scales
static int analogHigh = 1022; // analog reading from load cell for high end test load
// This is used when you change the load cell platform to something else that weighs
// different and the load is no longer on zero. Add an offset to set to zero.
int loadAdjustment = 0; // Adjust non loaded load cell to 0
// Values for the analog sample buffer for running average to smooth analogue reads
static int samplesBuffer = 100; // Should be the same as the number of samples
int analogSamples[100] = {0}; // Number of analog samples to average and set to 0
int lastSampleIndex = 0; // Last analog sample
long sampleBufferTotal = 0; // The sum of all the samples held in the buffer
long analogSamplesAverage = 0; // Running average of the sum of the buffer
// Results plot or display frequency
long time = 0; // Set time to mark start of delay
int plotDelay = 10; // Time gap between plots for display or graph
void setup() {
if (calibrate) {
Serial.begin(9600); // Set a slower boadrate for calibration
plotDelay = 1000; // Slow the readings output to display for calibration
} else {
Serial.begin(115200); // Set a faster baudrate for Processing Application
}
}
void loop() {
int analogValue = analogRead(analogPin); // Get analog read
// Add new analog reading to buffer and return oldest reading
int oldestSample = addSample(analogValue);
// Get running average, pass the latest and oldest analog sample reading
analogSamplesAverage = runningAverage(analogValue, oldestSample);
if(millis() > time + plotDelay){
// Convert analog value to load in grams
int loadGrams = map(analogSamplesAverage, analogLow, analogHigh, loadLow, loadHigh);
loadGrams = loadGrams - loadAdjustment; // Shift the load to measure from 0 load
if (calibrate) {// print test results
Serial.print("Analog pin value: ");Serial.println(analogValue);
Serial.print("Smoothed analog value: ");Serial.println(analogSamplesAverage);
Serial.print("Scale load (grammes): ");Serial.println(loadGrams);
Serial.println(" ");
} else { // Output to Processing as such
Serial.println(loadGrams);
}
time = millis(); // Set time to mark start of delay
}
}
// Function - running average
long runningAverage(long newSample, long oldSample) {
// Add new sample and subtract old sample from the samples buffer total
// then return sample average
sampleBufferTotal += newSample; // Add new analog sample
sampleBufferTotal -= oldSample; // Subtract oldest analog sample
return sampleBufferTotal / samplesBuffer; // Return average analog sample reading
}
// Function - add and remove analog samples from ring buffer, return oldest sample
// The ring buffer is used to keep track of the oldest sample.
int addSample(int newSample) {
// Add new analog read sample to the ring buffer and return the oldest analog reading
int oldSample;
if (lastSampleIndex == samplesBuffer - 1 ) { // Check if end off buffer reached
oldSample = analogSamples[0]; // Get old analog sample from start of buffer
analogSamples[0] = newSample; // Add new analog sample to start of buffer
lastSampleIndex = 0; // Record sample index currently working on
} else {
lastSampleIndex ++; // Move to next index
oldSample = analogSamples[lastSampleIndex]; // Get old analog sample
analogSamples[lastSampleIndex] = newSample; // Add new analog sample
}
return oldSample; // Return old analog sample
}