Components and supplies
Rotary potentiometer (generic)
Arduino Due
Loadcell Sensor
Male/Male Jumper Wires
Breadboard (generic)
Resistor 1k ohm
TM1637 4-bits digital LED-display
Pushbutton Switch, Momentary
SparkFun Load Cell Amplifier - HX711
SG90 Micro-servo motor
Male/Female Jumper Wires
Apps and platforms
Arduino IDE
Project description
Code
final code.ino
arduino
1#include <Q2HX711.h> 2#include <TM1637.h> 3#include <Servo.h> 4 5#define POTENTIOMETER_PIN A5 // potentiometer port 6Servo servo; 7int CLK = 11; 8int DIO = 10; 9float weight = 0.00; 10 11TM1637 tm(CLK, DIO); 12int firstdigit = 0; 13int seconddigit = 0; 14int thirddigit = 0; 15int fourthdigit = 0; 16 17const byte hx711_data_pin = 4; 18const byte hx711_clock_pin = 5; 19const int buttonPin = 2; // the number of the pushbutton pin 20 21float y1 = 145.28; // calibrated mass to be added 22long x1 = 0L; 23long x0 = 0L; 24float avg_size = 10.0; // amount of averages for each mass measurement 25int buttonState = 0; // variable for reading the pushbutton status 26 27 28Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711 29 30void setup() { 31 pinMode(buttonPin, INPUT_PULLUP); 32 servo.attach(8); 33 tm.init(); 34 35 //set brightness; 0-7 36 tm.set(3); 37 Serial.begin(9600); // prepare serial port 38 delay(1000); // allow load cell and hx711 to settle 39 x0 = 8363340; // rate for scale 40 x1 = 8545139; // rate for scale 41} 42 43void loop() { 44 servo.write(0); 45 while(digitalRead(buttonPin) == HIGH){ 46 delay(20); 47 } 48 long reading = 0; 49 for (int jj = 0; jj < int(avg_size); jj++) { 50 reading += hx711.read(); 51 } 52 reading /= long(avg_size); 53 // calculating mass based on calibration and linear fit 54 float ratio_1 = (float) (reading - x0); 55 float ratio_2 = (float) (x1 - x0); 56 float ratio = ratio_1 / ratio_2; 57 float tare2 = y1 * ratio; 58 tm.display(3, 0); 59 tm.display(2, 0); 60 tm.display(1, 0); 61 tm.display(0, 0); 62 63 64 delay(100); // little bit time for board to rest 65 while (digitalRead(buttonPin) == HIGH) { 66 int data = analogRead(POTENTIOMETER_PIN); // read pot 67 firstdigit = map (data, 0, 1023, 0, 9); // convert to 1 digit 68 tm.display(3, firstdigit); // print digit in lcd 69 } 70 delay(200); // little bit time for board to rest 71 72 while (digitalRead(buttonPin) == HIGH) { 73 int data = analogRead(POTENTIOMETER_PIN); // read pot 74 seconddigit = map(data, 0, 1023, 0, 9); // convert to 1 digit 75 tm.display(2, seconddigit); // print digit in lcd 76 } 77 delay(500); // little bit time for board to rest 78 while ( digitalRead(buttonPin) == HIGH) { 79 int data = analogRead(POTENTIOMETER_PIN); // read pot 80 thirddigit = map(data, 0, 1023, 0, 9); // convert to 1 digit 81 tm.display(1, thirddigit); // print digit in lcd 82 } 83 delay(500); 84 while (digitalRead(buttonPin) == HIGH) { 85 int data = analogRead(POTENTIOMETER_PIN); // read pot 86 fourthdigit = map(data, 0, 1023, 0, 9); // convert to 1 digitgit); 87 tm.display(0, fourthdigit); // print digit in lcd 88 delay(100); // little bit time for board to rest 89 } 90 float weight = firstdigit * 0.1 + seconddigit + thirddigit * 10 + fourthdigit * 100 ; // the data is in the while loop so program cant read it out of the loop 91 // averaging reading 92 93 for (int jj = 0; jj < int(avg_size); jj++) { 94 reading += hx711.read(); 95 } 96 reading /= long(avg_size); 97 // calculating mass based on calibration and linear fit 98 ratio_1 = (float) (reading - x0); 99 ratio_2 = (float) (x1 - x0); 100 ratio = ratio_1 / ratio_2; 101 float mass = y1 * ratio; 102 float taredmass = mass - tare2; 103 104 float r = round(taredmass * 10); 105 106 int d1 = (int) r % 10; 107 int d2 = (int) r / 10 % 10; 108 int d3 = (int) r / 100 % 10; 109 int d4 = (int) r / 1000 % 10; 110 while (digitalRead(buttonPin) == HIGH) { 111 while (mass < weight - 1) { 112 113 long reading = 0; 114 for (int jj = 0; jj < int(avg_size); jj++) { 115 reading += hx711.read(); 116 } 117 reading /= long(avg_size); 118 // calculating mass based on calibration and linear fit 119 float ratio_1 = (float) (reading - x0); 120 float ratio_2 = (float) (x1 - x0); 121 float ratio = ratio_1 / ratio_2; 122 mass = y1 * ratio; 123 taredmass = mass - tare2; 124 125 126 float r = round(taredmass * 10); 127 Serial.println(mass); 128 Serial.println(weight); 129 130 int d1 = (int) r % 10; 131 int d2 = (int) r / 10 % 10; 132 int d3 = (int) r / 100 % 10; 133 int d4 = (int) r / 1000 % 10; 134 tm.display(3, d1); 135 tm.display(2, d2); 136 tm.display(1, d3); 137 tm.display(0, d4); 138 139 servo.write(120); 140 delay(200); 141 servo.write(180); 142 143 144 145 } 146 while (taredmass < weight - 0.5) { 147 148 long reading = 0; 149 for (int jj = 0; jj < int(avg_size); jj++) { 150 reading += hx711.read(); 151 } 152 reading /= long(avg_size); 153 // calculating mass based on calibration and linear fit 154 float ratio_1 = (float) (reading - x0); 155 float ratio_2 = (float) (x1 - x0); 156 float ratio = ratio_1 / ratio_2; 157 mass = y1 * ratio; 158 taredmass = mass - tare2; 159 160 161 float r = round(taredmass * 10); 162 Serial.println(mass); 163 Serial.println(weight); 164 165 int d1 = (int) r % 10; 166 int d2 = (int) r / 10 % 10; 167 int d3 = (int) r / 100 % 10; 168 int d4 = (int) r / 1000 % 10; 169 tm.display(3, d1); 170 tm.display(2, d2); 171 tm.display(1, d3); 172 tm.display(0, d4); 173 174 servo.write(110); 175 delay(500); 176 servo.write(170); 177 178 179 180 } 181 while (taredmass < weight - 0.2) { 182 183 long reading = 0; 184 for (int jj = 0; jj < int(avg_size); jj++) { 185 reading += hx711.read(); 186 } 187 reading /= long(avg_size); 188 // calculating mass based on calibration and linear fit 189 float ratio_1 = (float) (reading - x0); 190 float ratio_2 = (float) (x1 - x0); 191 float ratio = ratio_1 / ratio_2; 192 mass = y1 * ratio; 193 taredmass = mass - tare2; 194 195 196 float r = round(taredmass * 10); 197 Serial.println(mass); 198 Serial.println(weight); 199 200 int d1 = (int) r % 10; 201 int d2 = (int) r / 10 % 10; 202 int d3 = (int) r / 100 % 10; 203 int d4 = (int) r / 1000 % 10; 204 tm.display(3, d1); 205 tm.display(2, d2); 206 tm.display(1, d3); 207 tm.display(0, d4); 208 209 servo.write(100); 210 delay(500); 211 servo.write(150); 212 213 214 215 } 216 servo.write(90); 217 long reading = 0; 218 for (int jj = 0; jj < int(avg_size); jj++) { 219 reading += hx711.read(); 220 } 221 reading /= long(avg_size); 222 // calculating mass based on calibration and linear fit 223 float ratio_1 = (float) (reading - x0); 224 float ratio_2 = (float) (x1 - x0); 225 float ratio = ratio_1 / ratio_2; 226 mass = y1 * ratio; 227 taredmass = mass- tare2; 228 float r = round(taredmass * 10); 229 int d1 = (int) r % 10; 230 int d2 = (int) r / 10 % 10; 231 int d3 = (int) r / 100 % 10; 232 int d4 = (int) r / 1000 % 10; 233 tm.display(3, d1); 234 tm.display(2, d2); 235 tm.display(1, d3); 236 tm.display(0, d4); 237 238 } 239 240 delay(1000); 241} 242
Downloadable files
Schematic
Schematic
Schematic
Schematic
Comments
Only logged in users can leave comments
proprietor
0 Followers
•0 Projects
Table of contents
Intro
0
0