Created
September 5, 2010 21:20
-
-
Save robb/566337 to your computer and use it in GitHub Desktop.
Big Piano Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Music Hackday 2010 | |
* Big Piano Hack | |
*/ | |
const int LED_PIN = 13; | |
const byte CHANNEL = 0; | |
/* The pin numbers in chromatic order | |
*/ | |
const int INPUT_PINS[] = {32, 30, 28, 26, 24, 22}; | |
const int BASE_PITCH = 60; | |
int **states; | |
int current = 0, previous = 1; | |
int NUMBER_OF_PINS; | |
void setup() { | |
// Obtain memory | |
NUMBER_OF_PINS = (sizeof(INPUT_PINS)/sizeof(int)); | |
states = (int **) malloc(2 * sizeof(int *)); | |
states[0] = (int *) malloc(NUMBER_OF_PINS * sizeof(int)); | |
states[1] = (int *) malloc(NUMBER_OF_PINS * sizeof(int)); | |
// Set pin modes | |
for (int i = 0; i < NUMBER_OF_PINS; i++) { | |
pinMode(INPUT_PINS[i], INPUT); | |
} | |
// go GO G0! | |
Serial.begin(115000); | |
} | |
void loop() { | |
for (int i = 0; i < NUMBER_OF_PINS; i++) { | |
int currentPin = INPUT_PINS[i]; | |
states[current][i] = digitalRead(currentPin); | |
if (states[current][i] == HIGH && states[previous][i] == LOW) | |
sendNoteOn(BASE_PITCH + i, 0x7F); | |
else if (states[current][i] == LOW && states[previous][i] == HIGH) | |
sendNoteOff(BASE_PITCH + i); | |
} | |
int tmp = current; | |
current = previous; | |
previous = tmp; | |
delay(50); | |
} | |
void sendNoteOn(byte pitch, byte velocity) { | |
sendMidiMessage(0x90 | CHANNEL, pitch, velocity); | |
} | |
void sendNoteOff(byte pitch) { | |
sendMidiMessage(0x80 | CHANNEL, pitch, 0x00); | |
} | |
void sendMidiMessage(byte cmd, byte data1, byte data2) { | |
Serial.print(cmd, BYTE); | |
Serial.print(data1, BYTE); | |
Serial.print(data2, BYTE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment