Sign in to follow this  
iamcanadian

Programming a color sensor

Recommended Posts

iamcanadian    122
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 this post


Link to post
Share on other sites
Kris_A    182
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 this post


Link to post
Share on other sites
Taymo    122
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 this post


Link to post
Share on other sites
iamcanadian    122
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 this post


Link to post
Share on other sites
Zahlman    1682
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Mattman    336
Quote:
Original post by iamcanadian
I 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 this post


Link to post
Share on other sites
Mattman    336
Quote:
Original post by Kris_A
something like this maybe?

*** Source Snippet Removed ***

edit: whoops, used C++ code

edit 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 this post


Link to post
Share on other sites
Taymo    122
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.

Share this post


Link to post
Share on other sites
iamcanadian    122
Zahlman, you would need a PIC that can support ADC, a pic18 does. you'll also need a program that can enable ADC conversion. I believe you're talking about steps and resolution in a A/D conversion process, which you're absolutely right.

My circuit has 3 outputs, one for green sensing, blue sensing, and red. so I would have 3 inputs to a PIC. The PIC converts A/D and and gives me a discreet value. Say for instance, the output voltage is 4 volts, after the analog to digital conversion is done, I would see 0100 in binary.

That is how I want the program to work, I know if you can do something in assembly language you can do it in C.

Thank you all for the help

I hope I've explained right, additional help is appreciated!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this