f14f062bd2d6ffd830577be38c3239619e83d084
[Arduino] / HX711 / HX711.ino
1 #include "HX711.h"
2
3 // HX711.DOUT   - pin #A2
4 // HX711.PD_SCK - pin #A1
5
6 HX711 scale(A2, A1);            // parameter "gain" is ommited; the default value 128 is used by the library
7
8 #include <LiquidCrystal.h>
9 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // rs enable d0-d3
10
11 void setup() {
12   lcd.begin(16, 2);
13   lcd.print("HX711");
14
15   Serial.begin(115200);
16   Serial.println("HX711 Demo");
17
18   Serial.println("Before setting up the scale:");
19   Serial.print("read: \t\t");
20   Serial.println(scale.read());                 // print a raw reading from the ADC
21
22   Serial.print("read average: \t\t");
23   Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC
24
25   Serial.print("get value: \t\t");
26   Serial.println(scale.get_value(5));           // print the average of 5 readings from the ADC minus the tare weight (not set yet)
27
28   Serial.print("get units: \t\t");
29   Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight (not set) divided 
30                                                 // by the SCALE parameter (not set yet)  
31
32   scale.set_scale(465.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
33   scale.tare();                                 // reset the scale to 0
34
35   Serial.println("After setting up the scale:");
36
37   Serial.print("read: \t\t");
38   Serial.println(scale.read());                 // print a raw reading from the ADC
39
40   Serial.print("read average: \t\t");
41   Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC
42
43   Serial.print("get value: \t\t");
44   Serial.println(scale.get_value(5));           // print the average of 5 readings from the ADC minus the tare weight, set with tare()
45
46   Serial.print("get units: \t\t");
47   Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided 
48                                                 // by the SCALE parameter set with set_scale
49
50   Serial.println("Readings:");
51 }
52
53 #ifdef FLOAT_AVG
54 #define g_bucket 10
55 #define g_size g_bucket*3
56 float g_history[g_size];
57 int g_pos = 0;
58 #endif
59
60 void loop() {
61   float g = scale.get_units();
62   Serial.print(g);
63
64  
65   lcd.setCursor(0,0); // col,row
66   lcd.print(g);
67   lcd.print(" ");
68
69 #ifdef FLOAT_AVG
70   g_history[g_pos++] = g; // insert into circular buffer
71   g_pos %= g_size;
72
73   float sum = 0;
74   for(int i=1; i<g_size; i++) {
75     int pos = ( g_pos + i ) % g_size;
76     sum += g_history[pos];
77     if ( i % g_bucket == 0) {
78         float avg = sum / i;
79         Serial.print(" ");
80         Serial.print(avg, 3);
81         lcd.print(avg);
82         lcd.print(" ");
83     }
84   }
85 #endif
86  
87   int key = analogRead(0);
88   Serial.print("\t");
89   Serial.println(key);
90
91   lcd.setCursor(12,1);
92   lcd.print(key);
93   lcd.print("   ");
94
95   lcd.setCursor(0,1);
96   if ( key > 1000 ) {
97         // nothing pressed
98   } else if ( key > 800 ) {
99         lcd.print(" tare ");
100         scale.tare();
101   }
102
103   uint32_t adc = scale.read();
104   lcd.print(adc);
105   lcd.print(" ");
106
107   Serial.print("\t");
108   Serial.println(adc);
109
110
111 //  scale.power_down();                         // put the ADC in sleep mode
112 //  delay(100);
113 //  scale.power_up();
114 }