public
Created

PianoBox v2

  • Download Gist
pianoBox_v2.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#include <CapSense.h>
#include <Tone.h>
 
/*
Generates up to 5 simultaneous tones and controls a series of LEDs
based on input from a series of capacitive sensors.
Uses the CapSense (http://www.arduino.cc/playground/Main/CapSense)
and Tone libraries (http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation)
William E. Byrd and Catarina Mota
GNU GPL v.3 03-13-12
*/
 
int notes[] = { NOTE_C4,
NOTE_CS4,
NOTE_D4,
NOTE_DS4,
NOTE_E4,
NOTE_F4,
NOTE_FS4,
NOTE_G4,
NOTE_GS4,
NOTE_A4,
NOTE_AS4,
NOTE_B4
};
 
int leds[] = { 22,24,26,28,30,32,34,36,38,40,42,44 };
 
// Max number of simultaneous tones (Arduino Uno has 3 hardware timers,
// Arduino Mega has 6)
const int numNotePlayers = 5;
//Number of keys (capacitive sensors)
const int numCapSensors = 12;
 
// Change the debug flag to 'true' to print values read by the
// capacitive sensors.
const boolean debug = false;
 
Tone notePlayer[numNotePlayers];
 
int startingNotePin = 2;
 
int playerUsed[numCapSensors];
int capForPlayer[numNotePlayers];
 
// First capsense receive (sensor) pin
int startingCapPin = 53;
int capPinIncrement = -2;
// CapSense send pin
int comparisonPin = 23;
 
// Sensor threshold at which the tones and LEDs are triggered
int capThresh = 500;
 
int i;
 
//Assigns pins 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47 to each key
CapSense cs[] = { CapSense(comparisonPin,startingCapPin),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 1)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 2)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 3)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 4)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 5)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 6)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 7)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 8)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 9)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 10)),
CapSense(comparisonPin,startingCapPin + (capPinIncrement * 11))
};
 
void setup(void) {
Serial.begin(9600);
 
//initializes the 5 tone players on pins 2,3,4,5,6
for (i = 0; i < numNotePlayers; i++) {
notePlayer[i].begin(startingNotePin+i);
}
pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
}
 
 
void loop(void) {
if (debug) {
Serial.print(cs[0].capSense(30));
Serial.print("\t");
Serial.print(cs[1].capSense(30));
Serial.print("\t");
Serial.print(cs[2].capSense(30));
Serial.print("\t");
Serial.print(cs[3].capSense(30));
Serial.print("\t");
Serial.print(cs[4].capSense(30));
Serial.print("\t");
Serial.print(cs[5].capSense(30));
Serial.print("\t");
Serial.print(cs[6].capSense(30));
Serial.print("\t");
Serial.print(cs[7].capSense(30));
Serial.print("\t");
Serial.print(cs[8].capSense(30));
Serial.print("\t");
Serial.print(cs[9].capSense(30));
Serial.print("\t");
Serial.print(cs[10].capSense(30));
Serial.print("\t");
Serial.print(cs[11].capSense(30));
Serial.print("\t\n");
}
int currentPlayer = 0;
for (i = 0; i < numCapSensors; i++) { //check each capacitive sensor
if (currentPlayer < numNotePlayers) { //if there is a free player (i.e. no more than 5 tones currently playing)
if (cs[i].capSense(30) > capThresh) { //if one of the keys has been triggered
digitalWrite(leds[i], HIGH); //turn on the corresponding LED
notePlayer[currentPlayer].play(notes[i]); //play the corresponding tone on the next available player
currentPlayer++;
} else {
digitalWrite(leds[i], LOW); //turn off the corresponding LED
}
}
}
 
//Stops playing all other tones and turns off all other LEDs
for (i = currentPlayer; i < numNotePlayers; i++) {
notePlayer[i].stop();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.