Basic Colour Sensor Problem


Recommended Posts

Hello all, first time poster here, so I apologize in advance if I posted this topic in the wrong section.

Let me first explain that I am very new to the DAQFactory software and am having a bit of trouble grasping it all. Without further adieu, here is my issue:

I am trying to build a basic colour sensor in order to make a very rudimentary facsimile of those "chameleon" lamps that have become so popular as of late. Essentially, I want to shine light sequentially (red, green, then blue) on an object and log the reflected light intensity with a photocell. I want to use these values in order to compute the relative values of the different colours of light and use these values to extract a colour based on ratios of the colour components.

Making the actual set-up was fairly easy, but now I'm having trouble with the sequencing. Here is what I have so far:

I have rigged my sequence (Light_Sequence) to start when I activate the Start/Stop button component. When the sequence begins, I want to turn my red LED on for one second and log the data received from my photocell while the LED is on. I want to repeat this process for both green and blue as well. Now, getting the LEDs to trigger sequentially was not hard:


Red=1
delay(1)
Red=0
delay(1)
Green=1
delay(1)
Green=0
delay(1)
Blue=1
delay(1)
Blue=0
[/CODE]

What I'm having trouble scripting is the "while" function that will have my photocell log the data while Red=1, etc. Now, I know that DAQFactory outputs these values to an array, I just don't know how to call up that data in order to use it.

What I would like is to take ten readings over the time that each LED is on, take the average of those ten readings, and output that value as an integer value for each colour. So, for instance, while the red LED is on, I want to take ten readings, take the average of those readings and output that value as an integer which I will call RedReflect (or something along those lines) which I will then compare to a similar value that I obtained from reading a white sample.

I'm sure the process needed to do this is so basic that half of you probably consider me an idiot for not being able to figure it out, but it has been years since I've needed to any level of programming or sequencing and I must admit I'm at a loss. Any help you could offer would be appreciated.

Link to comment
Share on other sites

OK, a couple assumptions here:

1) when you say "output" these values, I'm going to assume you just mean put them in a variable so you can display the last reading. Logging them to disk is something different, or should I say, another step.

2) I'm going to assume you are using a single sensor.

First, you need to have the sensor reading coming in 10 times a second so you can get 10 readings. Just set the Timing on your sensor channel to 0.1.

Next, you just need to create some global variables to capture the readings. Then its pretty easy. You just change your script to this:


global redReflect
global greenReflect
// etc for each color
while (1)
Red=1
delay(1)
RedReflect = mean(sensorChannel[systime(), systime()-0.99])
Red=0
delay(1)
// repeat for other colors:
endwhile
[/CODE]

You could probably also subset by value: sensorChannel[0,9], but its possible that you'll get some overlap with a reading when the LED isn't on, so by subsetting by time, we ensure we only get values while the LED is on since time is something we have control over.

Link to comment
Share on other sites

OK, a couple assumptions here:

1) when you say "output" these values, I'm going to assume you just mean put them in a variable so you can display the last reading. Logging them to disk is something different, or should I say, another step.

Exactly, my apologies if that was unclear. I just wanted to know how to display the mean value of the last ten readings as a variable.

2) I'm going to assume you are using a single sensor.

You are correct.

First, you need to have the sensor reading coming in 10 times a second so you can get 10 readings. Just set the Timing on your sensor channel to 0.1.

That much I already had done. Thanks!

Next, you just need to create some global variables to capture the readings. Then its pretty easy. You just change your script to this:


global redReflect
global greenReflect
// etc for each color
while (1)
Red=1
delay(1)
RedReflect = mean(sensorChannel[systime(), systime()-0.99])
Red=0
delay(1)
// repeat for other colors:
endwhile
[/CODE]

You could probably also subset by value: sensorChannel[0,9], but its possible that you'll get some overlap with a reading when the LED isn't on, so by subsetting by time, we ensure we only get values while the LED is on since time is something we have control over.

[b]Please forgive my ignorance, but would a while(1) not execute a neverending loop, or at least as long as the sequence is running? So, if I was too slow to trigger the sequence off with my button component, would it then keep recalculating all of my *Reflect values? Perhaps multiple while functions with specific condition statements (i.e. while(Red=1), etc.) might work better? Either way, I thank you for your help, it is greatly appreciated. I guess I was overcomplicating things in my head. Now I see that it was far easier than I ever thought. Also, the time subset is brilliant. While it did occur to me that I might get some overlap, I figured that overlap would occur with each of my colour readings and that the returned values would still be congruent with one another. [/b]

P.S. Stupid me, I only today realized that the software came with a very detailed user's guide and application guide. Hopefully I won't have to bother you folks with my newbie problems next time. Thanks again for your assistance!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.