0% found this document useful (0 votes)
48 views28 pages

Full Text 01

The document discusses a color detection research project. It describes developing a device that can identify colors using a sensor that measures light reflection. The sensor consists of red, green, and blue LEDs and photodiodes. Measurements were taken of various colors at different distances and angles to understand important detection factors. Methods for color identification were researched, including comparing measured light values to references. The project involved selecting hardware components like a sensor, amplifier, and microcontroller, as well as software development.

Uploaded by

saikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views28 pages

Full Text 01

The document discusses a color detection research project. It describes developing a device that can identify colors using a sensor that measures light reflection. The sensor consists of red, green, and blue LEDs and photodiodes. Measurements were taken of various colors at different distances and angles to understand important detection factors. Methods for color identification were researched, including comparing measured light values to references. The project involved selecting hardware components like a sensor, amplifier, and microcontroller, as well as software development.

Uploaded by

saikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

c

o
l

Color Detection Research Project


By

Neel Kabirpanthi

M.Sc. Project report 20 credits.


2006-06-15
Supervisors: Michael Tittus (University College of Boras) and
Christian Madristch (Carinthia Tech Institute) 2/2006
Index

1. Introduction .......................................................................................................................... 3

2. Description ............................................................................................................................ 4
a) The RGB Color Room.................................................................................................... 4

3. Different Measures............................................................................................................... 6
a) Measures with different distance to the object............................................................... 6
b) Measures with different angel of the sensor................................................................... 7
c) Temperatures measurement............................................................................................ 8
d) Temperatures measurement of the monitor diode.......................................................... 9

4. Researches of methods for color identification................................................................ 13


a) First method.................................................................................................................. 14
b) Second method ............................................................................................................. 15
c) Third method ................................................................................................................ 15
d) Comparison of the three methods................................................................................. 15
e) Last method for color identification............................................................................. 16

5. Selection of the construction units - Hardware ............................................................... 17


a) Introduction .................................................................................................................. 17
b) Power supply ................................................................................................................ 17
c) Sensor ........................................................................................................................... 17
d) The Amplifier............................................................................................................... 18
e) Microcontroller............................................................................................................. 18
f) Speech chip .................................................................................................................. 19
g) Schematic ..................................................................................................................... 20
h) Board ............................................................................................................................ 20

6. The design ........................................................................................................................... 22

7. Introduction of Software Development ............................................................................ 23


a) The Program................................................................................................................. 24

2/28
1. Introduction

The task of the project was to develop a device which can detect different colors by using the
sensor... of the company. The target group of the device are blind persons or persons who can
not detect different colors. There are already different systems on the market, but they are
very expensive and not useful for affordable.
The new device should be a compact, simple and a cheap solution.

3/28
2. Description

The sensor consists of three LED (red, green, blue) two Pin Diodes and a Monitor Diode.
Consecutively on Led will be turned on and then the Pin Diodes are measuring the reflection
of the light. Each color has a different reflection for the three LED. When the reflection is
high the Pin Diodes have a small resistor, is there a little reflection the resistor is high. This
value of resistance we transform into a voltage. So we get for each LED a value of voltage
which will be converting into the RGB Values. This value were compared with the reference
color (a collection of selected colors), and so the device get the right color. The name of the
color is given back by a speaker.

a) The RGB Color Room

To get an idea how it works to detect the color, we give a short explain of the RGB
Color Room.

The RGB Color Room is an additive Model. This means that the three fundamental
colors (Red, Green, and Blue) can be added to white. Nearly each color can be
described with these three fundamental colors.

In the graph we see the RGB Color Room. On the x-Axes is the color Red, on the y-
Axes is the color Blue and on the z-Axes is the color Green.
On the diagonal of the color room we find the grey scale. In the graph we can see that
each color is a combination of the three fundamental colors.

The measurements were made, to get an idea, which are the important factors to detect
the right color. For the measurements we used a System which contains a Lab VIEW-
Program, DAC-Card and the sensor. As Colors we used the „Digital ColorChecker
SG“of the company Gretagmacbeth, which consists of the standard colors and much
more colors.
In the following table are the colors of the color Checker which we used for our
measurements.

4/28
Index Koordinaten
1 2-E
2 2-F
3 2-G
4 2-H
5 2-I
6 2-J
7 3-E
8 3-F
9 3-G
10 3-H
11 3-I
12 3-J
13 4-E
14 4-F
15 4-G
16 4-H
17 4-I
18 4-J
19 5-E
20 5-F
21 5-G
22 5-H
23 5-I
24 5-J

5/28
3. Different Measures

a) Measures with different distance to the object

The following measurements were made to detect the influence to the RGB-values, by
different distances between sensor and the color.
The distance which was normally 2mm we increased to 4mm.

Difference by the Red-Led

300,000

250,000
values of the colors

200,000

RED-Led(2mm)
150,000
RED-Led(4mm)

100,000

50,000

0,000
0 20 40 60 80 100 120
Different Colors

Differnce by the Green-LED

250,000

200,000
values of colors

150,000
Green-LED(2mm)
Green-LED(4mm)
100,000

50,000

0,000
0 20 40 60 80 100 120
Differnet colors

6/28
Difference Blue-LED

300,000

250,000
values of the colors

200,000

Blue-LED(2mm)
150,000
Blue-LED (4mm)

100,000

50,000

0,000
0 20 40 60 80 100 120
Differenc colores

The graphs are showing the results by 2mm distance and by 4mm distance for each
LED. On the x axes are the 24 standard colors, but for each color we took 5
measurements.
In this diagrams we can see, that the distance to the color, which we want to measure
is important, because the values of the three LED are changing because of the
distance.

b) Measures with different angel of the sensor

The following measurements were made to detect the influence to the RGB-values, by
different Angle between sensor and object. We had fixed the sensor, with an auxiliary
construction, in three different angels (85°- / 85°+ / 90°). The following graphs
descript the 24 measured colors.

Difference angle between the sensor and the object for the Red-LED

260,000

210,000
value of color

160,000
90°
85° +
85° -
110,000

60,000

10,000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
different colors

7/28
Difference angle between the sensor and the object for the Green-LED

250,000

200,000
value of the color

150,000
90°
85° +
85° -
100,000

50,000

0,000
1 6 11 16 21
different colors

Difference angle between the sensor and the object for the Blue-LED

300,000

250,000

200,000
value of the colors

90°
150,000 85° +
85° -

100,000

50,000

0,000
1 6 11 16 21
different colors

The curve 85° + describes the measurements, when the blue LED is in the origin and
the curve 85° - describe the measurements, when the red-LED is in the origin. The
curve 90° describes the measurements, when the sensor is justified on the color.
We think, for correct measurement it is important that the angle between the sensor
and the object (color) is always the same. So we must show, that the angel is nearly
90°, because it is easier to holt the sensor with a angel of 90° then with 85°+ or 85°-.

c) Temperatures measurement

We had problems because we do not get repeatable measurements. So we made some


thoughts about the temperature. We made measurements to find out if a changing of
the temperature of the sensor influences a changing of the RGB Values.

So we made 1000 measurements on a white paper without break between the


measurements. The results are held on in the following graph.

8/28
1000 measurements without break

250,000

245,000

240,000
value of the colors

235,000

230,000 Red-LED
Green-LED
225,000 Blue-LED
220,000

215,000

210,000

205,000
0 100 200 300 400 500 600 700 800 900 1000
counter of measures

On this point we can not say that the changing of the values is an effect of the
temperature. So we made new measurements.

At first we made 30 measurements and between the measurements we turned all three
Leds on for 2 minutes. On the graph below are the results of this measurement.

measurement with 2 minutes break( all LED-OFF)

240,000

235,000

230,000
v a lu e o f h te c o lo rs

225,000
Red-LED
220,000 Green-LED
Blue-LED
215,000

210,000

205,000

200,000
0 5 10 15 20 25 30
counte r of me asure s

The brake of two minutes causes that the sensor can cool down. The values are nearly
constant, so the temperature has an influence to the values rather to the Pin Diodes.

d) Temperatures measurement of the monitor diode

After a consultation with the producer of the sensor we get an idea how we can solve
the problem with the temperature. The sensor has inside a Monitor Diode.

9/28
With this Monitor Diode we can measure the temperature, and we can adjust the RGB
values.

In the next graphs we made different measurements including the measurements with
the Monitor Diode (temperature).

RGB-values of the first measurement

215,000

210,000
value of the colors

205,000

Red
200,000 Green
Blue

195,000

190,000

185,000
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76
qua ntity of m e a sure s

Monitor Diode of the first measurement

3,578

3,577

3,576

3,575
values of the colors

3,574

3,573

3,572

3,571

3,57

3,569
1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77
quantity of m e as ur e s

10/28
RGB-values of the third measurement

210,000

205,000
value of the color

200,000
Red
Green
Blue
195,000

190,000

185,000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
quantity of measures

11/28
Monitor Diode of the third measurement

3,58

3,578
value of the color

3,576

Reihe1

3,574

3,572

3,57
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
quantity of measures

The voltage of the monitor diode depends on the temperature of the sensor.

12/28
4. Researches of methods for color identification

An important point is to give out the right color, but at first we have to detect it. To find the
right color we use a table with the RGB values of the 24 most important colors.

To explain the methods for detecting the color we take random values for the measured RGB
values.

For example:
R G B
measured value 35 100 70

This example shows you the color bluish green with the index 6. The next step is to calculate
the difference between the measured values and the reference values which is pictured below.

Reference value Difference


R G B Index R G B Index
dark skin 43,009 26,224 15,589 1 8,009 73,776 54,411 1
light skin 147,376 75,147 55,27 2 112,376 24,853 14,73 2
blue sky 36,181 39,887 85,321 3 1,181 60,113 15,321 3
foliage 27,294 33,864 15,758 4 7,706 66,136 54,242 4
blue flower 70,648 49,911 109,701 5 35,648 50,089 39,701 5
bluish green 49,265 101,815 98,12 6 14,265 1,815 28,12 6
orange 148,275 86,562 13,905 7 113,275 13,438 56,095 7
purplish blue 22,561 21,964 102,041 8 12,439 78,036 32,041 8
moderate red 150,229 28,028 33,018 9 115,229 71,972 36,982 9
purple 35,495 13,254 35,623 10 0,495 86,746 34,377 10
yellow green 79,791 129,208 19,224 11 44,791 29,208 50,776 11
orange yellow 164,194 133,479 16,922 12 129,194 33,479 53,078 12
blue 10,26 9,849 79,661 13 24,74 90,151 9,661 13
green 23,519 58,385 16,796 14 11,481 41,615 53,204 14
red 166,618 14,802 11,86 15 131,618 85,198 58,14 15
yellow 194,183 177,114 15,151 16 159,183 77,114 54,849 16
magenta 174,562 27,858 78,173 17 139,562 72,142 8,173 17
cyan 18,601 29,882 90,129 18 16,399 70,118 20,129 18
white 9.5 (.05 D) 235,122 233,511 226,297 19 200,122 133,511 156,297 19
neutral 8 (.23 D) 147,211 150,291 150,387 20 112,211 50,291 80,387 20
neutral 6.5 (.44 D) 89,186 91,952 92,484 21 54,186 8,048 22,484 21
neutral 5 (.70 D) 47,088 49,274 49,878 22 12,088 50,726 20,122 22
neutral 3.5 (1.05 D) 21,758 22,841 23,363 23 13,242 77,159 46,637 23
black 2 (1.5 D) 8,09 8,105 8,311 24 26,91 91,895 61,689 24

13/28
a) First method
+
Now we take the 12 minimum values of the red-LED and accept the two other values
form the two other colors.

12 min of red value accept accept


Index R G B
10 0,495 86,746 34,377
3 1,181 60,113 15,321
4 7,706 66,136 54,242
1 8,009 73,776 54,411
14 11,481 41,615 53,204
22 12,088 50,726 20,122
8 12,439 78,036 32,041
23 13,242 77,159 46,637
6 14,265 1,815 28,120
18 16,399 70,118 20,129
13 24,740 90,151 9,661
24 26,910 91,895 61,689

In the second step, we pick out the 6 Minimum values of the green-LED from the table
on the top.

accept 6 min. of green value accept


Index R G B
6 14,265 1,815 28,120
14 11,481 41,615 53,204
22 12,088 50,726 20,122
3 1,181 60,113 15,321
4 7,706 66,136 54,242
18 16,399 70,118 20,129

In the third step, we pick out the 3 minimum values of the blue-LED from the table
above.

accept accept 3 min. of blue value


Index R G B
3 1,1811808 60,1125408 15,321
22 12,0878272 50,7264448 20,122
18 16,39924672 70,1175488 20,129

Now we made the sum of all values of each index. The result is:

Index absolute deviation


3 76,615
22 82,936
18 106,646

The result with the first method is the color blue sky with the index 3.

14/28
b) Second method

In the second method we used an allowance for the research, in this we choose index
10. This means, that the measured values can have only a difference of +/-10 to the
reference value.

In the first step we pick out the values from each LED, where the difference is lower
+/- 10 and made the sum of all values of each index

Red-LED sum of the differences


Index R G B
10 0,495 86,746 34,377 121,617
3 1,181 60,113 15,321 76,615
4 7,706 66,136 54,242 128,084
1 8,009 73,776 54,411 136,195

Green-LED
Index R G B
6 14,265 1,815 28,120 44,200

Blue-LED
Index R G B
17 139,562 72,142 8,173 219,876
13 24,740 90,151 9,661 124,552

The result with the second method is the color bluish green with the index 6.

c) Third method
The third method is nearly the same like the second method. The difference is only,
that we don’t make the sum of all differences. We make a weighting of all values from
each index.

d) Comparison of the three methods

Comparison of the three methods

Method memory capacity computing power


Dependent on the accuracy, the smaller the range of
tolerance, the smaller is the storage requirement.
However it can occur with the fact that the ‘correct’ color This will need surely at few
1 is excluded. computing power

This will need more computing


power because we use many
Depend on the allowance, the smaller it is, the smaller is values which are needed for
2 the storage requirement. the result.

15/28
This will need surely at most
computing power, because of
Depend on the allowance, the smaller it is, the smaller is the weighting of all values from
3 the storage requirement. each index.

As a conclusion, we can say, all the three methods have a possibility, to get the false
color-name. So we have found an easier and exacting method to find the correct color.

e) Last method for color identification

The method which we are using to detect the color is a mathematical method. With
Red, Green and Blue we can create a RGB - Color room, in this room counts the same
mathematical rules like in other vector rooms.
To detect the color we calculate the smallest vector to the reference colors. This is a
normal vector calculation.
Calculation: smallest vector…v
Reference red value …rv
Reference green value. …gv
Reference blue value …bv
Measured red value …mrv
Measured green value …mgv
Measured blue value …mbv
v = ( rv − mrv ) 2 + ( gv − mgv ) 2 + (bv − mbv ) 2

For every reference color we get a result, but the smallest value gives as the correct
color.

16/28
5. Selection of the construction units - Hardware

a) Introduction

The Hardware is an important component of this project. It is divided in three mean


parts: Microcontroller, Sensor, Speech part. Each mean part needs other parts and
components for the correct functionality.

b) Power supply

A 3 Volt Battery is used for the power supply. The Microcontroller and the Speech
Chip needs 5 Volt, so the 3 Volt is not enough. The MAX756 is a step up converter
which converts an input voltage between 1.7 Volt to 5 Volt up to 3 Volt or to 5 Volt.
This Chip detects also when the battery is low. With 5 Volt output voltage it is
possible to get an output current up to 200 mA.

The picture shows the Layout of the step up converter.

c) Sensor

The Sensor consists of a Red LED, Green LED, Blue LED, two Photo Diodes, and a
Monitor Diode. There is also a connected ground and a reference Voltage for the
Photo Diodes.
The Red LED needs a voltage around 1.91 Volt and a current of 14mA, so the resister
between microcontroller and the LED must be 22K. The blue LED needs a voltage of
3.75 Volt and a current of 7mA so the resister must be 180Ohms. The Green LED
takes 5 Volt and a current of 32mA, but the microcontroller has an output current of a
maximum of 25mA. So the current is given by a transistor. The basis of this transistor
is connected to the microcontroller and the emitter is connected to the green LED. If

17/28
the output port of the microcontroller is set low the transistor locks. If the output port
is high the green LED gets the current.
The Photo Diodes are used for detecting the light intensity. The resistor or the
conductivity of the Photo Diodes is changing when the light intensity is changing.
The Monitor Diode is used for detecting the temperature in the sensor. It is also
changing the resistor when the temperature is changing.

d) The Amplifier

An amplifier is used to amplify the signal of the Photodiodes. The amplifier is an


inverting amplifier. It consists of an OPV LM358 a resistor and a capacitor. If the
resistor of the Photo Diodes is changing the output voltage of the OPV is also
changing. The capacitor is used to stable the voltage.

e) Microcontroller

We use the PIC18F2455 from microchip. More information about the microcontroller
please visit the datasheet
(http://www.microchip.com/downloads/en/DeviceDoc/39632c.pdf).
The Voltage of the Pin Diode1 is connected with the Pin 2 (Port A0), and the Pin
Diode 2 is connected with Pin 3 (Port A1). On the Pin3 we find the Monitor Diode. On
the Pins 11, 12, 13 are connected the Leds (Blue, Red, Green). It is possible to detect
the low battery with the Port RC5 (Pin16). On Pin 18 (Port RC7) is the bush bottom to
start a measurement. The Pins 21, 22, 23, 24, 25 (Port B0…B4) are the address lines
for the Speech Chip. With Pin 27 (Port B6) we can detect when a played message is
finish. To start playing a message we give the signal on Pin 28 (Port B7).

18/28
f) Speech chip

We are using the speech chip to give out the name of the color by a speaker. After
researches we have decided to use an ISD chip, because it was the only chip which is
able to give out an analog signal.
For us it was important because the digital output of a voice is very hard to
understand.

We are using the ISD 1420, which can store 20 seconds. The following graph shows
the protective circuit.

This circuit was important to record the different messages (color names). The
addresses A0-A7 we are using to select the position of the message with a binary code.
The buttons PLAYL / PLAYE / REC are to play or store the messages. The AGC
input is for amplify of the input signal. The input signal can be transmitted by two
different ways. The first way we are using is a Microphone and the second way we set
the output “ANA OUT” after the capacitor at Ground. Then we connect an analog
signal (PC/ MP3-player …) on the “ANA IN”.
For the output we are using an 8 Ohm speaker.

19/28
g) Schematic

h) Board

20/28
Top:

Bottom:

21/28
6. The design

22/28
7. Introduction of Software Development

The goal of this report is to development of software for microcontroller to develop the
embedded system of the color detection project. The basic idea of the software is to
communicate with two modules of the embedded system sensor and speech chip.

First of all, Ports configuration done of microcontroller. After that it turns on each LED of the
sensor in sequence and when the Led is turning on, Pin diodes of the sensor measures the
reflection of Led light and it was simply done by calling analog to digital function (ADC).

Then some mathematical operations are come in the picture, which are applied on result of
ADC to make it comparable with universal RGB values (stored in arrays).

Next step is to compare this measured and calculated RGB values with universal RGB values
and here, software finds the minimum distance between measured and universal RGB values.

At the end, after finding minimum distance it is ready to send 5 bits address to speech chip
where the colours are stored and after receiving this message speech chip is responsible to
speak the matching color.

The microcontroller used was PIC18F2455 from microchip. The programmer tools were
MPLAB and PICSTART Plus from Microchip. All the programming development was made
in ANSI C using Microchip C18 compiler.

For more information complete software implementation is included.

23/28
a) The Program
#include <stdio.h>
#include <math.h>
#include <adc.h>
#include <delays.h>
#include <p18f2455.h>

//Function declaration
void color_detection(int, int, int);
void play(void);
int adc_1(void);
int adc_2(void);

//Global arrays
unsigned long red[] = {43 , 147, 36, 27, 71, 49, 148, 23, 35, 164, 10, 24, 167, 194, 175, 19, 235, 47, 8};
unsigned long green[]= {26, 75, 40, 34, 50, 102, 87, 22, 13, 133, 10, 58, 15, 177, 28, 30, 234, 49,8};
unsigned long blue[]={16, 55, 85, 16, 110, 98, 14, 102, 36, 17, 80, 17, 12,15,78, 90, 226, 50, 8};
unsigned long new[19], search=0;

void main (void)


{
int voltage_r, voltage_g, voltage_b;
int
voltage1,voltage2,voltage3,voltager,voltageg,voltageb,voltage_r1,voltage_r2,voltage_g1,voltage_g2,voltage_b1,voltage_b2;
unsigned long i=0;
unsigned long delay=65000;

//TRIS register configuration


TRISA=0xFF;// Set PORTA pins as input
TRISB=0x40;// Set PORTB <0.3>pins as output and <4.7>pins as input
TRISC=0x80;// Set PORTC <7>pin as input rest are outputs
PORTC=0x00;// Clear PORTC
PORTB=0x00;// Clear PORTB
PORTCbits.RC7=1;//Set push button to high
PORTBbits.RB7=1;//Set digital output on high for playing message

OpenADC(ADC_FOSC_32 & ADC_LEFT_JUST , ADC_CH0 & ADC_CH1 & ADC_16_TAD & ADC_INT_OFF &
ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS , 13);//Initialize analog to digital converter
while(1)
{

if(PORTCbits.RC7==0)//Wait for press button to set on low


{
for(i=0;i<1000;i++)//Turn on Red LED for short time
{
PORTC=0x02;
voltage_r1=adc_1();// Store the result of Analog to Digital conversion(Red LED)

voltage_r2=adc_2();
}
voltager=(unsigned int)((voltage_r1+voltage_r2)/2);
//Next two instructions converts the result(Red LED) into RGB form
voltage1= (voltager-180)*(-1);
voltage_r = (voltage1*3);
i=0;
for(i=0;i<delay;i++)//Turn off Red LED and wait for short time
{
PORTC=0x00;
}i=0;

for(i=0;i<1000;i++)//Turn on Green LED for short time


{
PORTC=0x04;
voltage_g1=adc_1();// Store the result of Analog to Digital conversion(Green LED)
voltage_g2=adc_2();
}i=0;
voltageg=(unsigned int)((voltage_g1+voltage_g2)/2);
//Next two instructions converts the result(Green LED) into RGB form
voltage2= (voltageg-180)*(-1);
voltage_g = (voltage2*7);
for(i=0;i<delay;i++)//Turn off Green LED and wait for short time
{
PORTC=0x00;
}i=0;

24/28
for(i=0;i<1000;i++)//Turn on Blue LED for short time
{
PORTC=0x01;
voltage_b1=adc_1();// Store the result of Analog to Digital conversion(Blue LED)
voltage_b2=adc_2();
}i=0;
voltageb=(unsigned int)((voltage_b1+voltage_b2)/2);
//Next two instructions converts the result(Blue LED) into RGB form
voltage3= (voltageb-180)*(-1);
voltage_b = (voltage3*4);
for(i=0;i<delay;i++)//Turn off Blue LED and wait for short time
{
PORTC=0x00;
}i=0;

PORTCbits.RC7=0;//Set again push button to high


color_detection(voltage_r, voltage_g, voltage_b);//Call the color_detection function
}
}
CloseADC();
}

//This function is responsible for detecting the color and sending the message to Speech chip
void color_detection(int voltage_r, int voltage_g, int voltage_b)
{
unsigned long nred=0, ngreen=0, nblue=0,i=0;
//This loop finds the distances between measured colors result and universal RGB color values
for(i=0;i<=18;i++)
{
nred= (unsigned long)((red[i]-voltage_r)*(red[i]-voltage_r));
ngreen= (unsigned long)((green[i] - voltage_g)*(green[i] - voltage_g));
nblue= (unsigned long)((blue[i] - voltage_b)*(blue[i] - voltage_b));
new[i]= (unsigned long) (nred + ngreen + nblue);

}
i=0;
search=(unsigned long)new[0];
//This loop search for minimum distance
for(i=0;i<18;i++)
{
if(search>new[i+1])
{
search=new[i+1];//printf("%lu\n",search);
}

}
//This loop compares the minimum distance with alredy stored distances and also pass the suitable 5 bits message to
Speech chip.
if(new[0]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();// Call the function play
}
else if(new[1]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[2]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=1;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[3]==search)

25/28
{
PORTBbits.RB0=1;
PORTBbits.RB1=1;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[4]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=1;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[5]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=0;
PORTBbits.RB2=1;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[6]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=1;
PORTBbits.RB2=1;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[7]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=1;
PORTBbits.RB2=1;
PORTBbits.RB3=0;
PORTBbits.RB4=0;
play();
}
else if(new[8]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[9]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[10]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=1;
PORTBbits.RB2=0;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[11]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=1;
PORTBbits.RB2=0;
PORTBbits.RB3=1;

26/28
PORTBbits.RB4=0;
play();
}
else if(new[12]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=1;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[13]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=0;
PORTBbits.RB2=1;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[14]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=1;
PORTBbits.RB2=1;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[15]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=1;
PORTBbits.RB2=1;
PORTBbits.RB3=1;
PORTBbits.RB4=0;
play();
}
else if(new[16]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=1;
play();
}
else if(new[17]==search)
{
PORTBbits.RB0=1;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=1;
play();
}
else if(new[18]==search)
{
PORTBbits.RB0=0;
PORTBbits.RB1=1;
PORTBbits.RB2=0;
PORTBbits.RB3=0;
PORTBbits.RB4=1;
play();
}
}

//Here analog to digital conversion happens


int adc_1(void)
{
int result;
SetChanADC(ADC_CH0);
Delay10TCYx(5);//Wait for 50 instruction cycle
ConvertADC();//Start A/D converter
while(BusyADC());//wait until ADC is busy
ReadADC();//Read analog to digital conversion

27/28
result = ADRESH;//Store the 8 bits result
return(result);
}

int adc_2(void)
{
int result;
SetChanADC(ADC_CH1);
Delay10TCYx(5);//Wait for 50 instruction cycle
ConvertADC();//Start A/D converter
while(BusyADC());//wait until ADC is busy
ReadADC();//Read analog to digital conversion
result = ADRESH;//Store the 8 bits result
return(result);
}
//This function is set digital output PORTB<7> to low to play the sound from speech chip
void play(void)
{
int i=0;
for(i=0;i<2;i++)
{
PORTBbits.RB7=0;
}
PORTBbits.RB7=1;
}

28/28

You might also like