Help with school piano stairs project


#1

Hi all
I am new to processing, but I need to use processing to complete a school electronic project.
I am trying to clone a project by Helen Bang on Instructables (https://www.instructables.com/id/Arduino-IR-Musical-Stairs/), but I realized that the processing code wouldn’t work with my computer. So,
I need a program that uses the information on the serial port of my Arduino to play piano sounds, and I have no clue to how to code it. This is also pretty urgent so could someone help?

Thank you!


#2

Why won’t the Processing code work with your computer?

What code have you tried?
What is the issue?
Connecting your sensors?
Reading values from your sensors?
Making sounds?
Are you getting errors? Or what?!?

Without more details… like the code that you are trying… and what it isn’t doing properly… and what you think it should be doing… we are at a TOTAL LOSS as to how to help you.

Double lemon loss! :lemon: :lemon:


#3

O.K
The 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;
  }
}```

#4

First think to verify is if you use the appropriate port number:

 port = new Serial(this, Serial.list()[4], 9600);

This usually change on other computer so check if [4] is the good one if not try the other in the list.
It might be diferent if you run Mac Os ,Windows or Linux.