-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeCode
103 lines (97 loc) · 2.22 KB
/
theCode
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
/*macro definition of water sensor and the buzzer*/
#define WATER_SENSOR 8
#define BUZZER 99
const int TouchPin=6;
const int ledPin=13;
#include <math.h>
int a;
float temperature;
int B=3975; //B value of the thermistor
float resistance;
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(4);
void setup()
{
AFMS.begin();
myMotor->setSpeed(225);
pins_init();
pinMode(TouchPin, INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
myTouch();
MyWater();
MyTemp();
}
void MyTemp()
{
a=analogRead(0);
resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ;
delay(1000);
Serial.print("Current temperature is ");
Serial.println(temperature*1.8+32);
}
void pins_init()
{
pinMode(WATER_SENSOR, INPUT);
pinMode(BUZZER, OUTPUT);
}
void MyWater()
{
if(isExposedToWater())
soundAlarm();
}
void myTouch()
{
int sensorValue = digitalRead(TouchPin);
if(sensorValue==1)
{
digitalWrite(ledPin,HIGH);
Serial.println("");
Serial.println("");
MyPumpIn();
int sensorValue = digitalRead(TouchPin);
}
if (sensorValue!=1)
{
digitalWrite(ledPin,LOW);
Serial.println("");
}
}
/************************************************************************/
/*Function: When the sensor is exposed to the water, the buzzer sounds */
/* for 2 seconds. */
void soundAlarm()
{
}
/************************************************************************/
/*Function: Determine whether the sensor is exposed to the water */
/*Parameter:-void */
/*Return: -boolean,if it is exposed to the water,it will return true. */
boolean isExposedToWater()
{
if(digitalRead(WATER_SENSOR) == LOW)
{
Serial.println("Water Has Been Detected");
delay(1000);
return true;
}
else
{
Serial.println("No Water Detected");
delay(1000);
return false;
}
}
void MyPumpIn()
{
myMotor->run(FORWARD);
delay(2000);
myMotor->run(RELEASE);
}