![](/media/cache/profiles/Cristian_Castro_-_Foto.jpg.50x50_q85.jpg)
Lectura de una celda de Carga HX711.
main.cpp@1:392a3fb6d177, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 04:51:59 2021 +0000
- Revision:
- 1:392a3fb6d177
- Parent:
- 0:5d67331a6e7e
Lectura de una celda de Carga.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
franni | 0:5d67331a6e7e | 1 | #include "mbed.h" |
CCastrop1012 | 1:392a3fb6d177 | 2 | #include "Hx711.h" |
CCastrop1012 | 1:392a3fb6d177 | 3 | |
CCastrop1012 | 1:392a3fb6d177 | 4 | #define n_muestras 10 |
franni | 0:5d67331a6e7e | 5 | |
CCastrop1012 | 1:392a3fb6d177 | 6 | DigitalOut led(LED1); |
CCastrop1012 | 1:392a3fb6d177 | 7 | |
CCastrop1012 | 1:392a3fb6d177 | 8 | Hx711 LoadCeld(A1, A0, 1, 1.0); |
franni | 0:5d67331a6e7e | 9 | |
franni | 0:5d67331a6e7e | 10 | Serial rs232(USBTX, USBRX); // USB Serial Terminal |
CCastrop1012 | 1:392a3fb6d177 | 11 | |
CCastrop1012 | 1:392a3fb6d177 | 12 | |
CCastrop1012 | 1:392a3fb6d177 | 13 | |
CCastrop1012 | 1:392a3fb6d177 | 14 | |
franni | 0:5d67331a6e7e | 15 | |
CCastrop1012 | 1:392a3fb6d177 | 16 | |
CCastrop1012 | 1:392a3fb6d177 | 17 | float escala = 0.0; |
CCastrop1012 | 1:392a3fb6d177 | 18 | float offset; |
franni | 0:5d67331a6e7e | 19 | float calibration_factor = 1000; //Ajustar este valor para calibrar el peso exacto |
CCastrop1012 | 1:392a3fb6d177 | 20 | int averageSamples = 300; |
franni | 0:5d67331a6e7e | 21 | |
franni | 0:5d67331a6e7e | 22 | int main(void) |
franni | 0:5d67331a6e7e | 23 | { |
franni | 0:5d67331a6e7e | 24 | |
CCastrop1012 | 1:392a3fb6d177 | 25 | |
CCastrop1012 | 1:392a3fb6d177 | 26 | for(int i = 0; i < n_muestras; i++ ) |
CCastrop1012 | 1:392a3fb6d177 | 27 | { |
CCastrop1012 | 1:392a3fb6d177 | 28 | while(!LoadCeld.is_ready()); |
CCastrop1012 | 1:392a3fb6d177 | 29 | |
CCastrop1012 | 1:392a3fb6d177 | 30 | offset = ( offset + LoadCeld.readRaw()); |
CCastrop1012 | 1:392a3fb6d177 | 31 | |
CCastrop1012 | 1:392a3fb6d177 | 32 | } |
franni | 0:5d67331a6e7e | 33 | |
CCastrop1012 | 1:392a3fb6d177 | 34 | offset = (offset / n_muestras); |
CCastrop1012 | 1:392a3fb6d177 | 35 | LoadCeld.set_offset(offset); |
CCastrop1012 | 1:392a3fb6d177 | 36 | LoadCeld.set_scale(1.0); |
CCastrop1012 | 1:392a3fb6d177 | 37 | rs232.printf("Offset: %.2f \n", offset); |
CCastrop1012 | 1:392a3fb6d177 | 38 | rs232.printf("Offset: %.2f \n", LoadCeld.get_offset()); |
CCastrop1012 | 1:392a3fb6d177 | 39 | offset = 0.0; |
CCastrop1012 | 1:392a3fb6d177 | 40 | |
CCastrop1012 | 1:392a3fb6d177 | 41 | //scale.tare(); //Reset the scale to 0 |
CCastrop1012 | 1:392a3fb6d177 | 42 | |
CCastrop1012 | 1:392a3fb6d177 | 43 | //long zero_factor = scale.averageValue(averageSamples); // Saca promedio de varias lecturas para estabilizar la medida |
franni | 0:5d67331a6e7e | 44 | |
franni | 0:5d67331a6e7e | 45 | while (true) { |
CCastrop1012 | 1:392a3fb6d177 | 46 | |
CCastrop1012 | 1:392a3fb6d177 | 47 | |
CCastrop1012 | 1:392a3fb6d177 | 48 | for(int i = 0; i < n_muestras; i++ ) |
CCastrop1012 | 1:392a3fb6d177 | 49 | { |
CCastrop1012 | 1:392a3fb6d177 | 50 | while(!LoadCeld.is_ready()); |
CCastrop1012 | 1:392a3fb6d177 | 51 | |
CCastrop1012 | 1:392a3fb6d177 | 52 | escala = ( escala + LoadCeld.read()); |
CCastrop1012 | 1:392a3fb6d177 | 53 | |
CCastrop1012 | 1:392a3fb6d177 | 54 | } |
CCastrop1012 | 1:392a3fb6d177 | 55 | escala = (escala / n_muestras); |
CCastrop1012 | 1:392a3fb6d177 | 56 | rs232.printf("Peso: %.2f \n", escala); |
CCastrop1012 | 1:392a3fb6d177 | 57 | escala = 0.0; |
CCastrop1012 | 1:392a3fb6d177 | 58 | //scale.setScale(calibration_factor); |
CCastrop1012 | 1:392a3fb6d177 | 59 | //float weight = scale.getGram(); |
CCastrop1012 | 1:392a3fb6d177 | 60 | //rs232.printf("Peso: %.2f\n", weight); |
CCastrop1012 | 1:392a3fb6d177 | 61 | //lcd.locate(0,0); |
CCastrop1012 | 1:392a3fb6d177 | 62 | //lcd.printf("Peso: %.2f Grms", weight); |
franni | 0:5d67331a6e7e | 63 | led = !led; // toggle led |
franni | 0:5d67331a6e7e | 64 | wait(0.2f); |
franni | 0:5d67331a6e7e | 65 | } |
franni | 0:5d67331a6e7e | 66 | } |