-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart_terrarium.ino
131 lines (122 loc) · 3.28 KB
/
Smart_terrarium.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
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] = {
{'a','b'},
{'c','d'},
};
byte rowPins[ROWS] = {4, 5,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define PHOTO_SENSOR_PIN A1
#define SCREEN_WIDTH 124
#define SCREEN_HEIGHT 32
#define WATER_LEVEL_PIN A6
#define SOIL_MOISTURE A0
#define PUMP 13
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int SOIL_MOISTURE_LIMIT = 20;
int SOIL_MOISTURE_VALUE;
int WATER_LEVEL_VALUE;
long duration;
int distance;
int lt_lvl;
char key;
void setup() {
Serial.begin(9600);
pinMode(PUMP, OUTPUT);
digitalWrite(PUMP, LOW);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(2000);
oled.clearDisplay();
oled.setTextSize(1); // text size
oled.setTextColor(WHITE);
oled.setCursor(0,10);
oled.print("welcome");
oled.display();
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
SOIL_MOISTURE_VALUE=analogRead(SOIL_MOISTURE);
SOIL_MOISTURE_VALUE=map(SOIL_MOISTURE_VALUE,550,10,0,100);
if (SOIL_MOISTURE_VALUE<SOIL_MOISTURE_LIMIT) {
digitalWrite(PUMP, HIGH);
}
else{
digitalWrite(PUMP, LOW);
}
key = keypad.getKey();
oled.setTextSize(1); // text size
oled.setTextColor(WHITE);
WATER_LEVEL_VALUE = analogRead(WATER_LEVEL_PIN);
if (WATER_LEVEL_VALUE<=100){
oled.clearDisplay();
oled.setCursor(0,10);
oled.print("WATER LEVEL LOW");
oled.display();
}
else{
oled.clearDisplay();
Serial.println(key);
if (key=='d'){
oled.clearDisplay(); // clear display
oled.setCursor(0, 10); // position to display
SOIL_MOISTURE_VALUE=analogRead(SOIL_MOISTURE);
oled.println("Soil moisture: "); // text to display
oled.print(SOIL_MOISTURE_VALUE);
oled.print(" bars");
oled.display();
}
if (key == 'b'){
oled.clearDisplay();
oled.setCursor(0,10);
WATER_LEVEL_VALUE = analogRead(WATER_LEVEL_PIN);
oled.println("pump water level: ");
oled.print(WATER_LEVEL_VALUE);
oled.print(" cm");
oled.display();
}
if (key == 'c'){
Serial.println("heloooo");
lt_lvl = analogRead(PHOTO_SENSOR_PIN);
oled.clearDisplay();
oled.setCursor(0,10);
oled.println("Light level: ");
oled.print(lt_lvl);
oled.print(" lux");
oled.display();
}
if (key == 'a'){
if (distance<=0.5){
oled.clearDisplay();
oled.setCursor(0,10);
oled.println("Ready for snipping");
oled.display();
}
else{
oled.clearDisplay();
oled.setCursor(0,10);
oled.println("Not ready for snipping");
oled.display();
}
}
}
}