# Programming a color sensor

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I designed a circuit that can differentiate between 3 colors, Red, Green, and Blue. Here is how is works: There are three outputs to the circuit. Red, Green, and Blue. When you reflect a Green object to the circuit, providing that your measuring the Green output of the circuit, it would reflect the highest voltage compared to the voltage if you reflect a Blue or Green object; same thing to the Red and Blue output. Now, my problem is, I don't know how to program it in C language :(. I'm using a PIC18 microcontroller. Here is how I want to program to look like. Convert analog/digital Read the voltage off all the three inputs report which one has the highest voltage, and that should be the color of the object. Any help is really appreciated. Thank you

##### Share on other sites
something like this maybe?

#define COL_R 1
#define COL_G 2
#define COL_B 3

float r = GetR();
float g = GetG();
float b = GetB();

int highest;

if (r > g)
if (r > b)
highest = COL_R;
else
highest = COL_B;
else
highest = COL_G;

edit: whoops, used C++ code

edit 2: hm, I think I may have read the question wrong, can you confirm that?

##### Share on other sites
Thank you Kris_A. Great start!

do you know how to convert a voltage to digital using a PIC18?

thank you!

##### Share on other sites
What compiler are you using? For the "volatge to digital" you need to implement the Analog to Digital converter. Everything you need to know will be in the datasheet although any C functions will not being they are very compiler dependant.

If you are using a CdS (Cadium Sulfide I think) photocell for seeing the light you should just do tests to see what the average volatge is from each light. Then do a ADC with the microcontroller and see which voltages are closest.

BTW..I don't mean to be a party pooper but isn't this a game programming forum. You will get much better results if you go to www.microchip.com and post on Microchip's forum. Good luck.

##### Share on other sites
thank you Taymo

That is exactly what I've done. took the average voltage, amplified it using Op-Amps, and want to convert it to digital. Unfortunately, I know little in C, I'm good in assembly language, but that's not the requirement. I'm Using MPLAB from Microchip as a compiler. I've looked in the datasheet for a PIC18F252 but it guides you in assembly language, not C. I just need some assist how to convert the voltage to digital and tells me what voltage it read, and compares it to the others; I'm sure its not that hard.

I'm sorry if I posted in the wrong forums. I thought this topic is best suited in the general programming forums. I've tried posting over at microchip forums, however, no one answered me.

Thank you

##### Share on other sites
PIC microcontrollers normally provide a hardware analog-to-digital converter. There is no such thing as a software analog-to-digital converter, full stop. You cannot implement it in C, nor in the PIC assembly language. This is because all the signals that are running around within the microcontroller itself are digital (voltages of either 0 or +5V, or something alont those lines).

An A/D converter is a hardware device that takes an analog input signal (something in some voltage range) and produces a series of digital signals, such that the high/low-ness of each of those signals represents a bit in a binary number, such that the value of the number corresponds to the input voltage.

Say the input voltage could run from 0.0000 to 0.9999 V, and you have a 4-bit A/D circuit: then the output value would be 0 to 15, depending on the input voltage, evenly distributed. So say the input were .51V: that would correspond to a value of 8, so the '8' signal would be set high by the circuit and the '4', '2' and '1' signals would all be set low.

This device consists, at least conceptually, of a whole bunch of op-amps in a rather tricky configuration, where the ones at the output are all intended to saturate (such that the 'line voltage' - either Vcc or ground - is emitted; hence, a digital signal).

##### Share on other sites
just a suggestion.. if you 're working in an academic environment or perhaps your company has access to it.. try doing your designs using matlab. you'll never have to leave the matlab suite for tasks like this one.

regards,
simon.

##### Share on other sites
Quote:
 Original post by iamcanadianI designed a circuit that can differentiate between 3 colors, Red, Green, and Blue.

You should say that it can differentiate among colors. "Between" means two items are included, "among" means three or more items are being mentioned. [smile]

##### Share on other sites
Quote:
 Original post by Kris_Asomething like this maybe?*** Source Snippet Removed ***edit: whoops, used C++ codeedit 2: hm, I think I may have read the question wrong, can you confirm that?

I believe his code example is a little off...

#define COL_R 1
#define COL_G 2
#define COL_B 3

float r = GetR();
float g = GetG();
float b = GetB();

int highest;

if (r > g)
if (r > b)
highest = COL_R;
else
highest = COL_B;
else
if (g > b)
highest = COL_G;
else
highest = COL_B;

##### Share on other sites
Quote:
 There is no such thing as a software analog-to-digital converter, full stop.

You still use the "software" to implement it though.

@iamcanadian - I don't know the function but there should be only one or two you need to start a ADConversion. Kinda sucks, there isn't too much documentation for programming in C the PICmicro.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634836
• Total Posts
3019544
×