Colour and Melody –Physical computing homework

Try to mapping between colour and the melody. The colour will change according you play the different note. I used a touch sensor, LED colour and play sound using another program.

 

The example of my work.

 

The source codeĀ 

#include <MPR121.h>
#include <Wire.h>
#define numElectrodes 12
//—LED
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 11

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter–see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second
int pinTouch=0;
int r,g,b;
void setup()
{
pixels.begin(); // This initializes the NeoPixel library.
//touch sensor
Serial.begin(9600);
while(!Serial); // only needed if you want serial feedback with the
// Arduino Leonardo or Bare Touch Board

Wire.begin();

// 0x5C is the MPR121 I2C address on the Bare Touch Board
if(!MPR121.begin(0x5A)){
Serial.println(“error setting up MPR121”);
switch(MPR121.getError()){
case NO_ERROR:
Serial.println(“no error”);
break;
case ADDRESS_UNKNOWN:
Serial.println(“incorrect address”);
break;
case READBACK_FAIL:
Serial.println(“readback failure”);
break;
case OVERCURRENT_FLAG:
Serial.println(“overcurrent on REXT pin”);
break;
case OUT_OF_RANGE:
Serial.println(“electrode out of range”);
break;
case NOT_INITED:
Serial.println(“not initialised”);
break;
default:
Serial.println(“unknown error”);
break;
}
while(1);
}

// pin 4 is the MPR121 interrupt on the Bare Touch Board
MPR121.setInterruptPin(2);

// this is the touch threshold – setting it low makes it more like a proximity trigger
// default value is 40 for touch
MPR121.setTouchThreshold(40);

// this is the release threshold – must ALWAYS be smaller than the touch threshold
// default value is 20 for touch
MPR121.setReleaseThreshold(20);

// initial data update
MPR121.updateTouchData();
}

void loop()
{
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();

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

if(MPR121.isNewTouch(i)){
//Serial.print(“electrode “);
Serial.println(i, DEC);
pinTouch=i;

} else if(MPR121.isNewRelease(i)){
//Serial.print(“electrode “);
if (i==0) { Serial.println(-15); }
else {Serial.println(-i, DEC);}

}
}
}

//—— LED color setting——
switch ( pinTouch ) {
case 1: r=255; g=0; b=0;break;
case 2: r=255; g=100; b=0;break;
case 3: r=255; g=0; b=100;break;
case 4: r=0; g=255; b=0;break;
case 5: r=255; g=255; b=0;break;
case 6: r=0; g=255; b=100;break;
case 7: r=0; g=0; b=255;break;
default: r=0;b=0,g=0;break;
} //end case

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

// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(r,g,b)); // Moderately bright green color.

pixels.show(); // This sends the updated pixel color to the hardware.

}//end for

} //end loop