-
Notifications
You must be signed in to change notification settings - Fork 0
/
handwashtimer.ino
145 lines (100 loc) · 3.18 KB
/
handwashtimer.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
//library code for lcd
#include <LiquidCrystal.h>
//setting up pins
const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//servo motor
#include <Servo.h>
Servo servo_9;
int pos = 0;
//servo motor end
//ultrasonic sensor pin
const int pingPin = 12;
//lcd
signed short seconds;
char timeline[16];
void setup()
{
servo_9.attach(13); //servo pin
Serial.begin(9600);
pinMode(11,OUTPUT);// led
//lcd initialize
lcd.begin(16, 2);
lcd.print("Time : ");
}
void loop()
{
long duration, cm;
//Ultrasonic start
// 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(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, 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(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// Print the distance
Serial.print("Distance: ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
// ultrasonic end
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
// the servo motor should activate when object is
//less than 30cm close to the sensor
if(cm < 30)
{
unsigned long previousMillis = 0; // will store last time LCD was updated
const long interval = 1000; // interval at which to blink (milliseconds)
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 180; pos += 1)
{
unsigned long forloop_Millis = millis();
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
//LCD TIMER START
if (forloop_Millis - previousMillis >= interval)
{
previousMillis = forloop_Millis;
lcd.setCursor(0, 1);
sprintf(timeline,"%0.2d secs", seconds);
lcd.print(timeline);
seconds += 1;
if(seconds == 21){seconds = 0;}
}
//led buld switch on
digitalWrite(11,HIGH);
// wait 20ms for servo to reach the position
delay(115);
}
//switch of the bulb
digitalWrite(11,LOW);
//moving servo back to zero
for (pos = 180; pos >= 0; pos -= 1)
{
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 20 ms for servo to reach the position
delay(20); // Wait for 15 millisecond(s)
}
} //if end
delay(100);
}//loop end
//FUNCTIONS
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 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 / 2;
}