How to use a local variable that i get from noteOn() in draw() ? [using midibus]

edited March 2018 in Library Questions

Hello everybody ! First time posting here I hope I'm in the right section. I'm looking to program a tool for live vjing : the finale idea is to be able to switch between different audio/video visualisators live with a midi controller... However I'm starting small. As of right now my goal is to understand how void works.

I am using "void noteOn(int channel, int pitch, int velocity)" to get the value of the local variable "pitch" How can i use this local variable in draw() ? How can i make the variable global but still get the value from void noteOn ?

Thanks !

Here's my code :

import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus

void setup() {
  size(400, 400);background(0);

  myBus = new MidiBus(this, 4, -1); // Create a new MidiBus with my 4th hardware device as input and nothing as the output device.

void draw() {
  int channel = 0;
  int pitch = 64;
  int velocity = 127;

  myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn
  myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff

  int number = 0;
  int value = 90;
  int trans_value=0;

  myBus.sendControllerChange(channel, number, value); // Send a controllerChange


void noteOn(int channel, int pitch, int velocity) {
  // Receive a noteOn
  println("Note On:");

  if (pitch == 68) {
      fill(204, 102, 0); rect(30, 20, 55, 55); **// this needs to go to draw, but i'm using "pitch" to trigger it and draw don't now pitch...**

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


  • Just create a sketch-level variable and then set it in the function. Something like this:

    int myVariable;
    void draw(){
    void setVariable(int newVariable){
      myVariable = newVariable;

    If they have the same name, you can use the this keyword to differentiate the sketch-level variable from the local variable.

  • Thank you Kevin this is the perfect answer !

Sign In or Register to comment.