You are on page 1of 5

// In serial monitor, change the line ending to "Newline"

const byte LED_PINS[] = {9, 10, 11}; // Pins of the LEDs in R, G, B order. IMPORTANT!

const byte LDR_PIN = A0; // Pin for the LDR

bool doCalibration = true; // Set "true" if you want to do calibration, "false" otherwise.

int whiteValue[] = {909, 947, 946}; // Change this value according to your calibration value.

int blackValue[] = {779, 820, 838}; // Change this value according to your calibration value.

// Tune this value in case you think the value of the channel is too high or too low

int colourCorrection[] = { -50, 0, 10};

int colourValue[3]; // This variable will hold the currently scanned colour.

float average, colourRange; // Will be used to calculate later.

void setup() {

Serial.begin(9600); // Start the serial communication

pinMode(LDR_PIN, INPUT); // Set the LDR that will be reading the colour value as input.

for (byte i = 0; i < 3; i++) {

pinMode(LED_PINS[i], OUTPUT); // Set RGB pin to output.

digitalWrite(LED_PINS[i], HIGH); // Write HIGH for common anode RGB LED. LOW if common cathode.

if (doCalibration) { // If the value is set as "true" earlier, calibration will be done.

calibrateColourSensor();

}
void loop() {

checkColour(); // Take latest raw colour value

calculateColour(); // Calculate the colour value

printColour(); // Print colour value to serial monitor

//This function will allow us to calibrate colour sensor

void calibrateColourSensor() {

Serial.println("Begin sensor calibration.");

Serial.println("Place a WHITE object in front of the sensor.");

waitForInput(); // Wait for the user to press enter in the serial monitor

checkColour(); // Update colourValue array

printColour(); // Display the scanned colour

for (byte i = 0; i < 3; i++) {

whiteValue[i] = colourValue[i]; // Transfer the values from colourValue to whiteValue array

Serial.println("Place a BLACK object in front of the sensor.");

waitForInput(); // Wait for the user to press enter in the serial monitor

checkColour(); // Update colourValue array

printColour(); // Display the scanned colour

for (byte i = 0; i < 3; i++) {

blackValue[i] = colourValue[i]; // Transfer the values from colourValue to blackValue array

}
Serial.println("Calibration complete.");

waitForInput(); // Wait for the user to press enter in the serial monitor

//This function waits for the user to press enter.

void waitForInput() {

Serial.flush(); // Remove any buffer

Serial.println("Press enter to continue...");

while (!Serial.available()); // Wait for input

while (Serial.available()) { // Go through all input value

if (Serial.read() == '\n') { // If found newline character

Serial.flush(); // Remove any buffer

break; // Break out of the loop

//This function will update the value of colourValue array

void checkColour() {

for (byte i = 0; i < 3; i++) {

digitalWrite(LED_PINS[i], LOW); // Turns on the LED

getReading(5); // Get the average value of 5 readings

colourValue[i] = average + colourCorrection[i]; // Apply colour correction

digitalWrite(LED_PINS[i], HIGH); // Turns off the LED

}
}

//This function calculate the average of the colour reading

void getReading(int times) {

int total = 0;

for (int i = 0; i < times; i++) {

total += analogRead(LDR_PIN); // Takes the reading and sum up

delay(10); // A bit of delay between each reading

average = total / times; // Basic calculation for average

//This function converts RAW value to value of 0 to 255

void calculateColour() {

for (byte i = 0; i < 3; i++) {

colourRange = abs(whiteValue[i] - blackValue[i]);

colourValue[i] = constrain(colourValue[i], blackValue[i], whiteValue[i]);

colourValue[i] = (colourValue[i] - blackValue[i]) / colourRange * 255;

//This function prints the value colour in the following form: (RRR,GGG,BBB). Eg. (34,150,200)

void printColour() {

Serial.print("(");

Serial.print(colourValue[0]); //Red

Serial.print(",");

Serial.print(colourValue[1]); //Green
Serial.print(",");

Serial.print(colourValue[2]); //Blue

Serial.print(")\t");

// This part requires thinkering and depends heavily on the values that you get.

if (colourValue[0] < 200 && colourValue[1] < 120 && colourValue[2] > 100) {

Serial.println("BLUE");

else if (colourValue[0] > 200 && colourValue[1] < 200 && colourValue[2] < 150) {

Serial.println("RED");

else if (colourValue[0] > 200 && colourValue[1] > 200 && colourValue[2] < 200) {

Serial.println("YELLOW");

else {

Serial.println();

You might also like