IR remotePiano
Parts needed: IR sensor (TSOP38238 used), infrared command remote (any kind)
- A speaker might make the sound better!
** Add the data file consisting the soundbank-deluxe.gm with your processing file.
** The file: soundbank-deluxe.gm can be found: http://www.oracle.com/technetwork/java/soundbanks-135798.html
- A speaker might make the sound better!
** Add the data file consisting the soundbank-deluxe.gm with your processing file.
** The file: soundbank-deluxe.gm can be found: http://www.oracle.com/technetwork/java/soundbanks-135798.html
Arduino Code:
#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 2;
boolean hasPrinted = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
// Serial.print("Start!");
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
// Serial.println(results.value, DEC);
delay (100);
irrecv.resume(); // Receive the next value
// first before doing anything,
// uncomment the Serial.println to receive the numbers for each button.
}
// seven number buttons
if (results.value == 3249195318){ // the numbers received when pressed 1
hasPrinted = false;
if (!hasPrinted){
Serial.println("1");
}
results.value = 0;
}
if (results.value == 3249162678){ // when pressed 2
hasPrinted = false;
if (!hasPrinted){
Serial.println("2");
}
results.value = 0;
}
if (results.value == 3249207558){
hasPrinted = false;
if (!hasPrinted){
Serial.println("3");
}
results.value = 0;
}
if (results.value == 3249174918){
hasPrinted = false;
if (!hasPrinted){
Serial.println("4");
}
results.value = 0;
}
if (results.value == 3249164718){
hasPrinted = false;
if (!hasPrinted){
Serial.println("5");
}
results.value = 0;
}
if (results.value == 3249205518){
hasPrinted = false;
if (!hasPrinted){
Serial.println("6");
}
results.value = 0;
}
if (results.value == 3249168798){
hasPrinted = false;
if (!hasPrinted){
Serial.println("7");
}
results.value = 0;
}
// left button
if (results.value == 3249196338){
hasPrinted = false;
if (!hasPrinted){
Serial.println("8");
}
results.value = 0;
}
// right button
if (results.value == 3249180018){
hasPrinted = false;
if (!hasPrinted){
Serial.println("9");
}
results.value = 0;
}
// up button
if (results.value == 3249147378){
hasPrinted = false;
if (!hasPrinted){
Serial.println("10");
}
results.value = 0;
}
// down button
if (results.value == 3249163698){
hasPrinted = false;
if (!hasPrinted){
Serial.println("11");
}
results.value = 0;
}
// enter button
if (results.value == 3249185118){
hasPrinted = false;
if (!hasPrinted){
Serial.println("12");
}
results.value = 0;
}
}
Processing Code:
import javax.sound.midi.*;
import processing.serial.*;
Serial port;
private Synthesizer synthesizer;
private MidiChannel channel; // channel we play on -- 10 is for percussion
private Instrument[] soundbankInstruments, synthesizerInstruments;
private int velocity = 64; // default volume is 50%
private int midiNote = -1;
private int prevNote = -1;
private int program = 73;
String currentInstrument = "nothing selected yet";
PFont font;
float x;
int y;
int n = 100;
float [] xdata = new float[n];
void setup() {
size (400, 400);
background (127);
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);
port.bufferUntil('\n'); // don't generate a serialEvent() unless you get a newline character:
Soundbank soundbank = null;
File file = new File(dataPath("soundbank-deluxe.gm"));
try {
soundbank = MidiSystem.getSoundbank(file);
synthesizer = MidiSystem.getSynthesizer( );
synthesizer.open();
channel = synthesizer.getChannels()[1];
}
catch (Exception e) { e.printStackTrace(); }
soundbankInstruments = soundbank.getInstruments();
synthesizer.loadAllInstruments(soundbank);
synthesizerInstruments = synthesizer.getLoadedInstruments();
// for (int i = 0; i < synthesizerInstruments.length; i++) {
// println(i + " " + synthesizerInstruments[i]);
// }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
void draw(){
background (127);
text(program + " " + currentInstrument, 10, height - 8);
text(midiNote, 100, 100);
noStroke();
}
void serialEvent(Serial port) {
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
println(inString);
try {
String[] data = split(inString, ",");
float x = float (data[0]);
xdata[0] = x;
// octaves
if (x == 10) { // up button (raise octave)
y += 12;
}
else if (x == 11) { // down button (lower octave)
y -= 12;
}
// notes
if (x == 1) { // C
midiNote = 60;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 2) { // D
midiNote = 62;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 3) { // E
midiNote = 64;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 4) { // F
midiNote = 65;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 5) { // G
midiNote = 67;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 6) { // A
midiNote = 69;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
else if (x == 7) { // B
midiNote = 71;
channel.noteOff(prevNote);
channel.noteOn(midiNote + y, 64);
prevNote = midiNote + y;
}
if (midiNote == -1){
channel.noteOff(prevNote);
}
// turn off all notes
if (x == 12) { // enter button
channel.allNotesOff();
}
// changing instruments
if (x == 9) { // right button
program++; if (program == 128) { program = 0; }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
else if (x == 8) { // left button
program--; if (program == -1) { program = 127; }
channel.programChange(program);
currentInstrument = synthesizerInstruments[program].toString();
}
redraw();
}
catch (Exception e) { e.printStackTrace(); }
}
}