design of drowsiness alarm using infrared sensor as …

48
DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS MEAN OF ACCIDENT PREVENTION A final project report presented to the Faculty of Engineering By Hamim Maulana 002201400019 in partial fulfillment of the requirements of the degree Bachelor of Science in Electrical Engineering President University February 2018

Upload: others

Post on 07-Apr-2022

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS MEAN OF ACCIDENT PREVENTION

A final project report

presented to

the Faculty of Engineering

By

Hamim Maulana

002201400019

in partial fulfillment

of the requirements of the degree

Bachelor of Science in Electrical Engineering

President University

February 2018

Page 2: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

ii President University

DECLARATION OF ORIGINALITY

I declare that this final project report, entitled “Design of Drowsiness Alarm Using Infrared

Sensor as Mean of Accident Prevention” is my own original piece of work. to the best of

my knowledge and belief, has not been submitted, either in whole or in part, to another

university to obtain a degree. All sources that are quoted or referred to are truly declared.

Page 3: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

iii President University

APPROVAL PAGE

DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS MEAN OF ACCIDENT PREVENTION

By

Hamim Maulana 002201400019

Approved by

Page 4: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

iv President University

ACKNOWLEDGEMENT

“If you cannot stand the fatigue of study, you will feel the poignant of stupidity” – Imam

Syafi’i.

In the name of Allah SWT, Lord of the worlds who has blessed the author in completing

this thesis. Sholawat and salam are given upon The Greatest Man in History, the prophet

Muhammad SAW who has taken us to the way of truth and brought us the true light of life.

This thesis is dedicated to my support system, for my parents, my source of motivation,

Mrs Sutinah Humaedi and Mr Nawari Almasrur A. Yahya for the love and unconditional

support. them. My beloved brothers, Abdul Haris Faisal and Asyifa Ahmad for their

endless love, this thesis cannot be completed without them.

I would to express my sincere appreciation gratitude to my final project supervisor Dr.-Ing.

Erwin Sitompul, S.T., M.Sc. for the continuous support for my work, for his patience,

advice, motivation and deep knowledge. His guidance helped me to finish this thesis.

Besides my Supervisor, I would like to thank for all the Electrical Engineering lecturers, A.

Suhartomo, M.Eng.Sc., M.M., Ph.D., Joni Wellman Simatupang, S.T., Ir. Adhi Wijaya,

Mia Galina, M.T., Carolus Kaswandi, M.Sc., Arthur Parsaoran Silitonga, B.Sc., M.Sc.

Last but not least for all my classmates, Electrical Engineering 2014, You guys are really

energizing me.

Cikarang, February 2018

Hamim Maulana

Page 5: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

v President University

APPROVAL FOR SCIENTIFIC PUBLICATION

I hereby, for the purpose of development of science and technology, certify and approve to

give President University a non-exclusive royalty-free right upon my final project report

with the title:

DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS MEAN OF ACCIDENT PREVENTION

along with the related software or hardware prototype (if needed). With this non-exclusive

royalty-free right, President University is entitled to conserve, to convert, to manage in a

database, to maintain, and to publish my final project report. These are to be done with the

obligation from President University to mention my name as the copyright owner of my

final project report.

Page 6: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

vi President University

ABSTRACT

Driver drowsiness is one of the common reasons for most of vehicle accidents. The purpose of this final project is to make an affordable drowsiness alarm for accident prevention. A system is created to detect fatigue symptoms of the driver to avoid accidents. It involves detection of eyelid position whether it is open or closed. An IR transmitter is used to transmit the infrared rays to the driver’s eyelid. An IR receiver is used to receive the reflected infrared rays from the eyelid. When drowsiness is detected, an alarm will be activated. The alarm will be deactivated only after the driver pushes a correct button sequence. Every drowsiness event is also recorded to a smartphone via Bluetooth connection. Keywords: eyelid detector, deactivator, Arduino Nano, Atmega328P, IR sensor, drowsiness, button combination, accident prevention.

Page 7: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

vii President University

TABLE OF CONTENT

DECLARATION OF ORIGINALITY .................................................................................. ii

APPROVAL PAGE ............................................................................................................. iii ACKNOWLEDGEMENT .................................................................................................... iv

APPROVAL FOR SCIENTIFIC PUBLICATION ............................................................... v

ABSTRACT ......................................................................................................................... vi

TABLE OF CONTENT ...................................................................................................... vii LIST OF FIGURES .............................................................................................................. ix

LIST OF TABLES ................................................................................................................ x

CHAPTER 1 INTRODUCTION ........................................................................................... 1

1.1. Background ................................................................................................................. 1

1.2. Problem Statement ...................................................................................................... 1

1.3. Final Project Objectives.............................................................................................. 1

1.4. Final Project Scope and Limitations ........................................................................... 2

1.5. Final Project Outline ................................................................................................... 2

CHAPTER 2 DESIGN SPECIFICATION ............................................................................ 3

2.1. Basic Model of the System ......................................................................................... 3

2.2. Software ...................................................................................................................... 3

Arduino IDE 1.8.5 ......................................................................................................... 3

2.3 Hardware ..................................................................................................................... 4

2.3.1 Googles with Attached IR Sensor ........................................................................ 4

2.3.2 Arduino Nano V3 ................................................................................................. 5

2.3.3. Piezoelectric Buzzer ............................................................................................ 7

2.3.4. nRF24L01 ............................................................................................................ 8

2.3.5. NO Push buttons .................................................................................................. 9

2.3.6. Real Time Module RTC DS1307 ...................................................................... 10

2.3.7. Bluetooth Module HC-05 .................................................................................. 11

2.4. Interface .................................................................................................................... 12

CHAPTER 3 DESIGN IMPLEMENTATIONS ................................................................ 14

3.1. Introduction .............................................................................................................. 14

3.2. Flowchart of the System ........................................................................................... 14

Page 8: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

viii President University

3.3. Hardware Implementations ...................................................................................... 16

3.3.1. Eyelid Detector Main Box Design and Hardware ............................................ 16

3.3.2. Deactivator Box Design and Hardware ............................................................. 18

3.4. Software Implementation ......................................................................................... 19

3.4.1. Eyelid Detector .................................................................................................. 20

3.4.2. Deactivator......................................................................................................... 22

CHAPTER 4 RESULTS AND DISCUSSIONS ................................................................ 25

4.1. Results ...................................................................................................................... 25

4.1.1. Eyelid Detector .................................................................................................. 25

4.1.2. Deactivator......................................................................................................... 27

4.1.3. Android Interface ............................................................................................... 28

4.1.4. Items Price ......................................................................................................... 28

4.2. Measurements ........................................................................................................... 29

4.2.1. Response Time of the Buzzer ............................................................................ 29

4.2.2. Response Time of Radio Transceiver ................ Error! Bookmark not defined. 4.2.3. Maximum Distance of Radio Transceiver ......................................................... 29

4.3. Strengths and Weaknesses ........................................................................................ 30

4.3.1. Strengths ............................................................................................................ 30

4.3.2. Weaknesses ........................................................................................................ 30

CHAPTER 5 CONCLUSIONS AND RECOMENDATIONS ........................................... 31

5.1. Conclusions .............................................................................................................. 31

5.2. Future Development ................................................................................................. 31

REFERENCES .................................................................................................................... 32

APPENDIX A Program Code ............................................................................................. 33

A.1. Eyelid Detector ........................................................................................................ 33

A.2. Deactivator ............................................................................................................... 36

Page 9: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

ix President University

LIST OF FIGURES

Figure 2.1. Block Diagram of the system .............................................................................. 3

Figure 2.2. Arduino IDE 1.8.5 .............................................................................................. 4

Figure 2.3. Googles with attached IR sensor ........................................................................ 5

Figure 2.4. Arduino Nano V3 ............................................................................................... 5

Figure 2.5. Piezoelectric buzzer ........................................................................................... 8

Figure 2.6. nRF24L01 .......................................................................................................... 8

Figure 2.7. NO Push buttons .............................................................................................. 10

Figure 2.8. RTC DS1307 .................................................................................................... 10

Figure 2.9. Bluetooth HC-05 .............................................................................................. 11

Figure 2.10. Android Application ...................................................................................... 13

Figure 3.1. Flowchart of the system ................................................................................... 15

Figure 3.2. Eyelid detector main box design ...................................................................... 16

Figure 3.3. Eyelid detector schematic ................................................................................ 17

Figure 3.4. Deactivator box design .................................................................................... 18

Figure 3.5. Deactivator schematic ...................................................................................... 19

Figure 4.1. Corner view of the googles ............................................................................. 25

Figure 4.2. Eyelid detector main box ................................................................................. 25

Figure 4.3. The author wearing the googles ....................................................................... 26

Figure 4.4. LED is on when eyes are closed ...................................................................... 26

Figure 4.5. Front view of deactivator box .......................................................................... 27

Figure 4.6. Visualization of the deactivator buttons .......................................................... 28

Figure 4.7. Android apps interface ..................................................................................... 28

Page 10: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

x President University

LIST OF TABLES

Table 2.1. Arduino Nano Specification ................................................................................ 6

Table 3.1. Eyelid Detector Pin Configuration ..................................................................... 17

Table 3.2. Deactivator Pin Configuration .......................................................................... 19

Table 3.3. Eyelid Detector Code ........................................................................................ 19

Table 3.4. Deactivator Code ............................................................................................... 21

Table 4.1. Price Table ......................................................................................................... 28

Table 4.2. Buzzer Response Time ...................................................................................... 29

Table 4.3. Maximum Distance of the Transmission ........................................................... 30

Page 11: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

1 President University

CHAPTER 1

INTRODUCTION

1.1. Background

One of the top causes of car accident is drowsy driving [1]. Driver drowsiness is one of the

common reasons for most of vehicle accidents. Nowadays, people have become more

aware of car accident [2]. The impact of the drowsiness while driving is huge. It brings

traffic accident which leads to material loss, even live losses [2]. There are many studies

have been carried out to build an automation system that helps human to keep safety

during driving, such as distance sensor, blind-spot assist, etc. The author is motivated to

provide a cheap and easy to build device. The purpose is to design a system to detect

fatigue symptoms of a driver in order to avoid accidents.

1.2. Problem Statement

Author concerns to reduce the possibility of a car accident which is caused by the driver

drowsiness. Therefore, a prototype of drowsiness alarm system is one of the best solution.

However, when doing this final project, there are some issues that need to be addressed:

1. How to design a system which can detect the drowsiness symptom?

2. How to make a compact, affordable, and low power device to prevent an accident

because of drowsiness?

1.3. Final Project Objectives

This final project is expected to achieve the following objectives:

1. Designing a low power and low-cost prototype of drowsiness alarm system

using Arduino Nano as the microcontroller.

2. Being able to detect eyelid position using IR sensor.

3. Making a wireless deactivator using nRF24L01 modem as a radio

communication.

4. Being able to send string and time to an android device via Bluetooth using HC-

05 module and RTC DS1307 module.

Page 12: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

2 President University

1.4. Final Project Scope and Limitations

The final project will be conducted under the following scopes:

1. The end product needs adjustment first before usage to make sure it properly

works.

2. The condition of drowsiness is eyes remain closed for 5 seconds.

3. The hardware includes two Arduino Nano and IR sensor, radio communications

modem nRF24L01, Bluetooth HC-05 and real time clock module RTC DS1307.

4. The programming is based on Arduino IDE v.1.8.5.

In conducting this research, there are several limitations to be considered:

1. The author uses googles to attach the eyelid detector position sensor.

2. The eyelid detector has detection range between 2 – 4 centimeters.

1.5. Final Project Outline

This final report consists of five chapters and is outlined as follows:

Chapter I: Introduction. This chapter consist of Final Project Background, Problem

Statement, Final Project Objectives, Final Project Scopes and Limitations, and Final

Report Outline.

Chapter II: Methods. This chapter will explain about human eye symptoms during drowsy.

Also, this part will describe the software and hardware used for the final project together

with their feature.

Chapter III: Design and Implementation. This chapter will consist of simulation

preparation, simulation execution, and the results comprehensively.

Chapter IV: Project Result and Discussion. This chapter will consist of analysis of the

result of the simulation to define the strengths and weakness of the system.

Chapter V: Conclusion and Recommendation. This chapter will resume the whole content

of the final project. The recommendations are needed to summarize improvement and

development that possible for the future of the final project.

Page 13: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

3 President University

CHAPTER 2

DESIGN SPECIFICATION

2.1. Block Diagram of the System

Figure 2.1. Block Diagram of the system

The block diagram for this final project is shown in Figure 2.1 The role of IR sensor is to

provide the signaling from eyelid position. The signal from the IR sensor will go to the

Arduino digital pin, processed to become an output to the buzzer.

The buzzer will not be easily deactivated. The driver has to be push the buttons with

specific button combination. The purpose of this method is to validate driver’s

consciousness. If the driver presses a correct sequence, the buzzer will be deactivated.

Otherwise, it will keep sounding continuously.

2.2. Software

Arduino IDE 1.8.5

The compiler software used for Arduino is Arduino IDE (Integrated Development

Environment) 1.8.5. It uses C language which is very user friendly [3]. The Arduino IDE is

Page 14: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

4 President University

used for programming and debugging the Arduino Nano in this project. The user interface

can be seen in the Figure 2.2.

Figure 2.2. Arduino IDE 1.8.5

2.3. Hardware

2.3.1 Googles with Attached IR Sensor

IR Sensors work by using a specific light sensor to detect a selected light wavelength in

the Infra-Red (IR) spectrum [4]. When an object is within a certain reference distance, the

light from the LED bounces off the object and comes back into the light sensor.

Page 15: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

5 President University

Figure 2.3. Googles with attached IR sensor

The author uses googles to attach the IR sensor, as can be seen in Figure 2.3 above. The

aim of attaching the IR sensor to the googles is to make the driver able to move the

driver’s head while the IR sensor still can detect the eyelid position.

In this project, the author uses digital IR Sensor Active High. It only has one digital output.

The value of the reference distance is to be set manually using a built-in potentiometer.

This is the reason why the device needs a manual adjustment for every user.

2.3.2 Arduino Nano V3

The Arduino Nano V3 is a small, complete, and breadboard-friendly board based on the

ATmega328 (Arduino Nano 3.0) as can be seen in Figure 2.4. It has 14 digital input/output

pins (of which 6 can be used as PWM outputs), 8 analog inputs, a 16 MHz quartz crystal,

an ICSP header and a reset button. Arduino Nano V3 has supported SRAM (Static

Random-Access Memory), USB Jack mini type B, EEPROM (Electrically Erasable

Programmable Read Only Memory) and Flash memory [5].

Page 16: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

6 President University

Figure 2.4. Arduino Nano V3

The full technical specification of Arduino Nano V3 is written in the Table 2.1 below:

Table 2.1. Arduino Nano Specification

Microcontroller ATmega328P Architecture AVR Operating Voltage 5 V Input Voltage 7V – 12V Flash Memory 32 KB of which 2 KB used by bootloader SRAM 2 KB Clock Speed 16 MHz Analog I/O Pins 8 EEPROM 1 KB DC Current per I/O Pins 40 mA (I/O Pins) Input Voltage 7-12 V Digital I/O Pins 14 PWM Output 6 Power Consumption 19 mA PCB Size 18 x 45 mm Weight 7 g Dimensions 0.73" x 1.70"

For further explanation, here are the pins configurations based on its classification:

a. Power pins

Power pins is a pin for powering external item, or to powering other pins. For

example, to make a push button, nRF24L01 etc. It has to voltage option, 5 V, 3.3 V

and ground.

b. Digital pins

It is a pin which can receive and sending signal by digital value. It uses binary

value which is 1 and 0. 1 for HIGH, and 0 for LOW. The meaning of High is when

the voltage reaches high reference voltage of the external module or pins, for

Page 17: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

7 President University

example IR sensor which have 3.3 V voltage, the condition of High is when the

voltage reaches 2.1 V, below it will be LOW or valued 0. There are 14 pins on

Arduino Nano it starts from pin 0 to 13. In digital pins it also has PWM (Pulse

width Modulation), it is a digital output which its duty cycle can be set, varying

from 0% to 100%, the output is similar to analog. The PWM pin marked with

symbol ~.

c. Analog pins

It is a pin which can receive and sending analog value. The voltage can be varying

from 0 V to 5 V. There are 8 analog pins on Arduino Nano which is A0, A1, A2,

A3, A4, A5, A6, A7.

d. IOREF pin

It is a pin which provides the voltage reference with which microcontroller

operates.

e. AREF pin

It is a pin that provides reference voltage for the analog inputs by using

analogReference() in the Arduino IDE.

f. LED

There are 4 built-in LED in Arduino Nano board. There are ON LED, RX LED,

TX LED, and L LED. The ON LED is the indicator if there is voltage in to the

board. The X LED is indicator if the Arduino receive external signal (outside of

Arduino), for example when the Arduino receive message from another Arduino,

the RX LED will flip flop. The TX LED is the indicator if the Arduino transmit

signal to the external device or module, for example when using Serial Monitor in

the Arduino IDE, the TX LED will flip flop. L LED is built-in LED which

connected to the pin 13.

g. Reset button

This button is to start over an Arduino program from beginning.

h. RXD and TXD pin

RXD pin is pin 0, the pin itself means receive TTL, serial data. TXD pin is pin 1, it

is for transmit TTL serial data. Usually these pins used for Bluetooth module.

2.3.3. Piezoelectric Buzzer

A buzzer is an audio signaling device. Typical uses of buzzers include alarm

devices, timers, and confirmation of user input. A buzzer consists of a coil mounted on a

Page 18: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

8 President University

diaphragm [6]. The coil current flows through an electromagnetic coil. Diaphragm will be

attracted in or out, depending on the direction of current and polarity of the magnet.

Because the coil is installed on a diaphragm, any movement of the coil will move the

diaphragms back and forth. This makes the air vibrate which will produce sound. The

buzzer is shown in Figure 2.5.

Figure 2.5. Piezoelectric Buzzer

2.3.4. nRF24L01

This project requires two nRF24L01, each for each Arduino Nano. The two nRF24L01

modems are used as the communication tools for the two Arduino Nano. The item is

shown in Figure 2.6.

Figure 2.6. nRF24L01

The nRF24L01 is a single chip 2.4 GHz transceiver with an embedded baseband protocol

engine (Enhanced ShockBurst™), designed for ultra-low power wireless applications. The

nRF24L01 is designed for operation in the world-wide ISM frequency band at 2.400 -

Page 19: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

9 President University

2.483 GHz. An MCU (microcontroller) and very few external passive components are

needed to design a radio system with the nRF24L01 [7].

The nRF24L01 is configured and operated through a Serial Peripheral Interface (SPI).

Through this interface the register map is available. The register map contains all

configuration registers in the nRF24L01 and is accessible in all operation modes of the

chip.

This chip enables one-way communication between two Arduino, transmitter to receiver.

For the purpose of sending signal from one Arduino to another. For further explanation,

here are the pins configurations:

a. CE (Chip Enable)

CE is a digital input to activates RX or TX mode.

b. CSN

Pin to establish SPI Chip Selection.

c. SCK

Pin for clock, oscilation between High and Low state.

d. MOSI

SPI Slave Data Input, its function is to specify the radio address.

e. MISO

SPI Slave Data Output, with tri-state option.

f. IRQ

Maskable interrupt pin. Is a hardware interrupt that may be ignored by setting a bit

in an interrupt mask register's (IMR) bit-mask. Active low.

g. VCC, GND

DC power is provided to the device on these pins. VCC is the +3.3 V input. GND

is the ground.

2.3.5. Normally Open Push buttons

A push button is a momentary switch which causes a temporary change in the state of

an electrical circuit only while the button is pressed. It plays a role as an input to deactivate

the buzzer. This project will use a normally open push button which can be seen in Figure

2.7.

Page 20: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

10 President University

Figure 2.7. Normally Open Push buttons

2.3.6. Real Time Module RTC DS1307

The DS1307 real time clock (RTC) IC is an 8-pin device using an I2C interface. Address

and data are transferred serially via a 2-wire, bi-directional bus [8] which means one wire

communication. The clock/calendar provides seconds, minutes, hours, day, date, month

and year qualified data. The item can be seen in Figure 2.8.

The main advantage of RTC is that it has an arrangement of battery backup which keeps

the clock/calendar running even if there is power failure.

Figure 2.8. RTC DS1307

For further explanation, here are the pins configurations:

a. SCL (Serial Clock Input) pin

SCL is used to synchronize data movement on the serial interface.

b. SDA (Serial Data Input/Output) pin

Page 21: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

11 President University

SDA is the input/output pin for the 2-wire serial interface. The SDA pin is open

drain which requires an external pullup resistor.

c. SQW/OUT (Square Wave/Output Driver)

When enabled, the SQWE bit set to 1, the SQW/OUT pin outputs one of four

square wave frequencies (1 Hz, 4 kHz, 8 kHz, 32 kHz). The SQW/OUT pin is open

drain and requires an external pull-up resistor.

d. VCC, GND

DC power is provided to the device on these pins. VCC is the +5 V input. GND is

the ground. When 5 V is applied within normal limits, the device is fully

accessible, and data can be written and read.

2.3.7. Bluetooth Module HC-05

Bluetooth HC-05 module is designed for transparent wireless serial connection setup. The

HC-05 Bluetooth Module can be used in a Master or Slave configuration. It is a solution

for affordable wireless communication. This serial port Bluetooth module is fully

qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3 Mbps Modulation with complete

2.4 GHz radio transceiver. It uses CSR Bluecore 04 External single chip Bluetooth system

with CMOS technology and with AFH (Adaptive Frequency Hopping Feature) [9]. The

item can be seen in Figure 2.9.

Figure 2.9. Bluetooth HC-05

For further explanation, here are the pins configurations:

a. EN

Is enable pin, when enable is pulled LOW, the module is disabled which means the

module will not turn on and it fails to communicate. When enable is left open or

Page 22: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

12 President University

connected to 3.3 V, the module is enabled i.e. the module remains on

and communication also takes place.

b. TX

TX is a communication pin using UART interface. This pin work as the

transmitter.

c. RX

RX is a communication pin using UART interface. This pin work as the receiver.

d. STATE

It acts as a status indicator. When the module is not connected to / paired with any

other Bluetooth device, signal goes Low. At this low state, the led flashes

continuously which denotes that the module is not paired with another device.

When this module is connected to/paired with any other Bluetooth device, the

signal goes High. At this high state, the led blinks with a constant delay say for

example 2 seconds delay which indicates that the module is paired.

e. VCC, GND

DC power is provided to the device on these pins. VCC is the +5 V input. GND is

the ground.

f. Button state

This is used to switch the module into AT command mode. To enable AT

command mode, press the button switch for a second. With the help of AT

commands, the user can change the parameters of this module but only when the

module is not paired with any other BT device. If the module is connected to any

other Bluetooth device, it starts to communicate with that device and fails to work

in AT command mode.

2.4. Interface

One of the objectives of this project is to make a record of the drowsy occurrence. To

achieve that objective, the author uses a java-based application that leverages the

Bluetooth feature on the smartphone to run serial communication via Bluetooth with the

drowsiness alarm system.

The author gets the serial communication application from Playstore, an Android

application provider. The strength of this application is that the application has a text-to-

speech feature [10].

Page 23: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

13 President University

This application is able to receives string sent by the Arduino Nano. It imports a real-time

string and time of drowsy occurrence. It also gives information of the current driver’s

condition. The interface can be seen in Figure 2.10.

Figure 2.10. Android Application

Page 24: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

14 President University

CHAPTER 3

DESIGN IMPLEMENTATIONS

3.1. Introduction

This chapter discusses about the hardware and software implementation of the project. In

this chapter the roles of consisting items will be described. Then, they are put as one into a

complete system.

Firstly, this chapter will begin with the explanation about the general work procedures of

the systems. The flowchart will be shown. This chapter also consists of the hardware

design and the final product design. The prototyping will be shown in schematic, including

the components required. At the last part, the software implementation will be discussed.

3.2. Flowchart of the System

The component used to detect the eyelid position is the IR sensor. From now on, it will be

referred to as eyelid detector. The eyelid detector is installed to googles. The output of the

eyelid detector is digital; with the state of High or Low. State High goes to the

microcontroller when the eye is closed, while Low state when the eye is open.

It is assumed that people who are drowsy will close their eyes for more than 5 seconds. So,

in this project, the author will define the symptom of the drowsiness as when the driver eye

is closed for more than 5 seconds. After the microcontroller receives the required signal

(eyelid detector is in High state for 5 seconds), the buzzer will turn to High state to

resuscitate the driver from drowsy condition. The buzzer will stay on until the driver

deactivates the buzzer manually. The process can be seen in the Figure 3.1.

Page 25: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

15 President University

Figure 3.1. Flowchart of the system

The eyelid detector is also designed to be capable to send the string and time as a record of

the drowsiness symptom occurrence to android devices.

The manual deactivation process is to validate the driver consciousness after drowsiness

symptom. Several buttons need to be pressed with unique sequence before the deactivation

signal will finally be sent. In this project, the author uses 3 buttons, with 5 sequential

presses.

Eyes close detected?

Deactivated?

Is it closed for more than 5 seconds?

LED On

Buzzer On andSend String to

Android

Buzzer Offand

Send String to Android

Yes

Yes

Yes

START

No

No

No

Page 26: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

16 President University

Since the deactivator is wireless, the author uses nRF24L01 radio communication modem

for connecting the system. The radio communication modem requires its own

microcontroller. The author uses an Arduino Nano. This device will be the transmitter. An

nRF24L01 radio communication modem is also required in order to receive the signal.

3.3. Hardware Implementations

3.3.1. Eyelid Detector Main Box Design and Hardware

Arduino Nano is the motherboard of the eyelid detector circuitry. The microcontroller

processes the input detected by the IR sensor then converts the input into an output to

activate the buzzer. The device is designed as compact as possible to achieve the driver

comfort. The IR sensor is attached to the googles, so that is able to detect the eyelid

position of the driver with various head orientations during driving. The main box design

can be seen in Figure 3.2.

Figure 3.2. Eyelid detector main box design

The connection of each item, including IR sensor, LED, buzzer, battery, RTC DS1307,

Bluetooth HC-05, and nRF24L01, in the eyelid detector system can be seen in Figure 3.3.

Page 27: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

17 President University

D1/TXD0/RX

RSTGND

D2D3D4D5D6D7D8D9

D10D11D12

VINGNDRST5VA7A6A5A4A3A2A1A0AREF3V3D13 G

ND

MO

SI5V RS

TSC

KM

ISO

LED Indicator

MISOSCK

CEGND

IRQMOSICSNVCC

GN

D5V SD

A

SCL

RXD

TXD

GN

D

VCC

OU

TG

ND

VCC

RTC DS1307

ARDUINO NANO V3

IR SENSOR

BUZZER

BLUETOOTHHC-05

nRF24L01

+-

9V220Ω

Figure 3.3. Eyelid detector schematic

For further details, the author attaches the pin configuration of the microcontroller and the

other items, such as module, sensor, modem in Table 3.1.

Table 3.1. Eyelid Detector Pin Configuration

No. Pin Number Function 1. TX1 RX of Bluetooth communication to android 2. RX0 TX of Bluetooth communication to android 3. D2 Eyelid sensor input 4. D4 LED indicator to define the device is work or not 5. D7 CE of nRF24L01 Wireless Radio Communication 6. D8 CSN of nRF24L01 Wireless Radio Communication

Page 28: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

18 President University

7. D9 Buzzer output 8. MISO (ICSP) MISO of nRF24L01 Wireless Radio Communication 9. MOSI (ICSP) MOSI of nRF24L01 Wireless Radio Communication 10. SCK (ICSP) SCK of nRF24L01 Wireless Radio Communication 11. A4 SDA of Real Time Clock 12. A5 SCL of Real Time Clock

3.3.2. Deactivator Box Design and Hardware

The deactivator uses its own microcontroller. The author uses Arduino Nano. Since the

deactivation is wireless, data transfer between two microcontrollers must be conducted.

This deactivator device will be acting as the signal transmitter. The nRF24L01 is a

wireless radio communication modem which acts as the bridge of two Arduinos. The

buildup of this device is simple and robust, with only input button and LED indicator to

define whether the device is sending the signal or not. The device is portable, so the driver

may put this device wherever the driver wants as long as within 15 m to the googles. The

design can be seen in Figure 3.4.

Figure 3.4. Deactivator box design

The deactivator schematic is shown in Figure 3.5.

Page 29: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

19 President University

D1/TXD0/RX

RSTGND

D2D3D4D5D6D7D8D9

D10D11D12

VINGNDRST5VA7A6A5A4A3A2A1A0AREF3V3D13 G

ND

MO

SI5V RS

TSC

KM

ISO

ARDUINO NANO V3 10kΩ 10kΩ 10kΩ

MISOSCK

CEGND

IRQMOSICSNVCC

nRF24L01

+-

9V

LED Indicator

220Ω

Figure 3.5. Deactivator schematic

The pin configurations of the device can be seen in table 3.2.

Table 3.2. Deactivator Pin Configuration

No. Pin Number Function 1. D2 Button input 2. D3 Button input 3. D4 Button input 4. D7 CE of nRF24L01 Wireless Radio Communication 5. D8 CSN of nRF24L01 Wireless Radio Communication 6. MISO (ICSP) MISO of nRF24L01 Wireless Radio Communication 7. MOSI (ICSP) MOSI of nRF24L01 Wireless Radio Communication 8. SCK (ICSP) SCK of nRF24L01 Wireless Radio Communication 9. MISO (ICSP) MISO of nRF24L01 Wireless Radio Communication

3.4. Software Implementation

This part contains the programing code of the microcontrollers including its description.

To implement the program to the microcontroller, the author uses Arduino IDE 1.8.5.

Page 30: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

20 President University

There are two main function in Arduino IDE. First is setup() function, which run once in

the beginning. The second is loop() function, which run continuously as long as the

Arduino is powered.

3.4.1. Eyelid Detector

In this section, the author will explain the interesting part of the program code in this project.

Table 3.3 explains the process to mark the initial time until reaching the desired final time,

in this case 5 seconds. It has two processes. First is to detect the state of the input. Seconds

is to check whether the input holds the state for 5 seconds or not, as can be seen in Table

3.3. The author uses a Timemark library in order to mark initial time and final hold time.

For the complete program code can be found in Appendix.

Table 3.3. Eyelid Detector Code

Coding Description while (steps == 0) digitalWrite(Buzzer, 0); if (buttonDebounce.expired()) bool currentState = digitalRead(IRsensor); if (currentState != IRState) IRState = currentState; if (IRState == HIGH) buttonHold.start(); else buttonHold.stop(); delay(100); else if (buttonHold.expired()) steps = 1; buttonHold.stop(); while (steps == 1) digitalWrite(Buzzer, 1); Serial.print("Driver is drowsy ");

//put as steps=0 when: //initially buzzer off //if reach 5 seconds //mark the true state //if the current state is different from IRstate //make it as current state //if the state is High //hold the initial time of high state //if no return to detect IRsensor state //if it has reach 5 seconds //mark as steps 1 //stop the time marking //when steps=1 //Buzzer on //Write on serial monitor

Page 31: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

21 President University

For the wireless communication, the author separates between the transmitter and the

receiver. In radio transmitter, the author should define an address to send the signal. In the

coding is referred to it as pipe, which refers to a specific channel, as written in Table 3.4.

Table 3.4. Radio Transmitter Program

Coding Description int msg[1]; RF24 radio(7, 8); const uint64_t pipe = 0xE8E8F0F0E1LL; void setup() radio.begin(); radio.setRetries(15, 15); radio.openWritingPipe(pipe); radio.stopListening(); void loop() if (Button combination is correct) msg[0] = 111; radio.write(msg, 1);

//declare radio message //puts CE at pin 7, CSN at pin 8 //Address of radio communication //Start the radio //retry the transmitting if fail 225 times (15x15) //call the pipe, the address of radio //set this as radio transmitter //if button combination are correct //radio transmitted

In the radio receiver, the author also put the same address as used in the transmitter. The

unique thing of the nRF24L01 modem is that it has no limited number of receivers as long

as it is in the range of the transmitters. In case the author puts a different address, the

signal can be transmitted but the receiver cannot receive the signal. The receiver program

can be seen in Table 3.5.

Table 3.5. Radio Receiver Program

Coding Description int msg[1]; RF24 radio(7, 8); const uint64_t pipe = 0xE8E8F0F0E1LL; void setup() Serial.begin(9600); radio.begin(); radio.openReadingPipe(1, pipe); radio.startListening(); void loop() if (radio.available()) radio.read(msg, 1); if (msg[0] == 111)

//declare radio message //puts CE at pin 7, CSN at pin 8 //address of radio communication //set baudrate //start the radio //open reading through the address //if radio available //if the radio receive signal

Page 32: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

22 President University

Serial.print("Radio signal received"); delay(10);

//write on serial monitor “Radio signal received”

3.4.2. Deactivator

Deactivation of the buzzer is conducted by using button combination. The purpose of this

is to validate the consciousness of the driver after drowsy occurrence. In the program code,

the author uses comparison to each button pressed. The program detects the sequences of

the button which is pressed. The particular code is written in the Table 3.5. For the

complete program code can be found in Appendix.

Table 3.6. Push Button Sequence Combination Code

Coding Description void loop() buttonPress = 0; combinationFind(button1, button3, button2, button3, button1); void combinationFind (int p1, int p2, int p3, int p4, int p5) prevResult = true; buttonPressed(p1, p2); buttonPressed(p2, p3); buttonPressed(p3, p4); buttonPressed(p4, p5); lastButtonPressed(p5); delay(10); void buttonPressed (int b1, int b2) if (b1 == b2) sameButton = true; sameButton = false; (prevResult == true) if (sameButton == true) if (digitalRead(b1) == HIGH) while (digitalRead(b1) != LOW) prevResult = false; previResult = false;

//read which button has pressed one another //reading each press //read the last presses //find out if the current button and the next button are the same //write sameButton as true (the current digit and next digit are the same) //write sameButton as false (the current digit and next digit are not the same) //if the two buttons are the same run this code //if the correct button is pressed //only move on to the next line when the current button is let go of, so that holding down the button won't register for the next digit of the code

Page 33: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

23 President University

delay(10); fail = true; break; else if (digitalRead(b1) == HIGH && digitalRead(b2) == LOW) while (digitalRead(b1) != LOW) prevResult = false; previResult = false; delay(10); fail = true; break; else if (digitalRead(b1) == LOW && digitalRead(b2) == HIGH) prevResult = false; previResult = false; fail = true; delay(100); break; prevResult = false; previResult = false; if (fail == false) prevResult = true; previResult = true; else prevResult = false; previResult = false; void lastButtonPressed(int b1) if (previResult == true) if (digitalRead(b1) == HIGH) delay(10); digitalWrite(LED, HIGH); delay(2000); digitalWrite(LED, LOW); fail = false; prevResult = true;

//delay 10 milliseconds so the button can be debounced //the result is fail //exit the while loop //check if the current button is being pressed and the other isn't, so that users can't push down on both buttons to pass //delay 10 milliseconds so the button can be debounced //the result is fail //exit the while loop //if the user presses the wrong button //the result is fail //delay 10 milliseconds so the button can be debounced //exit the while loop //if the user didn't fail to put in the right digit for the code //rewrite the values prevResult and previResult so the program knows the user inserted the right digit //if the user did fail to put in the right digit for the code //rewrite the values of prevResult and previResult so the program knows the user inserted the wrong digit //read the last button pressed //the output of the button combination //button combination succeeded //correct sequence

Page 34: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

24 President University

previResult = true; break;

//break here and ready to deactivate buzzer in the future drowsiness symptom occurrence

Page 35: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

25 President University

CHAPTER 4

RESULTS AND DISCUSSIONS

4.1. Results

The finished devices have meets the design objectives as stated in previous chapter. For

each device part, the eyelid detector and the deactivator are made to be light and compact.

The driver should not be bothered while using the device.

4.1.1. Eyelid Detector

Eyelid detector consists of two main components. First is googles as can be seen in Figure

4.1. The second is eyelid detector main box as can be seen in Figure 4.2.

Figure 4.1. Corner view of the googles

The body of the eyelid detector main box is built from a compact plastic box as can be

seen in Figure 4.2. The body is robust for daily usage, with the weight of around 70 g

including battery. The box contains the Arduino Nano, Bluetooth HC-05, RTC D1307,

nRF24L01, buzzer, and 9 V battery.

Page 36: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

26 President University

Figure 4.2. Eyelid detector main box

The googles is not really bothering when it is being used, as can be seen in Figure 4.3 the

author wearing the googles.

Figure 4.3. The author wearing the googles

The eyelid detector works properly, as can be seen in Figure 4.4. The LED is on while the

author’s eyes are closed.

Page 37: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

27 President University

Figure 4.4. LED is on when eyes are closed

4.1.2. Deactivator

Figure 4.5. Front view of deactivator box

The deactivator works correctly as expected. The push buttons are easy to press. The

device is portable. A driver may put the box at any place somewhere comfortable for him.

The weight of the box is around 50 g, including battery. It is still able to transmit signal in

the range 15 meter in open area. The buttons can be seen on Figure 4.5. from left to right,

button 1, button 2, button 3, and LED.

The deactivation sequence is button 1, button 3, button 2, button 3, button 1 (as reference

see Figure 4.6)

Page 38: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

28 President University

Figure 4.6. Visualization of the deactivator buttons

4.1.3. Android Interface

The communication between the eyelid detector and the android phone is well established.

The eyelid detector sends the drowsy occurrence time. It exports the information at the

same time as the occurrence of the drowsiness symptom. The interface can be seen in the

Figure 4.7.

Figure 4.7. Android apps interface

4.1.4. Items Price

To prove the affordability of the device, the author attaches the list of the items the author

required in the making of the project. The price is based on the what the author pays when

bought the items. The complete list of the price can be seen in Table 4.1.

B1 B2 B3 LED

Page 39: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

29 President University

Table 4.1. Price Table

No. Items Quantity Price 1. Arduino Nano 2 Rp. 76,000 2. IR sensor 1 Rp. 11,000 3. Buzzer 1 Rp. 5,000 4. Googles 1 Rp. 9,000 5. nRF24L01 modem 2 Rp. 30,000 6. Bluetooth HC-05 module 1 Rp. 50,000 7. RTC DS1307 1 Rp. 15,000 8. Mini Push button 3 Rp. 1,500 9. ABS Box 2 Rp. 15,000

10. nRF24L01 modem 2 Rp. 30,000 11. LED 2 Rp. 3,000

Total cost Rp. 245,500

4.2. Measurements

4.2.1. Response Time of the Buzzer

Response time is defined as the duration time for the buzzer to react after the device

receives the signal of drowsiness. Drowsiness is defined to be occurred when the driver’s

eyes are closed for more than 5 seconds. The response time of the buzzer is measured for

several experiments. The result can be seen in Table 4.2.

Table 4.2. Buzzer Response Time

No.

Drowsiness appear at (After detecting eyes are closed for more than 5

seconds)

Buzzer On at Time difference

1. 13 : 14 : 05 13 : 14 : 5.3 300 ms 2. 13 : 16 : 06 13 : 16 : 6.4 400 ms 3. 13 : 18 : 03 13 : 18 : 3.1 100 ms 4. 11 : 25 : 16.4 11 : 25 : 16.6 200 ms 5. 11 : 27 : 05 11 : 27 : 5.1 100 ms

In the table, the time difference is the time interval between the drowsiness detection and

the buzzer activation.

4.2.2. Maximum Distance of Radio Transceiver

The most important feature of transceiver modem is maximum range, as mean of proving

that the nRF24L01 modem is a reliable radio communication. The author also conducted

an observation as can be seen in Table 4.3.

Page 40: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

30 President University

Table 4.3. Maximum Distance of the Transmission

No. Location Distance Result 1. Open Space 3 meters Able 2. Open Space 15 meters Able

3. Room separated by two doors 8 meters Able

4. Open Space 16 meters Unable 5. Open Space 18 meters Unable

4.3. Strengths and Weaknesses

4.3.1. Strengths

1. The googles allow the sensor to detect the eyelid position in various head

orientations.

2. The overall system requires relatively low power to operate. The overall power

consumption is 1.04 watt.

3. Low cost device, as can be seen in Table 4.1 the total cost is Rp. 245,500.

4. The wireless radio communication has maximum transmission range until 15 m in

open area. There is no issue regarding this because in this project, since the

communication occurs within 1 meter.

5. Light weight, the overall weight is 120 g.

4.3.2. Weaknesses

1. Since the main sensor is attached to googles, it is still not practice for regular basis

usage.

2. The googles might be inconvenient for some people.

3. The shape of final product is not really ergonomic.

Page 41: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

31 President University

CHAPTER 5

CONCLUSIONS AND RECOMENDATIONS

5.1. Conclusions

Several conclusions can be stated after the completion of “Design of Drowsiness Alarm

Using Infrared Sensor as Mean of Accident Prevention” experiment, which are

1. Drowsiness alarm is successfully designed and constructed. The overall power

consumption is 1.04 watt. It also costed only Rp. 245,500.

2. The eyelid detector which uses IR sensor is successfully detects the eyelid position.

3. The wireless communication between the Arduino from the main box and the

Arduino from the deactivator box is successfully established within 15 m. The

communication with android device via Bluetooth enables the record of drowsiness

occurrence.

4. The device is successful to send string and time to the Android device.

5.2. Future Development This system focuses on bringing safety while driving. Further development is suggested

since there are many drowsiness symptoms which might appear during driving. This

project only uses eyes activity symptom as the signal. There are also other ways to detect

drowsiness far beyond the eyelid symptom itself appears, such as detecting human

behavior that might lead to drowsiness.

For further improvement, the author suggests some recommendations which are

1. Making a light and compact design of the googles. Since the weight of the googles

also lead to drowsiness symptom.

2. Using image processing to track the driver face, which means no need of googles

anymore.

3. Making a simpler system, where the eyelid sensor should be able to be attached

anywhere. By this, the device will not doing disturb driver’s vision while driving.

Page 42: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

32 President University

REFERENCES

[1] Serious Accidents, “Top Causes of Car Accidents” 2015. [On-line]. Available:

https://www.seriousaccidents.com/legal-advice/top-causes-of-car-accidents.

[2] Sleepiness at the Wheel, Autoroutes & Ouvrages Concedes and The French Institut

of Sleep and Vigilance, France, June 2013.

[3] M. Banzi, Getting Started with Arduino, B. Jepson, Ed., 2nd ed. Sebastopol, CA:

Make:Books, 2011.

[4] IR Sensor Datasheet– Single, Robosoft System, December 2009.

[5] Arduino Nano Manual, Arduino, June 2008.

[6] M. S. Vijaya, Piezoelectric Materials and Device Applications in Engineering and

Medical Sciences, Boca Raton, Florida: CRC Press, 2016, pp. 94-99.

[7] nRF24L01 Single Chip 2.4GHz Transceiver Product Specification, Nordic

Semiconductor, July 2007.

[8] DS1307 Datasheet, Dallas Semiconductor, June 2015.

[9] HC-05 Bluetooth Module, ITead Studio, June 2010.

[10] Nudge Me, “Talking Serial Monitor” 2016. [On-line] Available:

https://www.play.google.com/apps/developer/nudgeme.

Page 43: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

33 President University

APPENDIX A Program Code

A.1. Eyelid Detector

Coding Description #include <SPI.h> #include "nRF24L01.h" #include "RF24.h" #include <Timemark.h> #include <Wire.h> #include "RTClib.h" int msg[0]; RF24 radio(7, 8); const uint64_t pipe = 0xE8E8F0F0E1LL; int IRPin = 2, Buzzer = 9, check = 5; bool IRState; int steps = 0; Timemark buttonDebounce(40); Timemark buttonHold(5000); RTC_DS1307 rtc; void flipflop() digitalWrite(Buzzer, 1); delay(1000); digitalWrite(Buzzer, 0); delay(100); void timewrite() DateTime now = rtc.now(); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); void setup() Serial.begin(9600); radio.begin(); radio.openReadingPipe(1, pipe);

//library of SPI to establish interface of two microcontrollers //library of nRF24L01 as the radio communication which is one of the SPI communication //library of Timemark to make a time threshold //library of wire //library of the Real Time Clock //declare radio //puts CE at pin 7, CSN at pin 8 //address of radio communication //set pin function //set the IR sensor state as a true false value //initialize the steps. the author stated each process as steps //IRpin debounce set to 40 milliseconds //threshold of the IR state //start RTC function //function to make the Buzzer flipflop //function to write the time of the drowsy occurrence //set the baudrate to 9600 //start the radio communication //set the radio address

Page 44: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

34 President University

radio.startListening(); while (!Serial); if (! rtc.begin()) Serial.println("Couldn't find RTC"); while (1); if (! rtc.isrunning()) Serial.println("RTC is NOT running!"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); pinMode(IRPin, INPUT); pinMode(Buzzer, OUTPUT); pinMode(check, OUTPUT); IRState = digitalRead(IRPin); buttonDebounce.start(); void loop() DateTime now = rtc.now(); while (steps == 0) digitalWrite(Buzzer, 0); if (buttonDebounce.expired()) bool currentState = digitalRead(IRPin); if (currentState != IRState) IRState = currentState; if (IRState == HIGH) buttonHold.start(); digitalWrite(check, HIGH); else buttonHold.stop(); digitalWrite(check, LOW); delay(100); else if (buttonHold.expired()) steps = 1; buttonHold.stop(); Serial.print("Driver is drowsy at "); timewrite(); delay(10); while (steps == 1)

//radio modem is ready to receive signal //this is a function to check whether the RTC is running correctly or not //set each pin as input or output //declare IRstate as reading the IRPin //start the eyelid detector timemark //RTC runs continuously and ready to send the drowsy occurrence //put as steps=0 when: //initially buzzer off //if the IR debounce is expired //set currentState as reading the IRPin states //if the current state is different from IRstate //make it as current state //if the state is High //start the time marking of HIGH state //if no return to detect IRsensor state //if the HIGH state has reach 5 seconds //mark the steps as steps 1 //stop the time marking //send string to the Android device //send time to the Android device //when steps=1

Page 45: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

35 President University

flipflop(); if (radio.available()) radio.read(msg, 1); if (msg[0] == 111) steps = 0; Serial.print("Buzzer is deactivated at "); timewrite(); delay(10);

//buzzer on continuously //if radio modem receive signal from transmitter //read the message //if the message is the same as transmitter //return to the steps =0, which means ready to detects future drowsy occurrence //send string to the android device //send time to the Android device

Page 46: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

36 President University

A.2. Deactivator

Coding Description #include <SPI.h> #include "nRF24L01.h" #include "RF24.h" const int button1 = 4; const int button2 = 3; const int button3 = 2; const int LED = 5; boolean prevResult; boolean sameButton; boolean previResult; boolean fail; int buttonPress = 0; int msg[0]; RF24 radio(7, 8); const uint64_t pipe = 0xE8E8F0F0E1LL; void setup() Serial.begin(9600); pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(LED, OUTPUT); attachInterrupt(digitalPinToInterrupt (2), TheAntiMultiPress, FALLING); attachInterrupt(digitalPinToInterrupt(3), TheAntiMultiPress, FALLING); radio.begin(); radio.setRetries(5, 5); radio.openWritingPipe(pipe); radio.stopListening(); void loop() buttonPress = 0; combinationFind(button1, button3, button2, button3, button1);

//library of SPI to establish interface of two microcontrollers //library of nRF24L01 as the radio communication which is one of the SPI communication //define what pin button1 is at //define what pin button2 is at //define what pin button3 is at //define LED indicator pin is at // check if the user presses the previous button correctly //check if the user presses the current button same as previous button //check if the user presses the previous pin correctly (after prevResult) //check if the user failed to enter the correct sequence //initialize button presses from 0 //declare radio //puts CE at pin 7, CSN at pin 8 //address of radio communication //set baudrate //set button1 as input //set button2 as input //set button3 as input //set LED as output //run the function TheAntiMultiPress whenever the button1 goes from HIGH to LOW //run the function TheAntiMultiPress whenever the button2 goes from HIGH to LOW //starting the radio communication //sets how often modem will retry if recipient doesn't receive data (5x5=25 times) //sets the address of the receiver to which the program will send data //It switch the modem to data transmission mode //a void which runs which runs continuously //initialize the number of presses //the sequence to reach true

Page 47: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

37 President University

void combinationFind (int p1, int p2, int p3, int p4, int p5) prevResult = true; buttonPressed(p1, p2); buttonPressed(p2, p3); buttonPressed(p3, p4); buttonPressed(p4, p5); lastButtonPressed(p5); delay(10); void buttonPressed (int b1, int b2) if (b1 == b2) sameButton = true; else sameButton = false; if (prevResult == true) if (sameButton == true) if (digitalRead(b1) == HIGH) while (digitalRead(b1) != LOW) prevResult = false; previResult = false; delay(10); fail = true; break; else if (digitalRead(b1) == HIGH && digitalRead(b2) == LOW) while (digitalRead(b1) != LOW) prevResult = false; previResult = false; delay(10); fail = true; break; else if (digitalRead(b1) == LOW && digitalRead(b2) == HIGH) prevResult = false; previResult = false; fail = true; delay(10); break;

//a function to find sequence. The int p1 etc is declaring the button pressed //read which button has pressed after one another //read the last presses //delay 10 milliseconds so the button can be debounced //a function to compare two sequential presses //compare if the current button and the next button are the same //write sameButton as true (the current digit and next digit are the same) //write sameButton as false (the current digit and next digit are not the same) //if prevResult is correct then, //if the two buttons are the same run this code //if the correct button is pressed //only move on to the next line when the current button is let go of, so that holding down the button won't register for the next digit of the code //the prevResult press is false // the previResult is false //delay 10 milliseconds so the button can be debounced //the user presses an incorrect sequence //exit the while loop //check if the current button is being pressed and the other isn't, so that users can't push down on both buttons to pass //delay 10 milliseconds so the button can be debounced //exit the while loop //if the user presses the wrong button //the prevResult press is false //the previResult is false // the user presses an incorrect sequence //delay 10 milliseconds so the button can be

Page 48: DESIGN OF DROWSINESS ALARM USING INFRARED SENSOR AS …

38 President University

prevResult = false; previResult = false; if (fail == false) prevResult = true; previResult = true; else prevResult = false; previResult = false; void lastButtonPressed(int b1) if (previResult == true) if (digitalRead(b1) == HIGH) delay(10); msg[0] = 111; radio.write(msg, 1); digitalWrite(LED, HIGH); delay(2000); digitalWrite(LED, LOW); Serial.println("TO DARE IS TO DO"); fail = false; prevResult = true; previResult = true; break; void TheAntiMultiPress() buttonPress++; delay(10); if (buttonPress > 1) prevResult = false; previResult = false; fail = true; delay(10); buttonPress = 0;

debounced //exit the while loop //the prevResult press is false //the previResult is false //if the user didn't fail to put in the right digit for the code //rewrite the values prevResult and previResult so the program knows the user inserted the right digit //if the user did fail to put in the right digit for the code //rewrite the values prevResult and previResult so the program knows the user inserted the wrong digit //read the last button pessed //if the previResult is true //radio transmitting the signal //indicator of the device, if it succeeds to send signal, LED will On for 2 seconds //button combination succeeded //correct sequence //break here and ready to send in the future drowsy symptom occurrence //a function to avoid multiple press in the sequence //increment button press every time a button is let go of //delay 10 milliseconds so the button can be debounced //if a button is pressed more than 1 times then restart the code //change parameters (prevResult and PreviResult) so the program will know multiple press has occurred and will restart the code //declare as an incorrect sequence //reset the value of buttonPress. Means that the user must start over the sequence