MidiBus constant tone

edited January 2016 in Library Questions

I have tried some things, but I won't get it working for me. I am using Serial with arduino to receive data from three distance sensors. If they measure something I want a tone to play until they measure something else. Compared with a piano, a note plays when you press a key (the distance sensor detects your hand) until you release the key (the distance sensor detects nothing again).

The problem is I only hear the tone repeat a lot instead of just one tone until your hand is removed.

The code is down below, as you can see I only made it for sensor A yet. Thanks for helping!

import processing.serial.*;

import themidibus.*;

MidiBus myBus; //Your MidiBus object

// Serial variables
Serial port;
int lineNumberReceived = 0;
String ArduinoData;       // The string of arduino Data which will be cut in pieces below
// Data pieces from the arduino
int receivedA_value = 0;  // Distance Sensor A
int receivedB_value = 0;  // Distance Sensor B
int receivedC_value = 0;  // Distance Sensor C
int receivedD_value = 0;  // Button D (To change tones)
int toneA=0; // Tone from distance sensor A
int toneB=0; // ** B
int toneC=0; // ** C
int sensor = 400; // Calibration of the sensors

//creating the channels, pitch and velocity variables for every sensor.
int sensors = 3;
int sensA=0, sensB=0, sensC=0;
int play[] = new int[sensors];
int channel[] = new int[sensors];
int pitch[] = new int[sensors];
int velocity[] = new int[sensors];





void setup() {
  size(50, 50);
  background(0);
  // Configuring the serial port and connecting with the Arduino
    port = new Serial(this, Serial.list()[1], 57600); // making serial contact
    port.bufferUntil('\n');

  // Configuring the MidiBus and connect with the GarageBand IAC MIDI port
    MidiBus.list();
    myBus = new MidiBus(this, "", "To GarageBand"); 
    //Initialize your MidiBus Object
}

void draw() {
     checkA(receivedA_value);

  print("A ");
  print(toneA);
  println(receivedA_value);


  if (toneA!=0){
    toneA(toneA);
  }
   if (toneB!=0){
  // toneB(toneB);
  }
  if (toneC!=0){
  // toneC(toneC);
  }


  delay(250);

}
// 
// Check Tones Functions
void checkA(int A){
  if (A > sensor){
    toneA=1;
  } else { toneA=0; }
}

// Create the tones
void toneA(int A){
  // Check if button D is pressed or not
    if(receivedD_value==0){
      // Check if tone A should play
        if (A == 1){
          channel[sensA]= 0;
          pitch[sensA]=43;
          velocity[sensA]=50;
          play[sensA] = 1;
        } else { play[sensA] = 0;}
    } else if (receivedD_value==1){
      if (A==1){
        channel[sensA]= 0;
          pitch[sensA]=39;
          velocity[sensA]=50;
          play[sensA] = 1;
      } else { play[sensA] = 0;}
    }else { play[sensA] =0;}



    playNote(play[sensA],channel[sensA],pitch[sensA],velocity[sensA]);
}

void playNote(int on, int channel, int pitch, int velocity){
  if(on == 0){
    myBus.sendNoteOff(channel,pitch,velocity);
  }
  else { myBus.sendNoteOn(channel,pitch,velocity); 
  }
}


// Serial Event Function
void serialEvent (Serial port) { // Checks port when new serial event occurs

  ArduinoData  = port.readStringUntil('\n'); // Get the arduino data string of each line
  lineNumberReceived = lineNumberReceived + 1; // Counter

  if (lineNumberReceived > 2) // We skip the first two lines to prevent wrong data from the beginning.
  {
    //Split the code into pieces to each spacebar
    String[] splitted = split(ArduinoData, " "); 


    println(splitted[1]); // A
    println(splitted[3]); // B
    println(splitted[5]); // C
    println(splitted[7]); // D

  // Convert the string to an integer and add it to the right variable to use 
  // in our applicaton.
    receivedA_value = int(splitted[1]);  // A
    receivedB_value = int(splitted[3]);  // B
    receivedC_value = int(splitted[5]);  // C
    receivedD_value = int(splitted[7]);  // D

  }
}


void delay(int time) {
  int current = millis();
  while (millis () < current+time) Thread.yield();
}

Answers

Sign In or Register to comment.