jun takahashi
/
loadcell_2
ACM experimental
main.cpp@0:6f1fa439a9bf, 2019-02-28 (annotated)
- Committer:
- j8813
- Date:
- Thu Feb 28 02:43:38 2019 +0000
- Revision:
- 0:6f1fa439a9bf
ACM experimental
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j8813 | 0:6f1fa439a9bf | 1 | #include "mbed.h" |
j8813 | 0:6f1fa439a9bf | 2 | #include "HX711.h" |
j8813 | 0:6f1fa439a9bf | 3 | |
j8813 | 0:6f1fa439a9bf | 4 | DigitalOut gpo(D0); |
j8813 | 0:6f1fa439a9bf | 5 | //DigitalOut led(LED_BLUE); |
j8813 | 0:6f1fa439a9bf | 6 | DigitalOut led(LED1); |
j8813 | 0:6f1fa439a9bf | 7 | |
j8813 | 0:6f1fa439a9bf | 8 | HX711 scale(p6,p7); |
j8813 | 0:6f1fa439a9bf | 9 | |
j8813 | 0:6f1fa439a9bf | 10 | AnalogIn scaleRaw(A3); |
j8813 | 0:6f1fa439a9bf | 11 | Serial pc(USBTX, USBRX); // USB Serial Terminal |
j8813 | 0:6f1fa439a9bf | 12 | float calibration_factor = 1.000; //-7050 worked for my 440lb max scale setup |
j8813 | 0:6f1fa439a9bf | 13 | int averageSamples = 100; |
j8813 | 0:6f1fa439a9bf | 14 | |
j8813 | 0:6f1fa439a9bf | 15 | int main(void) |
j8813 | 0:6f1fa439a9bf | 16 | { |
j8813 | 0:6f1fa439a9bf | 17 | // pc.printf("Starting Scale"); |
j8813 | 0:6f1fa439a9bf | 18 | // pc.printf("HX711 calibration sketch"); |
j8813 | 0:6f1fa439a9bf | 19 | // pc.printf("Remove all weight from scale"); |
j8813 | 0:6f1fa439a9bf | 20 | // pc.printf("After readings begin, place known weight on scale"); |
j8813 | 0:6f1fa439a9bf | 21 | // pc.printf("Press + or a to increase calibration factor"); |
j8813 | 0:6f1fa439a9bf | 22 | // pc.printf("Press - or z to decrease calibration factor"); |
j8813 | 0:6f1fa439a9bf | 23 | |
j8813 | 0:6f1fa439a9bf | 24 | |
j8813 | 0:6f1fa439a9bf | 25 | scale.setScale(0); |
j8813 | 0:6f1fa439a9bf | 26 | scale.tare(); //Reset the scale to 0 |
j8813 | 0:6f1fa439a9bf | 27 | |
j8813 | 0:6f1fa439a9bf | 28 | long zero_factor = scale.averageValue(averageSamples); //Get a baseline reading |
j8813 | 0:6f1fa439a9bf | 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:6f1fa439a9bf | 30 | |
j8813 | 0:6f1fa439a9bf | 31 | while (true) { |
j8813 | 0:6f1fa439a9bf | 32 | scale.setScale(calibration_factor); //Adjust to this calibration factor |
j8813 | 0:6f1fa439a9bf | 33 | float weight = scale.getGram(); |
j8813 | 0:6f1fa439a9bf | 34 | //float raw = scaleRaw.read(); |
j8813 | 0:6f1fa439a9bf | 35 | pc.printf(" %5.3e\n", weight); |
j8813 | 0:6f1fa439a9bf | 36 | //pc.printf("Raw Value: %.7f\n", raw); |
j8813 | 0:6f1fa439a9bf | 37 | // pc.printf(" calibration_factor: %5.3e\n", calibration_factor); |
j8813 | 0:6f1fa439a9bf | 38 | |
j8813 | 0:6f1fa439a9bf | 39 | |
j8813 | 0:6f1fa439a9bf | 40 | if(pc.readable()) { |
j8813 | 0:6f1fa439a9bf | 41 | char temp = pc.getc(); |
j8813 | 0:6f1fa439a9bf | 42 | if(temp == '+' || temp == 'a') |
j8813 | 0:6f1fa439a9bf | 43 | calibration_factor += 10; |
j8813 | 0:6f1fa439a9bf | 44 | else if(temp == '-' || temp == 'z') |
j8813 | 0:6f1fa439a9bf | 45 | calibration_factor -= 10; |
j8813 | 0:6f1fa439a9bf | 46 | } |
j8813 | 0:6f1fa439a9bf | 47 | gpo = !gpo; // toggle pin |
j8813 | 0:6f1fa439a9bf | 48 | led = !led; // toggle led |
j8813 | 0:6f1fa439a9bf | 49 | wait(0.2f); |
j8813 | 0:6f1fa439a9bf | 50 | } |
j8813 | 0:6f1fa439a9bf | 51 | } |