How does the Piano Glove translate a color into a tone?

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zackboston
 
Posts: 129
Joined: Mon Feb 25, 2013 12:47 pm

How does the Piano Glove translate a color into a tone?

Post by zackboston »

Hello!

Our teenagers have built the Piano Glove for a #BlackLivesMatter art + technology teaching activity they are planning.

I understand how the Flora works with the Color Sensor and the RGB LED and can explain this to the youth. However, I am not an expert coder and am finding it difficult to translate how the Flora turns the RGB values into a Tone. I need to be able to do this in simple language.

It looks like it tries to "see" whether the R or G or B value is the highest, then goes on to generate the tone from the highest value. Is that accurate?

If you get me on the right path, I can probably figure out how to read the code from there. . . or if you can point me to the crucial parts of the code to read in order to understand how it translates into tones, that could work too.

Thanks. They have the circuit working (only took three weeks!) but they have questions about the piano tones that I cannot yet answer. . . so glad they are curious.

User avatar
adafruit_support_bill
 
Posts: 88089
Joined: Sat Feb 07, 2009 10:11 am

Re: How does the Piano Glove translate a color into a tone?

Post by adafruit_support_bill »

Hello again. Hope you are dug-out from the latest storm!

I think you have the basic idea. But since not all colors are pure primary colors, there is a little extra processing to try to reduce the R, G & B reading into a primary color.

In this bit of code, he determines the lowest of the R, G and B values (remove) and calculates the largest difference in intensity between that and the other two readings (normalize)

Code: Select all

  float remove, normalize;
  if ((b < g) && (b < r)) {
    remove = b;
    normalize = max(r-b, g-b);
  } 
  else if ((g < b) && (g < r)) {
    remove = g;
    normalize = max(r-g, b-g);
  } 
  else {
    remove = r;
    normalize = max(b-r, g-r);
  }
The lowest color is subtracted out - since it can't contribute to a 'primary' color.

Code: Select all

  // get rid of minority report
  float rednorm = r - remove;
  float greennorm = g - remove;
  float bluenorm = b - remove;
Then the values are 'normalized' to a value between 0 and 1 for use in selecting a tone.

Code: Select all

  // now normalize for the highest number
  rednorm /= normalize;
  greennorm /= normalize;
  bluenorm /= normalize;

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”