-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource_code.ino
More file actions
154 lines (142 loc) · 3.32 KB
/
Copy pathsource_code.ino
File metadata and controls
154 lines (142 loc) · 3.32 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <AFMotor.h>
#include <LiquidCrystal.h>
// Create motor objects
AF_DCMotor motor1(1); //1 indicates the port(M1) of the shield used to connect motor
AF_DCMotor motor2(2);
// Define arduino port numbers used with variable names
int irPin=6;
int MQ3=9;
int blink=13;
int buzzer=A0; //A0 is an analog pin but can be used as a digital pin
// Set all sensors output to HIGH by default
int irsensor_output=HIGH;
int blink_output=HIGH;
int alcohol_output=HIGH;
// Extra flags and counter variables
int flag_1=0;
int flag_2=0;
int flag_3=0;
int count=0;
int stop=0;
// Set up the arduino ports used by LCD display
const int rs = 10, en = 11, d4 = 5, d5 = 1, d6 = 3, d7 = 2;
//Create a lcd object
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Code which is executed at the start
void setup()
{
//initialize all pins
pinMode(irPin,INPUT);
pinMode(MQ3,INPUT);
pinMode(blink,INPUT);
pinMode(buzzer,OUTPUT);
//start and stop both motors
motor1.setSpeed(200);
motor1.run(FORWARD);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(FORWARD);
motor2.run(RELEASE);
//initialize LCD
lcd.begin(16, 2);
lcd.print("Hey!!!");
}
void check_proximity(int* flag_1)
{
irsensor_output=digitalRead(irPin);//read IR sensor output
if(irsensor_output==LOW)//when object found set flag_1 to 1
*flag_1=1;
else
*flag_1=0;
}
void check_alcohol(int* flag_2)
{
alcohol_output=digitalRead(MQ3);//read MQ3 sensor output
if(*flag_2==0 && alcohol_output==LOW)
{
*flag_2=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alcohol Detected");
lcd.display();
}
else if(*flag_2==1 && alcohol_output==HIGH)
{
*flag_2=0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No Alcohol");
lcd.display();
}
}
void check_blink(int* flag_3)
{
blink_output=digitalRead(blink);//read eye blink sensor output
if(blink_output==HIGH)//increment count if eye is closed
{
count=count+1;
}
else
{
count=0;
*flag_3=0;
}
if(count==6000)//approx 3sec
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Fatigue Detected");
lcd.display();
*flag_3=1;
}
}
void loop()
{
lcd.display();
delay(500);
// Turn on motor
motor1.run(FORWARD);
motor2.run(FORWARD);
// Accelerate from zero to maximum speed
for (int i=0; i<255; i++)
{
motor1.setSpeed(i);
motor2.setSpeed(i);
delay(10);
}
while(1)//continously keep checking all sensors
{
lcd.display();
check_proximity(&flag_1);
if(stop==0 && flag_1==1) //if motor was running and just detected object
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Object Detected");
lcd.display();
stop=1; //flag indicating that motor stopped
motor1.run(RELEASE);
motor2.run(RELEASE);
}
if(stop==1 && flag_1==0)//if motor was released and no object is detected
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No Object");
lcd.display();
stop=0; //flag indicating motor is started
motor1.run(FORWARD);
motor2.run(FORWARD);
motor1.setSpeed(10);
motor2.setSpeed(10);
}
check_alcohol(&flag_2);
check_blink(&flag_3);
if(flag_3==1)//buzzer at 3000Hz if fatigue detected
tone(buzzer,3000);
else if (flag_2==1)//buzzer at 1000Hz if alcohol detected
tone(buzzer,1000);
else
noTone(buzzer);
}
}