Answers.Body: <p>There will certainly be some <em>mechanical</em> and <em>robotics</em> 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?")</p>
  
  <p>From a <em>programming</em> perspective, 49 possible options is kindof a middle ground.  </p>
  
  <p>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 <em>like</em> that could work quite well. </p>
  
  <p>You could just have a lookup table. Or a lookup table of lookup tables. In a C-like language that would look something like</p>
  
  <pre><code>table = [
    'red':  ['red': -90, 'blue': -45],
    'blue': ['red': +90, 'blue': +45]
  ];
  turn_degree = table[first_color][second_color];
  </code></pre>
  
  <p>I would call that the "normal" approach, although you're getting toward the upper size of the table one might really want. </p>
  
  <p>You could also treat one color as a modifier for the first:</p>
  
  <pre><code>first_table = ['red': -90, 'blue': +90];
  second_table = ['red': -45, 'blue': +45];
  turn_degree = first_table[first_color] + second_table[second_color];
  </code></pre>
  
  <p>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. </p>
  
  <p>A robotics consideration: Will the car <em>always</em> correctly sense which color is the "first" color?</p>
  
  <p>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. </p>
  
  <p><strong>But seriously, how should you do this in a classroom?</strong></p>
  
  <ul>
  <li>If either sensor sees red, the car will turn left.</li>
  <li>If either sensor sees blue, the car will turn right.</li>
  <li>If either sensor sees yellow, the car will turn 15°.</li>
  <li>If either sensor sees green, the car will turn 45°.</li>
  <li>If either sensor sees orange, the car will turn 60°.</li>
  <li>If either sensor sees purple, the car will turn 90°.</li>
  <li>If either sensor sees black, the car will turn 135°.</li>
  <li>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.</li>
  </ul>
  
  <pre><code>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]);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-06T17:46:27.557Z
Answers.Text: Thanks alot for your professional approach of my case, for me i was thinking in a way that the two sensors will see different colors at the same time, like there is a line cutting the road which is made up of two distinct colors, for example brown at one end and red on the other. My question is that how to tell the robot to do a certain task when he detects this combination, in my project the task is that the robot must turn right 120 degrees for brown and red.
Answers.UserDisplayName: Huss ibra
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-06T17:47:34.787Z
Answers.Text: I was thinking in a way that if cs1 = brown , and cs2 =red, go 120 right.
Answers.UserDisplayName: Huss ibra
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-06T17:48:18.133Z
Answers.Text: Its going to be nested if conditions i think, what do you think?
Answers.UserDisplayName: Huss ibra
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-06T18:14:13.800Z
Answers.Text: Nesting `if`s is fine, but if you find yourself nesting _similar_ `if` statements then that suggest there may be a better way.  Sequential `if`s, `else`s, and `else if`s will at least keep your code flat. `switch` statements usually aren't worth the bother, but some people like them. If you can avoid `if`s etc ("branching") altogether, then you probably should.
Answers.UserDisplayName: ShapeOfMatter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-06T16:49:25.643Z
Answers.LastActivityDate: 2019-03-06T16:49:25.643Z
Answers.UserDisplayName: ShapeOfMatter
Body: <p>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.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-06T18:15:58.453Z
Comments.Text: If you've already tried anything, then you should edit your question to explain what you've already tried. Consider sharing your work-in-progress code.
Comments.UserDisplayName: ShapeOfMatter
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-06T19:25:09.673Z
Comments.Text: I am still working on this project, i will be glad to share it's code when its done or partially done. Thanks for interest
Comments.UserDisplayName: Huss ibra
Tags: <ev3><programming><sensor-colour>
Title: Using two color sensors simultaneously
UserDisplayName: Huss ibra
ThreadText: # Using two color sensors simultaneously
  
  (Asked by: username_0 on 2019-03-06)
  
  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.
  
  **Tags:** ev3, programming, sensor-colour
  > **Comments:**
  > **username_1**: If you've already tried anything, then you should edit your question to explain what you've already tried. Consider sharing your work-in-progress code.  
  > **username_0**: I am still working on this project, i will be glad to share it's code when its done or partially done. Thanks for interest  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-03-06. Score: 2
  
  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]);
  }
  
  > **Comments:**
  > **username_0**: Thanks alot for your professional approach of my case, for me i was thinking in a way that the two sensors will see different colors at the same time, like there is a line cutting the road which is made up of two distinct colors, for example brown at one end and red on the other. My question is that how to tell the robot to do a certain task when he detects this combination, in my project the task is that the robot must turn right 120 degrees for brown and red.  
  > **username_0**: I was thinking in a way that if cs1 = brown , and cs2 =red, go 120 right.  
  > **username_0**: Its going to be nested if conditions i think, what do you think?  
  > **username_1**: Nesting `if`s is fine, but if you find yourself nesting _similar_ `if` statements then that suggest there may be a better way.  Sequential `if`s, `else`s, and `else if`s will at least keep your code flat. `switch` statements usually aren't worth the bother, but some people like them. If you can avoid `if`s etc ("branching") altogether, then you probably should.  
  
  ---
  
  