-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Part
More file actions
138 lines (107 loc) · 3.06 KB
/
Arduino_Part
File metadata and controls
138 lines (107 loc) · 3.06 KB
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
// Import libraries
#include <Arduino.h>
#include <U8x8lib.h>
// Make synonyms using #define. No semi-colons needed here.
#define MOSFET 2 // The MOSFET driver for the water pump on digital I/O 2
#define REDLED 4 // Big red LED on digital I/O 4
#define BUTTON 6 // Push button on digital I/O 6
#define MOISTURE A1
#define WET_THRESH 600 // below 600 is considered wet
#define BUZZER 5 // buzzer
// Creates an instance of the `U8X8_SSD1306_128X64_NONAME_HW_I2C` class
auto display = U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
// Set up variable for measuring time.
unsigned long elapsedTime = 0; // "global" variable as long integer, positive values only.
unsigned long startTime = 0; // "global" variable as long integer, positive values only.
int moistureValue = 0;
/* -------------- Initialize the Grove board ------------- */
void setup() {
pinMode(MOSFET, OUTPUT); // Sets the D2 pin (MOSFET + Pump) to output
pinMode(REDLED, OUTPUT); // Sets the D4 pin (LED) to output
pinMode(BUTTON, INPUT); // Sets the D6 pin (Button) to input
digitalWrite(MOSFET, LOW); // pump off
Serial.begin(9600);
display.begin(); // start up the OLED display
display.setFlipMode(0); // set to 1 or 0, depending on orientation of board
display.clearDisplay(); // blank display
display.setFont(u8x8_font_profont29_2x3_r); // set font
// Record initial time.
startTime = millis();
}
void loop(){
Water();
Java();
myfunction();
sendJava();
buzzer();
moistureValue = analogRead(MOISTURE);
//place the cursor at positon x = 0 , y= 0
display.setCursor(0,0);
display.print("A1: " + String(moistureValue));
}
// auto detecting moisture value and water
void Water(){
if (moistureValue <= WET_THRESH){
digitalWrite(MOSFET, LOW); // pumpo on
}
else {
digitalWrite(MOSFET, HIGH); // pumpo on
}
}
void sendJava(){
const auto value = analogRead(MOISTURE);
const byte data[] = {0, 0, highByte(value), lowByte(value)};
Serial.write(data, 4);
Serial.println();
}
// turn on the pump using java button
void Java(){
if (!Serial.available()) {
return;
}
const auto receivedData = Serial.read();
// format the data
char buf[16];
sprintf(buf, "%03d", receivedData);
if(receivedData<100) {
digitalWrite(MOSFET,LOW);
}
else if (receivedData == 255)
{
digitalWrite(MOSFET,HIGH);
delay(2000);
}
else {
digitalWrite(MOSFET,LOW);
}
}
void myfunction(){
if(!Serial.available()){
return;
}
const auto receivedData = Serial.read();
char buf[16];
sprintf(buf, "%03d", receivedData);
if(receivedData<100) {
digitalWrite(MOSFET,LOW);
}
else if (receivedData == 255)
{
digitalWrite(MOSFET,HIGH);
delay(2000);
}
else {
digitalWrite(MOSFET,LOW);
}
}
void buzzer(){ // buzzer on for certain value proportional to the slider values
if (!Serial.available()) {
return;}
const auto sliderData = Serial.read();
char buf [16];
sprintf(buf, "%03d", sliderData);
if(sliderData > 100){
analogWrite(BUZZER,HIGH);
// put your main code here, to run repeatedly:
}
}