Using two color sensors simultaneously I am new to the world of robotics, and I'm preparing a new project with my students. Our goal is to make a self driving robot that can read colored signs from the road in order to turn right or left a specific angle. For example, if the color sensor 1 detects red color, and at the same time the color sensor 2 detects blue color, the robot must turn 90 degrees right. What's the best programming approach to do this? Consider that the total number of combinations could be up to 49 since each color sensor can read 7 colors.
There will certainly be some mechanical and robotics questions that you'll need to figure out, such as where on the car to position the sensors, and how to ensure that the car is reliably reading both colors at every intersection. (Which brings up contextual questions like "are there discrete intersections?")

From a programming perspective, 49 possible options is kindof a middle ground.  

You could have a systematic representation. For example, each color could map to a digit, and so the pair represents a two digit base-7 number of 2π/49 radians (where 0 means go back the way you came). I don't actually suggest that particular scheme, but something like that could work quite well. 

You could just have a lookup table. Or a lookup table of lookup tables. In a C-like language that would look something like

table = [
  'red':  ['red': -90, 'blue': -45],
  'blue': ['red': +90, 'blue': +45]
];
turn_degree = table[first_color][second_color];


I would call that the "normal" approach, although you're getting toward the upper size of the table one might really want. 

You could also treat one color as a modifier for the first:

first_table = ['red': -90, 'blue': +90];
second_table = ['red': -45, 'blue': +45];
turn_degree = first_table[first_color] + second_table[second_color];


This is secretly the same as that first option I gave (base7), but more flexible, easier to read, and easier to explain to kids. And you could use multiplication instead of addition if you wanted. 

A robotics consideration: Will the car always correctly sense which color is the "first" color?

If not, then there's actually only 28 (21?) possible combinations. You can still use any of the above methods, but additionally you'll define an ordering of the colors (so for example, you'll just decide that green is "less than" blue). Then your "first" color will always be the lesser of the two colors, regardless of which sensor sensed what. 

But seriously, how should you do this in a classroom?


If either sensor sees red, the car will turn left.
If either sensor sees blue, the car will turn right.
If either sensor sees yellow, the car will turn 15°.
If either sensor sees green, the car will turn 45°.
If either sensor sees orange, the car will turn 60°.
If either sensor sees purple, the car will turn 90°.
If either sensor sees black, the car will turn 135°.
If the car doesn't see two colors, or if the colors don't clearly say what to do, then the car just keeps going forward.


first = get_sensor_one();
second = get_sensor_two();
directions = ['red': -1, 'blue': 1];
degrees = ['yellow': 15, 'green': 45, 'orange': 60, 'purple': 90, 'black': 135];
if(contains(directions, first)){
  direction_color = first;
  degree_color = second;
} else {
  direction_color = second;
  degree_color = first;
}
if(!contains(directions, direction_color) || !contains(degrees, degree_color)){
  keep_going();
} else {
  turn(degrees[degree_color] * direction[direction_color]);
}