Interactive assignment – Juan Rivera

“Piano Sleeve”

CODE:

//

#include <Wire.h>
#include “Adafruit_MPR121.h”
#include “pitches.h”

#define BUZZER_PIN A0 // The output pin for the piezo buzzer
#define NUM_OF_SAMPLES 5 // Higher number whens more delay but more consistent readings

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are ‘released’
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
int note;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
int notes[] = {NOTE_A4,NOTE_AS4,NOTE_B4,NOTE_C4, NOTE_D4, NOTE_DS4,NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_C5, NOTE_D5 }; // C-Major scale
//int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale
//int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale

//#include <CurieBLE.h>
//
//#define TXRX_BUF_LEN 20 //max number of bytes
//#define RX_BUF_LEN 20 //max number of bytes
//uint8_t rx_buf[RX_BUF_LEN];
//int rx_buf_num, rx_state = 0;
//uint8_t rx_temp_buf[20];
//uint8_t outBufMidi[128];

//Buffer to hold 5 bytes of MIDI data. Note the timestamp is forced
//uint8_t midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00};

//Loads up buffer with values for note On
//void noteOn(char chan, char note, char vel) //channel 1
//{
// midiData[2] = 0x90 + chan;
// midiData[3] = note;
// midiData[4] = vel;
//}
//
////Loads up buffer with values for note Off
//void noteOff(char chan, char note) //channel 1
//{
// midiData[2] = 0x80 + chan;
// midiData[3] = note;
// midiData[4] = 0;
//}
//
//BLEPeripheral midiDevice; // create peripheral instance
//
//BLEService midiSvc(“03B80E5A-EDE8-4B33-A751-6CE34EC4C700”); // create service
//
//// create switch characteristic and allow remote device to read and write
//BLECharacteristic midiChar(“7772E5DB-3868-4112-A1A9-F2669D106BF3”, BLEWrite | BLEWriteWithoutResponse | BLENotify | BLERead, 5);

void setup() {
Serial.begin(9600);

// BLESetup();
// Serial.println((“Bluetooth device active, waiting for connections…”));
//
// while (!Serial) { // needed to keep leonardo/micro from starting too fast!
// delay(10);

// Set the buzzer as an output:

pinMode(BUZZER_PIN, OUTPUT);
// Serial.println(“Adafruit MPR121 Capacitive Touch sensor test”);

// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println(“MPR121 not found, check wiring?”);
while (1);
}
Serial.println(“MPR121 found!”);
}

//void BLESetup()
//{
// // set the local name peripheral advertises
// midiDevice.setLocalName(“Auxren”);
// midiDevice.setDeviceName(“Auxren”);
//
// // set the UUID for the service this peripheral advertises
// midiDevice.setAdvertisedServiceUuid(midiSvc.uuid());
//
// // add service and characteristic
// midiDevice.addAttribute(midiSvc);
// midiDevice.addAttribute(midiChar);
//
// // assign event handlers for connected, disconnected to peripheral
// midiDevice.setEventHandler(BLEConnected, midiDeviceConnectHandler);
// midiDevice.setEventHandler(BLEDisconnected, midiDeviceDisconnectHandler);
//
// // assign event handlers for characteristic
// midiChar.setEventHandler(BLEWritten, midiCharacteristicWritten);
// // set an initial value for the characteristic
// midiChar.setValue(midiData, 5);
//
// // advertise the service
// midiDevice.begin();
//}

void loop() {
// Get the currently touched pads
currtouched = cap.touched();

for (uint8_t i = 0; i < 12; i++) {
// if it *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(” touched”);
tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed
// note = map(i, 0, 11, 30, 100);
// noteOn(0, note, 127); //loads up midiData buffer
// midiChar.setValue(midiData, 5);//midiData); //posts 5 bytes
// delay(8);
//
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(” released”);
noTone(BUZZER_PIN);
// noteOff(0, note);
// midiChar.setValue(midiData, 5);//midiData); //posts 5 bytes
// delay(8);
}
}
// reset our state
//Serial.println(currtouched);
lasttouched = currtouched;
// buttonState = reading;
delay(100);

//int note = map(currtouched, 0, 4096, 0, 127) + 50);
//int note = random(((currtouched*10)-10), ((currtouched+10)*5));
// Serial.println(note);
//readMIDI();
}

//void midiDeviceConnectHandler(BLECentral& central) {
// // central connected event handler
// Serial.print(“Connected event, central: “);
// Serial.println(central.address());
//}
//
//void midiDeviceDisconnectHandler(BLECentral& central) {
// // central disconnected event handler
// Serial.print(“Disconnected event, central: “);
// Serial.println(central.address());
//}
//
//void midiCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// // central wrote new value to characteristic, update LED
// Serial.print(“Characteristic event, written: “);
//}

 

Materials:

  • Copper tape
  • red lose wire
  • green lose wire
  • adafruit MPR121 capacitive touch sensors
  • Aurduino UNO r3 Board
  • Tiny bread board
  • Tiny audio buzzer
  • 9V battery
  • 9V battery connector
  • compression sleeve

Pictures: