Skip to content

@ericskiff /pianoBox_v2.pde
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
PianoBox v2
#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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.