-
Notifications
You must be signed in to change notification settings - Fork 2
/
water-level-sensor-demo.ino
180 lines (140 loc) · 3.59 KB
/
water-level-sensor-demo.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
#include <Wire.h>
#include <Arduino.h>
#include <U8x8lib.h>
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ A0, /* data=*/ A1, /* reset=*/ U8X8_PIN_NONE); // Digispark ATTiny85
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
const int ledPin = 2;
const int buzzerPin = 3;
int value = 0;
unsigned char low_data[8] = {0};
unsigned char high_data[12] = {0};
#define NO_TOUCH 0xFE
#define THRESHOLD 100
#define ATTINY1_HIGH_ADDR 0x78
#define ATTINY2_LOW_ADDR 0x77
void getHigh12SectionValue(void)
{
memset(high_data, 0, sizeof(high_data));
Wire.requestFrom(ATTINY1_HIGH_ADDR, 12);
while (12 != Wire.available());
for (int i = 0; i < 12; i++) {
high_data[i] = Wire.read();
}
delay(10);
}
void getLow8SectionValue(void)
{
memset(low_data, 0, sizeof(low_data));
Wire.requestFrom(ATTINY2_LOW_ADDR, 8);
while (8 != Wire.available());
for (int i = 0; i < 8 ; i++) {
low_data[i] = Wire.read(); // receive a byte as character
}
delay(10);
}
void check()
{
int sensorvalue_min = 250;
int sensorvalue_max = 255;
int low_count = 0;
int high_count = 0;
while (1)
{
uint32_t touch_val = 0;
uint8_t trig_section = 0;
low_count = 0;
high_count = 0;
getLow8SectionValue();
getHigh12SectionValue();
Serial.println("low 8 sections value = ");
for (int i = 0; i < 8; i++)
{
Serial.print(low_data[i]);
Serial.print(".");
if (low_data[i] >= sensorvalue_min && low_data[i] <= sensorvalue_max)
{
low_count++;
}
if (low_count == 8)
{
Serial.print(" ");
Serial.print("PASS");
}
}
Serial.println(" ");
Serial.println(" ");
Serial.println("high 12 sections value = ");
for (int i = 0; i < 12; i++)
{
Serial.print(high_data[i]);
Serial.print(".");
if (high_data[i] >= sensorvalue_min && high_data[i] <= sensorvalue_max)
{
high_count++;
}
if (high_count == 12)
{
Serial.print(" ");
Serial.print("PASS");
}
}
Serial.println(" ");
Serial.println(" ");
for (int i = 0 ; i < 8; i++) {
if (low_data[i] > THRESHOLD) {
touch_val |= 1 << i;
}
}
for (int i = 0 ; i < 12; i++) {
if (high_data[i] > THRESHOLD) {
touch_val |= (uint32_t)1 << (8 + i);
}
}
while (touch_val & 0x01)
{
trig_section++;
touch_val >>= 1;
}
value = trig_section * 5;
SERIAL.print("water level = ");
SERIAL.print(value);
SERIAL.println("% ");
SERIAL.println(" ");
SERIAL.println("*********************************************************");
u8x8.setFont(u8x8_font_7x14B_1x2_r);
u8x8.setCursor(0,1);
u8x8.print("water level:");
u8x8.setCursor(3,20);
u8x8.print(value);
u8x8.print("% ");
delay(50);
if(trig_section * 5 == 100)
{ u8x8.setCursor(3,20);
u8x8.print("overflow!");
delay(100);
digitalWrite(ledPin, HIGH);
analogWrite(buzzerPin, 256);
delay(50);
digitalWrite(ledPin, LOW);
analogWrite(buzzerPin, LOW);
delay(50);}
else
{digitalWrite(ledPin, LOW);
analogWrite(buzzerPin, LOW);}
}
}
void setup() {
pinMode(ledPin, OUTPUT);
SERIAL.begin(115200);
Wire.begin();
u8x8.begin();
u8x8.setPowerSave(0);
}
void loop()
{
check();
}