Musical Stairs [TSOP38238 + IR LED]
This is a small scaled musical stairs! It was a project that had a deadline; had only two days to build it. So, it is a very small scaled project, but still serves its purpose!
IR Sensor (receiver) that I used is TSOP38238 from Adafruit, and the IR LEDs are also from Adafruit.
The little black boxes have the IR LEDs in them (made the boxes to help people avoid stepping on the LED), the sensors are all attached on different breadboards at the left end of the stairs. This has 4 active steps, one playing a single note on the piano. For this project, the notes are C,D,E,F, respectively from first step to the top step, and if you press the right arrow key on the computer (on processing), the notes all change to G,A,B,C, respectively from the top step to the lowest. Then, if you press the left arrow key again, the notes go back to C,D,E,F. So, even with these small piano stairs, you can play the whole scale from C to C (one octave)!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To make the Arduino Code work for your sensors, you have to follow the following:
1. Download the IRremote library from: https://github.com/shirriff/Arduino-IRremote and move the library to your Arduino libraries folder.
- It is a library made by Ken Shirriff; it is an excellent library for IR sensors.
2. Open the IRremoteInt.h file.
3. If you scroll down a little, you fill find a section like this: (if you are using the Arduino Uno, and the following Arduinos)
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#define IR_USE_TIMER1
// #define IR_USE_TIMER2
// #define IR_USE_TIMER3
3. Then, uncomment the lower two #define lines, so that now you have:
#define IR_USE_TIMER1
#define IR_USE_TIMER2
#define IR_USE_TIMER3
4. Save the file, and now, you can use all the PWM pins to activate your LEDs. (Those include 3, 5, 6, 9, 10, 11 digital pins).
5. When giving a file for the processing file, you have to remember to add the data folder with your processing file.
- The data file consists of the soundbank-deluxe.gm file.** The file: soundbank-deluxe.gm can be found: http://www.oracle.com/technetwork/java/soundbanks-135798.html
IR Sensor (receiver) that I used is TSOP38238 from Adafruit, and the IR LEDs are also from Adafruit.
The little black boxes have the IR LEDs in them (made the boxes to help people avoid stepping on the LED), the sensors are all attached on different breadboards at the left end of the stairs. This has 4 active steps, one playing a single note on the piano. For this project, the notes are C,D,E,F, respectively from first step to the top step, and if you press the right arrow key on the computer (on processing), the notes all change to G,A,B,C, respectively from the top step to the lowest. Then, if you press the left arrow key again, the notes go back to C,D,E,F. So, even with these small piano stairs, you can play the whole scale from C to C (one octave)!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To make the Arduino Code work for your sensors, you have to follow the following:
1. Download the IRremote library from: https://github.com/shirriff/Arduino-IRremote and move the library to your Arduino libraries folder.
- It is a library made by Ken Shirriff; it is an excellent library for IR sensors.
2. Open the IRremoteInt.h file.
3. If you scroll down a little, you fill find a section like this: (if you are using the Arduino Uno, and the following Arduinos)
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#define IR_USE_TIMER1
// #define IR_USE_TIMER2
// #define IR_USE_TIMER3
3. Then, uncomment the lower two #define lines, so that now you have:
#define IR_USE_TIMER1
#define IR_USE_TIMER2
#define IR_USE_TIMER3
4. Save the file, and now, you can use all the PWM pins to activate your LEDs. (Those include 3, 5, 6, 9, 10, 11 digital pins).
5. When giving a file for the processing file, you have to remember to add the data folder with your processing file.
- The data file consists of the soundbank-deluxe.gm file.** The file: soundbank-deluxe.gm can be found: http://www.oracle.com/technetwork/java/soundbanks-135798.html
Arduino Code:
#include <IRremote.h>
#define PIN_IR 10
#define PIN_DETECT 12
#define PIN_IR2 9
#define PIN_DETECT2 11
#define PIN_IR3 9
#define PIN_DETECT3 7
#define PIN_IR4 5
#define PIN_DETECT4 2
IRsend irsend;
void setup() {
Serial.begin(9600);
pinMode(PIN_DETECT, INPUT);
pinMode(PIN_DETECT2, INPUT);
pinMode(PIN_DETECT3, INPUT);
pinMode(PIN_DETECT4, INPUT);
pinMode(PIN_IR, OUTPUT);
pinMode(PIN_IR2, OUTPUT);
pinMode(PIN_IR3, OUTPUT);
pinMode(PIN_IR4, OUTPUT);
digitalWrite(PIN_IR, HIGH);
digitalWrite(PIN_IR2, HIGH);
digitalWrite(PIN_IR3, HIGH);
digitalWrite(PIN_IR4, HIGH);
irsend.enableIROut(38);
irsend.mark(0);
}
void loop() {
Serial.print(digitalRead(PIN_DETECT));Serial.print(", ");
Serial.print(digitalRead(PIN_DETECT2));Serial.print(", ");
Serial.print(digitalRead(PIN_DETECT3));Serial.print(", ");
Serial.print(digitalRead(PIN_DETECT4));Serial.print(", ");
Serial.println("");
delay(100);
}
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 = 0; // set the instrument as the piano
String currentInstrument = "nothing selected yet";
PFont font;
float x,t,z,u;
int y, c, d, e;
int currentStateX, currentStateT, currentStateZ, currentStateU ;
int previousStateX, previousStateT, previousStateZ, previousStateU;
int n = 100;
float [] xdata = new float[n];
float [] tdata = new float[n];
float [] zdata = new float[n];
float [] udata = new float[n];
void setup() {
size (400, 400);
background (127);
// println(Serial.list());
// if you don't know your serial port number,
// uncomment so you can plug in the right number for the next line (Serial.list()[number])
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);
noStroke();
}
void serialEvent(Serial port) {
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
println(inString);
try {
String[] data = split(inString, ",");
x = float (data[0]);
t = float (data[1]);
z = float (data[2]);
u = float (data[3]);
long noteStartTime = millis();
if (x == 1){
midiNote = 60;
currentStateX = 1;
}
else if (x == 0) {
currentStateX = 0;
}
if (currentStateX != previousStateX){
if (currentStateX == 1){
channel.noteOn(midiNote + c, 64);
prevNote = midiNote + c;
}
if (currentStateX == 0){
if (millis() - noteStartTime > 10) {
channel.noteOff(prevNote);
}
}
}
else if (currentStateX == previousStateX) {
}
previousStateX = currentStateX;
if (t == 1){
midiNote = 62;
currentStateT = 3;
}
else if (t == 0) {
currentStateT = 4;
}
if (currentStateT != previousStateT){
if (currentStateT == 3){
channel.noteOn(midiNote + d, 64);
prevNote = midiNote + d;
}
if (currentStateT == 4){
if (millis() - noteStartTime > 10) {
channel.noteOff(prevNote);
}
}
}
else if (currentStateT == previousStateT) {
}
previousStateT = currentStateT;
if (z == 1){
midiNote = 64;
currentStateZ = 5;
}
else if (z == 0) {
currentStateZ = 6;
}
if (currentStateZ != previousStateZ){
if (currentStateZ == 5){
channel.noteOn(midiNote + e,64);
prevNote = midiNote + e;
}
if (currentStateZ == 6){
if (millis() - noteStartTime > 10) {
channel.noteOff(prevNote);
}
}
}
else if (currentStateZ == previousStateZ) {
}
previousStateZ = currentStateZ;
if (u == 1){
midiNote = 65;
currentStateU = 7;
}
else if (u == 0) {
currentStateU = 8;
}
if (currentStateU != previousStateU){
if (currentStateU == 7){
channel.noteOn(midiNote + y,64);
prevNote = midiNote + y;
}
if (currentStateU == 8){
if (millis() - noteStartTime > 10) {
channel.noteOff(prevNote);
}
}
}
else if (currentStateU == previousStateU) {
}
previousStateU = currentStateU;
redraw();
}
catch (Exception e) { e.printStackTrace(); }
}
}
void keyPressed() {
if (keyCode == 39){ // right arrow button
c += 12;
d += 9;
e += 5;
y += 2;
}
if (keyCode == 37) { // left arrow button
c -= 12;
d -= 9;
e -= 5;
y -= 2;
}
}