Saturday, November 7, 2015

MENGUKUR KETINGGIAN AIR DENGAN ARDUINO

Pengen Coba Part 2 (dua)
We present the candidature of Mr. Danilo Abbasciano that is proposed for the realization of the firmware for the TiDiGino project and that presents us an application with Arduino: Display the level of a tank.
The project reads and displays the height of water level in a well or a cistern.

We will use the open source Arduino hardware device, an ultrasonic Parallax sensor to measure the height of the water, a 16 x 2 LCD display with Hitachi HD44780 driver and a buzzer that is activated when the level exceeds the threshold.


The project, as we have already mentioned, is composed of several parts. A sonar sensor to be placed at the top of the well (at a safe distance from water level) that points downward so as to measure the distance between the point of placement (in our case the highest point of the well) and the surface water. Taking a simple difference between known quantities: the distance between the bottom and the measurement read from the sensor, we get the height of the water surface. Knowing the surface of the well also it is easy to calculate the volume of water present. At predetermined intervals Arduino reads the distances and displays the height and volume of water in the well

There is a horizontal bar that shows the trend in relative water level inside the well for an easily and immediately read.

If the level exceeds a first threshold warning triggered the alarm buzzer to beep slowly if the level exceeds the second threshold, the ringing frequency increases until the level will drop back below the threshold or when you manually turn off the ringer through a button
Arduino controls the operating logic, using the following sketches:
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- mode: c -*- */
/**
 * pozzo.pde
 * version: 1.2
 */

#include <LiquidCrystal.h>

#define PING_PIN 13
#define BUZZER_PIN 8
#define SWITCH_INT 0 /* 0 => pin 2 */
#define PI 3.1415926535898
#define SUPERFICE_BASE (R_POZZO * R_POZZO * PI)
#define SIZE_BAR (16 * 5)
#define ALARM_ICON 0 /* code */
#define SOUND_ICON 6 /* code */
#define SOUND_ICON_ON 7 /* code */

#define R_POZZO 0.5 /* raggio pozzo (m) */
#define H_POZZO 146.0 /* cm */
#define SOGLIA_ALLARME_1 100 /* cm */
#define SOGLIA_ALLARME_2 120 /* cm */
#define DELAY_0 60000 /* ms; 1000 * 60 * 1 = 1 min */
#define DELAY_1 600 /* ms */
#define DELAY_2 200 /* ms */

/* initialize the library with the numbers of the interface pins */
LiquidCrystal lcd(12, 11, 5, 4, 3, 6);

int mute = 0;

byte *getChar(int n, byte newChar[]) {
  int i;
  byte code[5] = {
    B10000,
    B11000,
    B11100,
    B11110,
    B11111};

  for (i = 0; i < 8; i++)
    newChar[i] = code[n - 1];

  return newChar;
}

void setup() {
  int i;
  float h;
  byte newChar[8];

  /* set up the LCD's number of rows and columns: */
  lcd.begin(16, 2);

  for (i = 1; i < 6; i++)
    lcd.createChar(i, getChar(i, newChar));

  newChar = {
    B00000,
    B00100,
    B01010,
    B01010,
    B11111,
    B00100,
    B00000,
  };

  lcd.createChar(ALARM_ICON, newChar);

  newChar = {
    B00011,
    B00101,
    B11001,
    B11001,
    B11001,
    B00101,
    B00011,
  };

  lcd.createChar(SOUND_ICON, newChar);

  newChar = {
    B00100,
    B10010,
    B01001,
    B01001,
    B01001,
    B10010,
    B00100,
  };

  lcd.createChar(SOUND_ICON_ON, newChar); 

  pinMode(BUZZER_PIN, OUTPUT);

  /**
   * LOW to trigger the interrupt whenever the pin is low,
   * CHANGE to trigger the interrupt whenever the pin changes value
   * RISING to trigger when the pin goes from low to high,
   * FALLING for when the pin goes from high to low.
   */
  attachInterrupt(SWITCH_INT, button, RISING);

  /* initialize serial communication */
  Serial.begin(9600);
}

void loop() {
  long hWatherCm;
  int litres;

  hWatherCm = read_height();
  if (check_alarm(hWatherCm) != 0) /* read again wather height */
    hWatherCm = read_height();

  lcd.clear();

  print_histogram(hWatherCm);

  lcd.setCursor(0, 1);

  lcd.print(hWatherCm);
  lcd.print(" cm - ");

  // litres = SUPERFICE_BASE * (hWather / 100.0) * 1000
  litres = floor(SUPERFICE_BASE * hWatherCm * 10);
  lcd.print(litres);
  lcd.print(" l ");

  lcd.setCursor(14, 1);
  lcd.write(SOUND_ICON);
  lcd.setCursor(15, 1);
  if (!mute)
    lcd.write(SOUND_ICON_ON);
  else
    lcd.write('X');

/*
  Serial.print("cm = ");
  Serial.println(hWatherCm);
*/

  switch (check_alarm(hWatherCm)) {
  case 1:
    lcd.setCursor(0, 0);
    lcd.write(ALARM_ICON);

    buzz(200);
    delay(DELAY_1);
    break;

  case 2:
    lcd.setCursor(0, 0);
    lcd.write(ALARM_ICON);

    buzz(200);
    delay(200);
    buzz(200);
    delay(DELAY_2);
    break;

  case 0: // no alarm
    delay(DELAY_0);
  }
}

void print_histogram(int hWatherCm) {
  int i;
  int bloks;
  float histogram;

  // hWatherCm : HPOZZO = histogram : SIZE_BAR
  histogram = (SIZE_BAR * hWatherCm) / H_POZZO;
  histogram = histogram + 0.5;

  bloks = (int)histogram / 5;

  for (i = 0; i < bloks; i++)
    lcd.write(5);

  if ((int)(histogram) % 5 > 0)
    lcd.write((int)(histogram) % 5);
}

long read_height() {
  /**
   * establish variables for duration of the ping,
   * and the distance result in centimeters:
   */
  long duration, hWatherCm;

  /**
   * The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   * Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   */
  pinMode(PING_PIN, OUTPUT);
  digitalWrite(PING_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(PING_PIN, HIGH);
  delayMicroseconds(5);
  digitalWrite(PING_PIN, LOW);

  /**
   * The same pin is used to read the signal from the PING))): a HIGH
   * pulse whose duration is the time (in microseconds) from the sending
   * of the ping to the reception of its echo off of an object.
   */
  pinMode(PING_PIN, INPUT);
  duration = pulseIn(PING_PIN, HIGH);

  /* convert the time into a distance */
  hWatherCm = H_POZZO - microseconds_to_centimeters(duration);

  if (hWatherCm < 0)
    return 0;

  if (hWatherCm > H_POZZO)
    return H_POZZO;

  return hWatherCm;
}

void buzz(int msec) {
  if (!mute)
    digitalWrite(BUZZER_PIN, HIGH);
  delay(msec);
  digitalWrite(BUZZER_PIN, LOW);
}

int check_alarm(int hWatherCm) {
  if (hWatherCm > SOGLIA_ALLARME_1) {
     if (hWatherCm < SOGLIA_ALLARME_2)
       return 1;
     else
       return 2;
  }
  return 0;
}

long microseconds_to_centimeters(long microseconds) {
  /**
   * The speed of sound is 340.29 m/s or 29.4 microseconds per centimeter.
   * The ping travels out and back, so to find the distance of the
   * object we take half of the distance travelled.
   */
  return microseconds / 29.387 / 2;
}

void button() {
  //  Serial.println("Pulsante premuto");
  mute = !mute;

  lcd.setCursor(15, 1);
  if (!mute)
    lcd.write(SOUND_ICON_ON);
  else
    lcd.write('X');
}

Sumber :klik disini

Bila Bermanfaat Di Shared ya

No comments: