#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# MidiBus constant tone

edited January 2016

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.*;

// Serial variables
Serial port;
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(), 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");
}

void draw() {

print("A ");
print(toneA);

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
// 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;}
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

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); // A
println(splitted); // B
println(splitted); // C
println(splitted); // D

// Convert the string to an integer and add it to the right variable to use
// in our applicaton.

}
}

void delay(int time) {
int current = millis();
while (millis () < current+time) Thread.yield();
}
``````
Tagged:
{"ft_lang_label":"__label__en","ft_lang_prob":0.6893445,"math_prob":0.93633777,"size":3948,"snap":"2019-43-2019-47","text_gpt3_token_len":1106,"char_repetition_ratio":0.1305781,"word_repetition_ratio":0.028286189,"special_character_ratio":0.30015197,"punctuation_ratio":0.1584022,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9642047,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-21T10:45:34Z\",\"WARC-Record-ID\":\"<urn:uuid:2803c119-48d0-4d71-9830-16a6328ef6b1>\",\"Content-Length\":\"42512\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7754d68a-31b3-4430-a647-afd670c86b50>\",\"WARC-Concurrent-To\":\"<urn:uuid:889104e8-dd88-48d2-86b3-35122d8cff01>\",\"WARC-IP-Address\":\"45.33.79.91\",\"WARC-Target-URI\":\"https://forum.processing.org/two/discussion/comment/59930/\",\"WARC-Payload-Digest\":\"sha1:UBNSKXN22ORWBCSVU7E4SP6K5LQYQ5FL\",\"WARC-Block-Digest\":\"sha1:IFOPVGMKENZFBLUMY7GICKOT2GQ32Y5P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496670770.21_warc_CC-MAIN-20191121101711-20191121125711-00472.warc.gz\"}"}