Okay, there is no magic genie, but it is finally, time to get to coding the Arduino, playing with your new sensor, and enjoying all the benefits that come along with having a lamp so discreet that it tries to blend in with its surroundings..

Umm, if you can think of a benefit, let me know, I was having trouble with that one.



I have included the code as a text file, you will have to open it in a text editor and then copy and paste it into the Arduino environment. Or, if you prefer, just open the Arduino software and follow along writing it as we go.



For the emulation lamp I am using my custom Moodlamp library, you can get the library files and documentation on my other RGB Instructable, RGB Moodlamp with Custom Library. Otherwise you can just copy the colour sensor handling code and output to your RGB lamp in your preferred manner.



So let`s look at the code.

Start with the first image, or follow along with the text file.



We start with an include statement followed by a couple of declarations.

The include statement is for the Moodlamp library. The declaration beneath is just saying that our instance of Moodlamp will be called lamp.

Then we set up a couple of place holders.

The first is an array that we will use to address the pins the that LEDs are connected to. Remember we connected our sensor lights to those same pin numbers.

The others should be clear enough by the notes in the image, if not, never fear, they are just some place holders for values that we will use later on.



The place holder for the average reading is not special, it got it’s own image because it was almost overlooked.



Once all our declarations have been made, we will now have to write the setup function. We start by assigning our sensor output pins. We do not need to declare the analog input of the sensor, and the lamps pins are taken care of by the Moodlamp library.

Then we start serial communication so that we can get some debugging information back from our lamp.



After that we can proceed with our loop function. The loop is the code that the Arduino will run again and again. As you can see we are calling a few functions.

First we call checkBalance to see if the balance has been set and set it if needed. (When it turns on)

Then we check the colour under the sensor. Once we have a colour, we print it to the screen, and then set the colour of the lamp. The last call to getReading does nothing useful at all, left it in there by accident. Oh well, I could claim that I was just keeping the lamp from being too idle.



So let`s look at the different functions.



CheckBalance is pretty straight-forward, it checks to see if a balance exists and if it doesn’t, then it sets it by calling the setBalance function.



Let’s look at the setBalance function, it is the longest one.



First we pulse the light red twice and then white using the Moodlamp library. This is to warn the operator that the white scan is about to take place. Leaving the lamp white(all three colour on) leaves as much interfering light as possible for our baseline scans which keeps them equally balanced.



What the following section does is light the red, then green, then green LEDs while taking a sensor reading at each step. This gives us some data about how much of each colour was reflected back to the sensor. These readings should be high as it is white, which should reflect a lot for each colour.



If the meaning is not inferred, you must place a sample of white ( a card or a paper) under the sensor while this scan takes place. The whole thing lasts about three seconds. The scan less than one.



When finished with scanning white, we will scan black. There are some warning lights again, a delay, and then the scan. Here you will want a black or grey sample under the lamp. We will talk a little more on what your sample should be later.



Once the balancing scans are completed, the lamp will turn off, and we set the Boolean value for whether or not balancing values have been established to true.



Once the balances have been set, our program will now start checking the colour beneath the sensor, chances are that in the physical world you still haven’t removed it from your black sample, but never mind that, we were talking about the code.



If you followed what was going on with the balancing scan, we are pretty much doing the same thing. The big difference is that we are using some math to balance our reading now. Basically we are altering our reading based on the maximum and minimum possible readings, and making it a value we can use, namely between 0-255(roughly). Which gives us a pretty fair representation of the colour.



After that, we simply set the colour. We need to just tweek the numbers a little, because often the math above returns greater than or less than our acceptable values. Then we set the red green and blue variables and use the Moodlamp library to facilitate displaying the colour.



I almost forgot to mention the getReading function, which just gets an average reading for times number of readings. Not strickly necessary, as one reading seems sufficient, but I did not know that at the time.



The two print functions are for debugging purposes, and simply print our readings to the serial port. Yes I know I call one in the loop, but you don't have to. I just like scrolling numbers sometimes. Plus I was outputting it to processing during testing, to see exactly what colour the sensor is seeing. I will try to find the time to document and upload that sketch in the near future, but for now you are on your own. (Not hard if you are familiar with Processing and serial communication)



Once you have finished writing your code, upload it to the lamp and start playing. By playing I mean testing....but in a fun way.