-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpg_leak_booking.ino
204 lines (195 loc) · 5.72 KB
/
lpg_leak_booking.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include<LiquidCrystal.h>
float f1;
int buz=5,gas=A1,fire=A2,m1=A4,m2=A5;
//RS=8,RW=Gnd,EN=9,D4=0,D5=1,D6=2,D7=3;
LiquidCrystal lcd(13,12,11,10,9,8);
//-----------Device-1-----------------//
byte PD_SCK; // Power Down and Serial Clock Input Pin
byte DOUT; // Serial Data Output Pin
byte GAIN; // amplification factor
long OFFSET = 0; // used for tare weight
float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(buz,OUTPUT);
pinMode(m1,OUTPUT);
pinMode(m2,OUTPUT);
//pinMode(gas,INPUT);
pinMode(fire,INPUT);
delay(500);
lcd.setCursor(0,0); //lcd.setCursor(column,line) starts with 0
lcd.print(" IOT BASED GAS ");
lcd.setCursor(0,1);
lcd.print(" MANAGEMENT SYS ");
delay(500);
//------------------- HX711 -----------------
scale1_begin(2,3,128); //(Dt,Sck,Gain)
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale1_read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale1_read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale1_get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale1_get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale1_set_scale(-110010); // this value is obtained by calibrating the scale with known weights; see the README for details
scale1_tare(5); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale1_read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale1_read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale1_get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale1_get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
lcd.setCursor(0,0); //lcd.setCursor(column,line) starts with 0
lcd.print("LPG : WEIG: FIRE");
lcd.setCursor(0,1);
lcd.print(" : : ");
delay(1000);
}
void loop()
{
//----- Read weights -----
int f = digitalRead(fire);
float g = analogRead(gas);
f1=scale1_get_units(5);
lcd.setCursor(0,1);
lcd.print(" : : ");
lcd.setCursor(0,1);
lcd.print(g);
lcd.setCursor(5,1);
lcd.print(f1);
lcd.setCursor(12,1);
lcd.print(f);
Serial.println("*"+String(g)+"/"+String(f)+"/"+String(f1)+"#");
if(g>500 || f==1)
{
digitalWrite(buz,HIGH);
}
else
{
digitalWrite(buz,LOW);
}
scale1_power_down(); // put the ADC in sleep mode
delay(1000);
scale1_power_up();
delay(2000);
}
//***********************************************************
// Device-1 Functions
//***********************************************************
void scale1_begin(byte dout, byte pd_sck, byte gain)
{
PD_SCK = pd_sck;
DOUT = dout;
pinMode(PD_SCK, OUTPUT);
pinMode(DOUT, INPUT);
scale1_set_gain(gain);
}
bool scale1_is_ready()
{
return digitalRead(DOUT) == LOW;
}
void scale1_set_gain(byte gain)
{
switch (gain) {
case 128: // channel A, gain factor 128
GAIN = 1;
break;
case 64: // channel A, gain factor 64
GAIN = 3;
break;
case 32: // channel B, gain factor 32
GAIN = 2;
break;
}
digitalWrite(PD_SCK, LOW);
scale1_read();
}
void scale1_yield(void) {};
long scale1_read()
{
// wait for the chip to become ready
while (!scale1_is_ready()) {
// Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue)
scale1_yield();
}
unsigned long value = 0;
uint8_t data[3] = { 0 };
uint8_t filler = 0x00;
// pulse the clock pin 24 times to read the data
data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);
// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < GAIN; i++) {
digitalWrite(PD_SCK, HIGH);
digitalWrite(PD_SCK, LOW);
}
// Replicate the most significant bit to pad out a 32-bit signed integer
if (data[2] & 0x80) {
filler = 0xFF;
} else {
filler = 0x00;
}
// Construct a 32-bit signed integer
value = ( static_cast<unsigned long>(filler) << 24
| static_cast<unsigned long>(data[2]) << 16
| static_cast<unsigned long>(data[1]) << 8
| static_cast<unsigned long>(data[0]) );
return static_cast<long>(value);
}
long scale1_read_average(byte times)
{
long sum = 0;
for (byte i = 0; i < times; i++) {
sum += scale1_read();
scale1_yield();
}
return sum / times;
}
double scale1_get_value(byte times)
{
return scale1_read_average(times) - OFFSET;
}
float scale1_get_units(byte times)
{
return scale1_get_value(times) / SCALE;
}
void scale1_tare(byte times)
{
double sum = scale1_read_average(times);
scale1_set_offset(sum);
}
void scale1_set_scale(float scale)
{
SCALE = scale;
}
float scale1_get_scale()
{
return SCALE;
}
void scale1_set_offset(long offset)
{
OFFSET = offset;
}
long scale1_get_offset()
{
return OFFSET;
}
void scale1_power_down()
{
digitalWrite(PD_SCK, LOW);
digitalWrite(PD_SCK, HIGH);
}
void scale1_power_up()
{
digitalWrite(PD_SCK, LOW);
}