ACM

Dependencies:   mbed HX711

Committer:
j8813
Date:
Thu Feb 28 02:31:40 2019 +0000
Revision:
1:239dc2b9afae
Parent:
0:61d69de5d788
ACM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j8813 0:61d69de5d788 1 #include "mbed.h"
j8813 0:61d69de5d788 2 #include "HX711.h"
j8813 0:61d69de5d788 3
j8813 0:61d69de5d788 4 DigitalOut gpo(D0);
j8813 0:61d69de5d788 5 //DigitalOut led(LED_BLUE);
j8813 0:61d69de5d788 6 DigitalOut led(LED1);
j8813 0:61d69de5d788 7
j8813 0:61d69de5d788 8 HX711 scale(p6,p7);
j8813 0:61d69de5d788 9
j8813 0:61d69de5d788 10 AnalogIn scaleRaw(A3);
j8813 0:61d69de5d788 11 Serial pc(USBTX, USBRX); // USB Serial Terminal
j8813 0:61d69de5d788 12 float calibration_factor = 1000; //-7050 worked for my 440lb max scale setup
j8813 0:61d69de5d788 13 int averageSamples = 100;
j8813 0:61d69de5d788 14
j8813 0:61d69de5d788 15 int main(void)
j8813 0:61d69de5d788 16 {
j8813 0:61d69de5d788 17 pc.printf("Starting Scale");
j8813 0:61d69de5d788 18 pc.printf("HX711 calibration sketch");
j8813 0:61d69de5d788 19 pc.printf("Remove all weight from scale");
j8813 0:61d69de5d788 20 pc.printf("After readings begin, place known weight on scale");
j8813 0:61d69de5d788 21 pc.printf("Press + or a to increase calibration factor");
j8813 0:61d69de5d788 22 pc.printf("Press - or z to decrease calibration factor");
j8813 0:61d69de5d788 23
j8813 0:61d69de5d788 24
j8813 0:61d69de5d788 25 scale.setScale(0);
j8813 0:61d69de5d788 26 scale.tare(); //Reset the scale to 0
j8813 0:61d69de5d788 27
j8813 0:61d69de5d788 28 long zero_factor = scale.averageValue(averageSamples); //Get a baseline reading
j8813 0:61d69de5d788 29 pc.printf("Zero factor: %.4f\n" , zero_factor); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
j8813 0:61d69de5d788 30
j8813 0:61d69de5d788 31 while (true) {
j8813 0:61d69de5d788 32 scale.setScale(calibration_factor); //Adjust to this calibration factor
j8813 0:61d69de5d788 33 float weight = scale.getGram();
j8813 0:61d69de5d788 34 //float raw = scaleRaw.read();
j8813 0:61d69de5d788 35 pc.printf("Reading: %5.3ef\n", weight);
j8813 0:61d69de5d788 36 //pc.printf("Raw Value: %.7f\n", raw);
j8813 0:61d69de5d788 37 pc.printf(" calibration_factor: %5.3e\n", calibration_factor);
j8813 0:61d69de5d788 38
j8813 0:61d69de5d788 39
j8813 0:61d69de5d788 40 if(pc.readable()) {
j8813 0:61d69de5d788 41 char temp = pc.getc();
j8813 0:61d69de5d788 42 if(temp == '+' || temp == 'a')
j8813 0:61d69de5d788 43 calibration_factor += 10;
j8813 0:61d69de5d788 44 else if(temp == '-' || temp == 'z')
j8813 0:61d69de5d788 45 calibration_factor -= 10;
j8813 0:61d69de5d788 46 }
j8813 0:61d69de5d788 47 gpo = !gpo; // toggle pin
j8813 0:61d69de5d788 48 led = !led; // toggle led
j8813 0:61d69de5d788 49 wait(0.2f);
j8813 0:61d69de5d788 50 }
j8813 0:61d69de5d788 51 }