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