Schedule‎ > ‎

Lab 22: A Color Tester

Introduction

Your Arduino kit comes with a photodetector that senses the intensity of light, but it cannot see colors. So what if you wanted to tell what color something was? Pictured is a circuit that combines your RGB LED and a photosensor in a way to make it possible to detect the color of an object. The circuit works by shining each color, one at a time, onto an object and measuring how much light bounces off of it back into the photosensor. 

Measuring Colors 

The circuit measures one color at a time. Here's the color reading procedure in pseudocode:

function ReadColor :

  turn off LEDs 
  wait 100 ms 
  zero = read sensor

  turn on RED 
  wait 100 ms 
  red = read sensor - zero 

  turn on GREEN
  wait 100 ms
  green = read sensor - zero 

  turn on BLUE 
  wait 100 ms 
  blue = read sensor - zero 

  turn off LEDs

  return Color(red, green, blue)

In order to get the most accurate reading you should put a piece of cardboard between the LED and your photosensor so that light has to bounce off your target (and cannot go directly from the LED to the sensor). 

Using a Class for Color
Colors are three values, one for red, green and blue. Passing three values around can be cumbersome and it's only possible to return one value in C. Fortunately that one value can be a class! Use a class to represent your colors. Start with this code in the header of your class: 

#ifndef COLOR_H 
#define COLOR_H

#include <WString.h>
#include <HardwareSerial.h>

class Color {

public:

  // This constructor initializes the Color to black (r,g,b all zero)
  Color(); 

  // This constructor to initialize a particular color, the r, g, and b
  // arguments are to be assigned to _red, _green and _blue. 
  Color(int r, int g, int b);

  int getRed() const;
  int getGreen() const;
  int getBlue() const;

  void setRed(int r);
  void setGreen(int g);
  void setBlue(int b);
  void setColor(int r, int g, int b);

  String getDominantColor() const;
  
private:

  // each value is the "on-ness" of a color (0 to 255)
  int _red, _green, _blue; 
};

#endif

You will implement each of the member functions in your C file. The getX() functions should return the value of the corresponding color and the setX() functions should set the value. The setColor() function should set all red, green and blue values. The getDominantColor() function should return the dominant color in String form: 

if maximum of (red, green, blue) is red 
  return "Red"
else if maximum of (red, green, blue) is green 
  return "Green"
else if maximum of (red, green, blue) is blue 
  return "Blue" 
else 
  return "Muddy"

Pro Tip

To simplify your code you can create constant colors that have names. For example, using the class above you can do this in the main part of your program:

const static Color red   (255, 0,   0  );
const static Color green (0,   255, 0  );
const static Color blue  (0,   0,   255);

Operation

Your project should be triggered using the serial port. When any key is pressed your project should measure the color then print the result. Here's an example of how it should work: 

To test the color press any key
<enter>
The color I see is: Blue 

You don't need a button like I had in class.

Extra Credit 

Your color class can be much more useful. For extra credit make a function that let's you add one color to another. Adding colors means adding the three separate components of the colors together. For the first part of the extra credit add the following function to your class and implement it: 

void addColor(const Color &other);

The addColor() function takes another color class and adds it's RGB values to the current class. Here's an example of how you would use it in your program: 

Color mycolor(0,0,0); // mycolor is now black 
mycolor.addColor(red); // mycolor is now red 
mycolor.addColor(green); // mycolor is now yellow!

Extra Extra Credit 

C++ has a feature called operator overloading. Operator overloading lets you use built-in operations (like addition) in a way that makes sense for your class. For the extra, extra credit add the following function to your class and implement it: 

Color operator+(const Color &other) const 

Your operator+() function should according to the following pseudocode:

function operator+ : 
    newc : Create a new color that's black 
    newc.red = red + other.red 
    newc.green = green + other.green 
    newc.blue = blue + other.blue 
    return newc 

If you properly implement your operator+() function the following code will work: 

Color yellow = red + green; 

Verify you get the correct colors using the LED.

Grading Criteria (15 pts, 10 EC)

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Projects 

  • 10: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications
    • Runs as required with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 8: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 5: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or not original work

Extra Credit (x2) :

  • 5 your addColor() function is present and correct.
  • 5 your operator+() function is present and correct. 

Project Coding Style

  • 3: All code is well-documented and follows the specified coding styles
  • 2: Code has a minor documentation error
  • 1: Code has multiple documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

Total possible: 25

How to Submit

Upload all project files to Canvas:

  1. A zipfile that contains:
    1. colortest.ino
    2. Color.h
    3. Color.cpp
You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline, but must resubmit all your assignment files.

Comments