-
Notifications
You must be signed in to change notification settings - Fork 0
/
toilet_vacant_indicator2.ino
140 lines (124 loc) · 3.1 KB
/
toilet_vacant_indicator2.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
/*
Harry's washroom vacant Projects
@ 2 Ultrasonic Sensors
This sketch reads four(4) ultrasonic
rangefinder and identified if the person
standing on designed area, if yes, it will
return the range and arduino will determined
if the door indicator will show red (full) or
green (vacant). for Covid projects
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection attached to ground
* SIG connection attached to digital pin 7
http://www.arduino.cc/en/Tutorial/Ping
This example code is in the public domain.
*/
#include <Adafruit_NeoPixel.h>
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
int sensor4 = 0;
float dist = 0;
int trigger;
int echo;
const int highMax = 200;
const int highMin = 1;
const int TrigDey = 1500;
// LED Pin
int LEDa1 = A1;
int LEDa2 = A2;
int LEDa3 = A3;
int LEDa4 = A4;
// NeoPixal
int NUMPIXELS = 12;
const int neoPin = A0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, neoPin, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(9600);
//LED indicator
pinMode(LEDa1,OUTPUT);
pinMode(LEDa2,OUTPUT);
pinMode(LEDa3,OUTPUT);
pinMode(LEDa4,OUTPUT);
digitalWrite(LEDa1, LOW);
digitalWrite(LEDa2, LOW);
digitalWrite(LEDa3, LOW);
digitalWrite(LEDa4, LOW);
//Ultrasonic Sensor
pinMode(3,OUTPUT);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(4,INPUT);
pinMode(7,OUTPUT);
pinMode(6,INPUT);
pinMode(9,OUTPUT);
pinMode(8,INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
pixels.begin();
sensor1 = readUltrasonicDistance(3, 2, LEDa1);
delay(500);
sensor2 = readUltrasonicDistance(5, 4, LEDa2);
//sensor3 = readUltrasonicDistance(7, 6, LEDa3);
//sensor4 = readUltrasonicDistance(9, 8, LEDa4);
Serial.print(sensor1);
Serial.print("cm , ");
Serial.print(sensor2);
Serial.println("cm , ");
//Serial.print(sensor3);
//Serial.print("cm , ");
//Serial.print(sensor4);
//Serial.println("cm , ");
//Outdoor Indicator
if(sensor1 > highMax || sensor2 > highMax
|| sensor3 > highMax || sensor4 > highMax)
{
delay(1500);
NeoPixels(1);
pixels.show();
}
else
{
NeoPixels(0);
pixels.show();
}
digitalWrite(LED_BUILTIN, LOW);
delay(TrigDey); // Wait for (X)ms
pixels.clear();
}
long readUltrasonicDistance(int trigger, int echo, int led)
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
dist=pulseIn(echo,HIGH)/58;
LED_SensorIndicator(dist, led);
return dist;
}
void NeoPixels(int colour)
{
if (colour == 1) {
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,255,0));
}
}
if (colour == 0) {
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(255,0,0));
}
}
}
void LED_SensorIndicator(int sonic, int led)
{
if(sonic < highMax && sonic > highMin)
{digitalWrite(led,HIGH);}
else
{digitalWrite(led,LOW);}
}