Alcohol Detector Using Pic16f877a and MQ-3 Sensor
Alcohol Detector Using Pic16f877a and MQ-3 Sensor
Bachelor of Science
in
with specialization in
by
June 2016
i
CERTIFICATE
This is to certify that the present work entitled Over Speed and Alcohol Detection
System for Vehicles is a bona fide record of work done by
from
Arba Minch University
ii
ACKNOWLEDGEMENT
First of all, we would like to thank God for his blessing and our family for their encouragement,
and we also want to express our deepest gratitude to our Advisors Dr.N. Ram Kumar and Dr.
Yadav for their unwavering support and guidance throughout this project. We also want to thank
Mr. Hilemariam for his friendly and selfless assistance. Our deepest appreciation also goes out to
Mr. Henok Ali for his material support. Next to this we want to extend our heartfelt gratitude to
Mr. Lelisa Adeba for his continuous help and excellent suggestions.
Finally, we want to thank Electrical and Computer Engineering department members who gave us
many needed support, encouragement and help throughout our project.
iii
ABSTRACT
The main aim of this project is to design a system for implementing an efficient over speed and
alcohol detection system for vehicles that will be useful to reduce car accidents.
Now a days, car accident reaches in an alarming rate. Accident occurs due to many reasons. From
those, most of accidents happen due to over speed and drunken driver problem. There are laws to
punish over speed and drunken drivers but this rules and regulations cannot have any system that
can reveal the speed status of the car and the driver alcoholic condition. So, there is a need of an
effective system to limit the speed of the vehicles and check the drunken drivers. These system
works when the vehicle moves beyond the allowed speed, it retrieve to a desired speed and when
the vehicle driven by a drunken person then the system automatically send a message to the nearest
traffic police station. Therefore the project members will design and implement the prototype of
this system.
iv
LIST OF TABLES
v
LIST OF FIGURES
vi
ABBREVIATIONS
vii
TABLE OF CONTENTS
CERTIFICATE ........................................................................................................................................... ii
ACKNOWLEDGEMENT ......................................................................................................................... iii
ABSTRACT ................................................................................................................................................ iv
LIST OF TABLES ...................................................................................................................................... v
LIST OF FIGURES ................................................................................................................................... vi
ABBREVIATIONS ................................................................................................................................... vii
CHAPTER 1 .............................................................................................................................................. 10
INTRODUCTION..................................................................................................................................... 10
1.1 Introduction ................................................................................................................................... 10
1.2 Problem statement ........................................................................................................................ 10
1.3 Project objective ............................................................................................................................ 10
1.4 Scope of the project ....................................................................................................................... 10
CHAPTER 2 .............................................................................................................................................. 12
LITERATURE REVIEW ........................................................................................................................ 12
2.1 Work review .................................................................................................................................. 12
2.2 Component review ........................................................................................................................ 14
2.2.1 Alcohol sensor (MQ-3) ...................................................................................................... 14
2.2.2 Color sensor (TCS3103).................................................................................................... 16
2.2.3 DC Motor ........................................................................................................................... 17
2.2.4 Motor Driver ..................................................................................................................... 17
2.2.5 Microcontroller ................................................................................................................. 18
2.2.6 LCD display ....................................................................................................................... 19
CHAPTER 3 .............................................................................................................................................. 20
METHODOLOGY ................................................................................................................................... 20
3.1 Introduction ................................................................................................................................... 20
3.2 Communication unit ..................................................................................................................... 20
3.2.1 Wireless Sensor Network.................................................................................................. 20
3.2.2 RS 232 Connector.............................................................................................................. 22
3.3 Overall system design ................................................................................................................... 23
3.4 Electronic hardware ..................................................................................................................... 24
3.5 Materials ........................................................................................................................................ 26
8
3.6 Software design ............................................................................................................................. 27
3.7 Computer software ....................................................................................................................... 29
3.8 Software Architecture................................................................................................................... 32
CHAPTER 4 .............................................................................................................................................. 33
RESULTS AND DISCUSSION ............................................................................................................... 33
4.1 Result.............................................................................................................................................. 33
4.2 Discussion....................................................................................................................................... 36
CHAPTER 5 .............................................................................................................................................. 37
CONCLUSION AND FUTURESCOPE OF WORK............................................................................. 37
5.1 Conclusion ..................................................................................................................................... 37
5.2 Limitation ...................................................................................................................................... 37
5.3 Future Scope of Work................................................................................................................... 37
REFERENCES .......................................................................................................................................... 38
APPENDIX ................................................................................................................................................ 39
9
CHAPTER 1
INTRODUCTION
1.1 Introduction
At this time, one of the major factors for loss of life, loss of infrastructures and materialsis a car
accident. Even though many quality roads are built, relatively latest car exists, different rules and
regulations are made statistically the car accidents are increasing year after year. The reasons for
these accidents are many failures to give way for pedestrians, over speed, influence of alcohol and
drug are the common and major ones. Starting from this point of view we designed a system which
can reduce the accidents caused by over speed and drunken driving.
1.2 Problem statement
In our world car accident increases from day to day because of many reasons. From those reasons
most car accidents occur due to drunken driving and over speed. Therefore this project will be a
solution to the problem by providing an embedded system, which installed in the vehicles.
Specific objectives:-
10
C– language for the Microcontroller and MySQL Server 2008 for the data base
Micro C and Proteous is used for simulation purpose.
11
CHAPTER 2
LITERATURE REVIEW
To design a system that can achieve the objective of the project some studies have been carried
out, which involving three main subtopics such as previous case study on related projects, studies
of hardware and software.
In Ankita Mishra [1] “Design of RF based speed control system for vehicles”,
The system has Smart Display controller meant for vehicle’s speed control and monitors the zones,
which can run-on an embedded system. Smart Display &Control (SDC) can be custom designed
to fit into a vehicle’s dashboard, and displays information on the vehicle. The project is composed
of two separate units: zone status transmitter unit and receiver (speed display and control) unit.
Once the information is received from the zones, the vehicle’s embedded unit automatically alerts
the driver, to reduce the speed according to the zone, it waits for few seconds, and otherwise
vehicle’s SDC unit automatically reduces the speed. To know the zone status they used RF
transmitter and receiver, but this is a lit bit expensive. So, instead we used a color sensor which is
cheaper than RF Tx/Rx.
In JiangpengDai [2] they are proposed “Mobile Phone Based Drunk Driving Detection”,
Early detection and alert of dangerous vehicle maneuvers typically related to drunk driving. The
entire solution requires only a mobile phone placed in vehicle and with accelerometer and
orientation sensor. A program installed on the mobile phone computes accelerations based on
sensor readings, and compares them with typical drunk driving patterns extracted from real driving
tests. Once any evidence of drunk driving is present, the mobile phone will automatically alert the
driver or call the police for help well before accident actually happens. They implement the
detection system on Android G1 phone and have it tested with different kinds of driving behaviors.
12
But they are designed a system for drunk driving detection only, so there is no any mechanism to
stop the car in this system, our project worked on this issue.
In K.Govindaraju [3] proposed “An Embedded Based Vehicle Speed Control System Using
Wireless Technology”,
The main objective of this system is to operate the vehicle in safe speed at critical zones. The base
station having the transmitter which is designed for Frequency Modulation (FM), the receiver part
is implemented in Vehicle. The ARM processor is implemented at receiver side, which receives
the critical frequency, and then it is activated in critical mode. Speed Control Driver (SCD) can be
custom designed to fit into a vehicle’s dashboard, and displays information on the vehicle. Once
the information is received, it automatically alerts the driver, to reduce the speed according to the
time and zone. This system only used to inform the speed limit of the road to the driver but if the
driver ignores the warning there is no system to stop him, so we are designed a system by using
RF communication to inform for the nearest traffic station.
The system has an alcohol sensor embedded on the steering of the car. Whenever the driver starts
ignition, the sensor measures the content of the alcohol in his breath and automatically switches
off the car if he is drunken. In this system the sensor delivers a current with a linear relationship
to the alcohol molecules from zero to very high concentration. The output of the sensor is fed to
the PIC microcontroller for comparison. If the measured value reaches the threshold, relay cut off
automatically and the buzzer produces sound. This alcohol detection system works only when the
driver starts ignition but the driver can start the car by other person (who is not drunken), our
project solves this problem whenever the driver is drinking alcohol, the alcohol sensor detects level
of alcohol concentration and if it is abnormal the system display and send the information to the
driver and traffic station respectively.
13
2.2 Component review
2.2.1 Alcohol sensor (MQ-3)
This alcohol gas sensor detects the concentration of alcohol gas in the air and outputs its reading
as an analog voltage. The concentration sensing range of 0.04 mg/L to 4 mg/L is suitable for
breathalyses.
Features:-
High sensitivity to alcohol and small sensitivity to Benzene.
Fast response and High sensitivity
Stable and long life
Operation Temperature: -10 to 70 degrees C
Heater consumption: less than 750mW
Simple drive circuit
Basically, MQ3 alcohol sensor has 6pins, the cover and the body. Even though it has 6 pins, you
can use only 4 of them. Two of them are for the heating system, which call H and the other 2 are
for connecting power and ground, which called A and B.
At the inside of the sensor, there is little tube. Basically, this tube is a heating system that is made
of aluminum oxide and tin dioxide and inside of it there are heater coils, which practically
produce the heat. 2 pins that called Pin H are connected to the heater coils and the other ones are
connected to the tube.
How does it work? The core system is the cube. As you can see in this cross-sectional view,
basically, it is an Alumina tube cover by SnO2, which is tin dioxide. And between them there is
an Aurum electrode, the black one. And also you can see how the wires are connected. So, why do
14
we need them? Basically, the alumina tube and the coils are the heating system, the yellow, brown
parts and the coils in the picture.
There is a straightforward formula used to calculate your blood alcohol content (BAC). Alcohol
content in a volume of breath or blood is expressed as mg/L (milligrams per liter). A 1% blood
alcohol content is 10g/L or 10,000mg/L. Since this level would almost certainly be fatal, let’s scale
this down from “dead” to “drunk”. A 0.1% BAC is 1000mg/L.
How does this relate to the breath? It turns out that there is a standard conversion from breath
alcohol content to BAC that is employed by commercial Breathalyzers. Breath and blood alcohol
content differ by a factor of 2100; that is, for every mg of alcohol in the breath, there are 2100mg
of alcohol in the blood. So, a person with BAC of 0.1% has 1000mg/L of alcohol in their blood
and 1000/2100 = 0.4762mg alcohol in their breath. So, our final formula for calculating BAC from
the alcohol measured in the breath is:
15
2.2.2 Color sensor (TCS3103)
The TCS3103 RGB Light-to-Voltage Converters combine three photodiodes with optical filters,
amplifiers, and gain-switching circuitry on a single chip. The output of each channel is a voltage
that is linearly proportional to light intensity (irradiance) on the corresponding photodiode element.
Each channel on the device consists of a photodiode with a red, green, or blue optical filter, a
current-to-voltage converter, and gain control elements. The voltage from each of the red, green,
and blue channels is output on the VOR, VOG, and VOB pins, respectively [9].
Features:-
The TCS3103 is intended for use in a variety of applications including color-point feedback
control in solid-state illumination and backlight systems, color adjustment in printers, process
controls, diagnostic systems, paper and product handling, toys, and games.
16
2.2.3 DC Motor
A DC motor is any of a class of electrical machines that converts direct current electrical power
into mechanical power. The most common types rely on the forces produced by magnetic fields.
Nearly all types of DC motors have some internal mechanism, either electromechanical or
electronic, to periodically change the direction of current flow in part of the motor. Most types
produce rotary motion; a linear motor directly produces force and motion in a straight line. DC
motors were the first type widely used, since they could be powered from existing direct-current
lighting power distribution systems. A DC motor’s speed can be controlled over a wide range,
using either a variable supply voltage or by changing the strength of current in its field windings.
Small DC motors are used in tools, toys, and appliances.
The universal motor can operate on direct current but is a lightweight motor used for portable
power tools and appliances. Larger DC motors are used in propulsion of electric vehicles, elevator
and hoists, or in drives for steel rolling mills. The advent of power electronics has made
replacement of DC motors with AC motors possible in many applications.
17
Figure 5-L293D Motor driver
2.2.5 Microcontroller
Microcontroller is a computer-on-a-chip. It is an inexpensive single-chip computer and sometimes
referred to as one-chip microcomputers, used to control a wide range of electrical and mechanical
appliances. The microcontroller existing on the encapsulated silver of silicon has features and
similarities to our standard Personal computers. The microcontroller is capable of storing and
running a program,
This powerful (200 nanosecond instruction execution) yet easy-to-program (only 35 single word
instructions) CMOS FLASH-based 8-bit microcontroller packs Microchip's powerful PIC
architecture into an 40 pin package and is upwards compatible with the PIC16C5X, PIC12CXXX
and PIC16C7X devices. The PIC16F877A features 256 bytes of EEPROM data memory, self-
programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter, 2
capture/compare/PWM functions, the synchronous serial port can be configured as either 3-wire
Serial Peripheral Interface (SPI) or the 2-wire Inter-Integrated Circuit (I²C) bus and a Universal
Asynchronous Receiver Transmitter (USART). All of these features make it ideal for more
advanced level A/D applications in automotive, industrial, appliances and consumer applications
[8].
18
Features:-
19
CHAPTER 3
METHODOLOGY
3.1 Introduction
The over speed and alcohol detection system consists of several parts (components) which are actuators,
processor, driver and communication unit. In addition to this, alcohol and color sensor are there to provide
feedback signal to the MCU. The alcohol (MQ3) and color sensor (TCS3103) are used to detect the
alcoholic concentration of the driver and the RGB colors painted on the road respectively. Then, the data
from sensors will be transmitting as inputs to the microcontroller. The microcontroller acted as a brain and
decided a proper command based on the inputs from the sensors. Then the outputs from the microcontroller
will be sending to the motor drive, RF module (or X-bee) and LCD. The motor drive is used to control the
movement of the motor which is motor rotate forward or backward, X-bee is used to transmit the appropriate
information to the traffic police station and the LCD is used to show the current alcoholic concentration
and speed status of the driver and the vehicle respectively.
RF module
An RF module (radio frequency module) is a (usually) small electronic device used to transmit
and/or receive radio signals between two devices. In an embedded system it is often desirable to
communicate with another device wirelessly. This wireless communication may be accomplished
through optical communication or through radio frequency (RF) communication. For many
applications the medium of choice is RF since it does not require line of sight. RF communications
incorporate a transmitter and/or receiver. RF modules may comply with a defined protocol for RF
20
communications such as ZigBee, Bluetooth low energy, or Wi-Fi, or they may implement a
proprietary protocol. For our project we used ZigBee sensor to transfer data and communicate.
ZigBee and its underlying 802.15.4 standard provide networks with two kinds of devices – full
function devices (FFDs) and reduced function devices (RFDs). All must have 64-bit IEEE
addresses, although short 16-bit addresses can be allocated to reduce packet size. FFDs may be
network coordinators or routers, while the endpoint devices that interact with the physical world
are the RFDs. All ZigBee networks must have a coordinator. The coordinator sets up the network,
is aware of all its constituent nodes, handles and stores information, acts as a repository for security
keys, and manages the information transmitted and received within the network. Routers act as
intermediaries, channeling information between devices. The endpoint devices have limited
functionality in order to reduce system cost and complexity. They are also the cheapest devices to
manufacture. They require limited memory and usually only interact with the physical world. They
have just enough capability to talk to their parent nodes, be they the coordinator or routers.
21
Figure 8-Zigbee Module
22
Figure 9-RS232 connector
Usually all the digital IC’s works on TTL or CMOS voltage levels which cannot be used to
communicate over RS-232 protocol. So a voltage or level converter is needed which can convert
TTL to RS-232 and RS 232 to TTL voltage level.
Alcohol
sensor
PIC 16F877A PWM speed Motor
microcontroller control
Color sensor
Communication
Unit
23
3.4 Electronic hardware
3.4.1 Power supply
Switching mode
Switching regulators are used as replacements for linear regulators when higher efficiency, smaller
size or lighter weight are required. They are, however, more complicated; their switching currents
can cause electrical noise problems if not carefully suppressed, and simple designs may have a
poor power factor.
24
3.4.2 Microcontroller unit
Here PIC16F877A microcontroller is used to all the operation regarding the embedded system.
PORT A is used to receive analogue signal from alcohol sensor (MQ3), color sensor and speed
sensor, Pin 0 to 5 of PORTB is used to provide signal (data) to the LCD display, pin 3 of PORTC
is used to send signal to the LED, pin2 of PORTC is used to provide enabling signal to the motor
driver and finally pin 1 to 2 of PORTD is used to drive the DC motor through the motor driver.
It is on bread board circuit which is able to control two DC motors in both directions. The two
inputs are connected to PORTD.RD0 to PORTD.RD1 of the microcontroller and the two output
pins are connected to a DC motor to control the rotation of the motors. Enable pins is connected
directly to PORTC.RC2 in order to make it to operate in a PWM Mode. This will help to drive
motors with variable speed.
The most commonly used RS 232 level converter is MAX-232. This IC requires four external
capacitor (each 10uF) for its internal charge pumps which can generate RS232 voltage levels (-
10v and +10v) from 5v power supply. A MAX-232 is actually a dual driver /receiver and we are
just used one. The input pin is connected to PORTC.RC6 of the microcontroller and the output pin
is connected to the DB9 connector in order to transmit data and make it capable of half duplex
UART communication. On the Pc’s side, only three lines are connected (Tx, Rx and Ground) to
the COM port through a 9 pin connector [10].
25
Figure 12-Level convertor
3.5 Materials
The list of tools and components used during the project are stated below:-
26
3.6 Software design
Interface:-
Since all analogue sensors need ADC to convert its analogue value to the digital value, which is
connected to the microcontroller unit. So we must determine the cofactor value of the sensor. First
we set the output value of the sensor to its corresponding voltage value (i.e. at 0.04mg/l and 4mg/l
it gives 0v and 5v respectively). Since the microcontroller (PIC16F877A) has 10 bit ADC, we
calculated the respective digital value (0-1023) of the reading alcohol concentration.
Mathematically:
X = Volt
𝑋×10𝑚𝑣
Then Volt=
0.047916𝑚𝑔/𝑙
10𝑋
Volt = ( ) v.
47.9
Then at 5v = 1023
Volt = Val
𝑉𝑜𝑙𝑡×1023 10𝑋
Val =
5𝑣
= (( ) v× 1023)÷ 5𝑣
47.9
Although the output of the device can be connected to a comparator for simple threshold
measurements, or to another amplifier stage, most applications will require that the three channels
27
be connected to analog to digital converters (ADCs) so that the light and color information can be
processed by a microcontroller. In many cases, the outputs can be directly connected to the ADC
inputs. In some cases, for example if the light is modulated, it may be desirable to insert active or
passive low-pass filters on the channels prior to the ADC, to smooth out the modulation ripple that
would otherwise result in noisy measurements. The cutoff frequency of the filters would, of course,
depend upon the modulation frequency of the incoming light. With a pulse-width modulated
(PWM) input light signal, it may be possible to synchronize the ADC sampling on-time of the
PWM signal. In this case, a low-pass filter would not be required because the device output would
be sampled only during the on time of the input light source.
c) Communication protocol
There are two types of communication serial and parallel communication. But serial
communications are used in microcontroller based systems, mostly due to the scarcity of available
input/output pins. Besides for long distance communications, serial data transfer is more simple
and cost effective as the required hardware connections in the data link can be reduced to three
(i.e. Tx, Rx and Gnd).
There are two different types of serial communications: Synchronous and Asynchronous. The key
challenge in a serial data link is to maintain the synchronization between the transmitter and the
receiver. The asynchronous method uses a start and stop bit protocol to synchronize the two ends.
Each character byte is sent in a frame consisting of a start bit, followed by the character bits,
followed (optionally) by a parity bit, and finalized by one or more stop bits. The sender and
receiver have to be initialized to use the same data rate, number of data bits, and number of stop
bits.
In idle condition, the transmit output is at logic high. When the transmitter is ready to send a
character byte, it signals the receiver by pulling the transmitter line low for one clock period. This
is the start bit and it tells the receiver that a frame follows. The receiver reads the number of
character bits expected according to the adopted protocol until the line is pulled to logic high by
the transmitter (one more stop bits), and that is the end of the frame. The whole process is repeated
every time the transmitter has to send a character byte. The receiver is resynchronizes itself to the
transmitter every time the data is sent using the start bit.
28
The USART module inside the PIC16F877A microcontroller supports both types of serial
communications but it is the best suited for the asynchronous method. In asynchronous mode, RC6
acts as a data transmit (Tx) output and RC7 as data receive (Rx) input. A byte of serial data is sent
as a string of 10 bits; a start bit, eight data bits, and a stop bit [7].
Start Bit Bit0 Bit1 Bit 2 Bit 3 Bit4 Bit5 Bit6 Bit7 Stop Bit
The pc’s serial port (also known as COM port) uses the RS 232-C standard for serial
communication.
The LCD display units data pins D4 to D7 are interface to the port are 33 to 36 of the
microcontroller for data receiving and controlling process. The interfacing ports are getting the
data and displaying to the LCD display. RS-Reset pin interface to the port of 37 and EN-Enable
pin interface to the port of 38. The read and write pin are connecting to the ground. The reset
enable pin is used to display the receiving data.
29
Figure 14-Software architecture
The graphical user interface (GUI) is developed by using C# and the data base is by using MySQL
server 2008. The developed GUI application is capable of showing the plate number, the fault type
and street number. The data base developed is again capable of storing the record which includes
the plate number, fault type and street number and street name matching table. The GUI will store
those records in to the data base and at the same time retrieve the information so as to display the
most recent information to the end user.
30
Figure 16-GUI (Graphical user interface)
There is two part here in these GUI, one is to desplay the current fault information and another is
to desplay the most recent one. The could be three different states for each fault type normal,
warning and abnormal. But the information is not going to be desplayed unless at least there is one
fault. If there is one fault the remainig could be at normal or warning state. For instance the drive
plate number might be 34256, the alcohol fault might be abnormal and the care is at station which
means the speed fault is normal. Again there coul be speed fault leaving the alcohol fault normal.
But there is no probablity of having a speed fault after detecting an alcohol fault, since wenever
an alcohol fault is detected the system by it self will make the car to stop after a moment.
31
associated name of the street. And again the fault record table will record the plate number of the
car, fault type, fault condition and finally date and time.
32
CHAPTER 4
33
Simulated result of alcohol sensor
34
Figure 22-PWM for 80 Kph
4.2 Discussion
The MQ-3 Alcohol sensor is cheaper in cost and is as effective as other types of breath alcohol
detector. Furthermore, the system can be easily interfaced to a microcontroller as the output yield
is in voltages. When power dissipation comes in to concern, the sensor model has very low power
dissipation although it works with presence of heat because it only needs low voltage value in the
range of 3.3v to 5v as its input.
Sensitivity of the sensor is also relatively high compared to the other models. Although the sensor
is capable of detecting other substances and misinterprets it as alcohol, the sensitivity towards
alcohol is much higher and this makes it a very reliable alcohol detecting device.
36
CHAPTER 5
The project gives a system that can detect the amount of alcohol concentration of the driver in the
vehicle to certain range and activates the microcontroller to control the engine and other extra
information which acts as an indicator that enables other drivers to judge the driver has taken
alcohol and needs any assistant or help. In addition to this, the system can detect the over speeding
of the vehicle and took action by sending the appropriate information to the traffic police station
via RF module (Xbee) and those who have the authority takes necessary penalty to drunken and
over speed drivers to prevent from similar situation.
5.2 Limitation
Since the system needs a well-coordinated traffic police stations, therefore this might be hold back
the feasibility of the project in most Ethiopian road.
37
REFERENCES
1. Ankita Mishra, Jyoti Solanki , Harshala Bakshi, Priyanka SaxenaPranav Paranjpe. "Design of RF
based speed control system for vehicles." International Journal of Advanced Research in
Computer and Communication Engineering, October 2012: Vol. 1, Issue 8.
2. Jiangpeng Dai, Jin Teng, Xiaole Bai, Zhaohui Shen and Dong Xuan. "Mobile Phone Based Drunk
Driving Detection." February,2010.
4. Nimmy James, Aparna C,Teena P John. "Alcohol Detection System." International journal of
reasrch in computer and communication technology, January- 2014: Vol 3, Issue 1.
38
APPENDIX
C-code for the system
int count = 0;
int alco_level = 0;
char uart_rd;
#define ID 0x00
39
sbit LCD_EN_Direction at TRISB5_bit;
void main ( )
portb = 0x00;
portd = 0x00;
porta = 0x00;
portc = 0x00;
trisa = 0xff;
trisc = 0x00;
trisb = 0x00;
40
trisd = 0x00;
Delay_ms (100);
UART1_Init (9600);
Delay_ms (100);
adcon1 = 0x04;
Delay_ms (100);
pwm1_start ();
pwm1_init (1000);
/*delay_ms (100);
pwm2_start ( );
pwm2_init (1000);
delay_ms (100);*/
pwm1_start ( );
pwm1_init (1000);
delay_ms (100);
adc_init ();
delay_ms (100);
lcd_init ( );
delay_ms 100);
lcd_cmd (_lcd_cursor_off);
lcd_cmd (_lcd_clear);
delay_ms (2000);
lcd_cmd (_lcd_cursor_off);
41
lcd_cmd (_lcd_clear);
while (1)
delay_ms (100);
delay_ms (100);
UART1_Write (ID);
delay_ms (100);
UART1_Write (alcohol);
delay_ms (100);
motor = forward;
fault = 1;
pwm1_set_duty (gear1);
delay_ms (1000);
pwm1_set_duty (gear2);
delay_ms (1000);
pwm1_set_duty (gear3);
delay_ms (1000);
pwm1_set_duty (0);
delay_ms (1000);
42
motor = stop;
delay_ms (300);
break;
If (speedsensor == 1)
UART1_Write (ID);
delay_ms (100);
UART1_Write (speed);
delay_ms (100);
while (speedsensor)
motor = forward;
fault = 1;
if(speedsensor == 0)
fault = 0;
if (colorsensor == 1)
count++;
43
/*if (count >= 3)
count = 0;*/
motor = forward;
pwm1_set_duty (gear1);
motor = forward;
pwm1_set_duty(gear1);
lcd_out(1,7,text8);
// lcd_out(2,7,"****");
motor = forward;
pwm1_set_duty(gear1);
lcd_out(1,7,text7);
lcd_out(2,7,text4);
44
}
motor = forward;
pwm1_set_duty(gear1);
lcd_out(1,7,text8);
lcd_out(2,7,text4);
motor = forward;
pwm1_set_duty (gear3);
If ((alco_level >= 513) && (alco_level <= 768) && (count == 2))
motor = forward;
pwm1_set_duty (gear3);
45
{
count = 0;
motor = forward;
pwm1_set_duty (gear2);
if ((alco_level >= 513) && (alco_level <= 768) && (count == 3))
count = 0;
motor = forward;
pwm1_set_duty (gear2);
46