MIDI doesnt work when I add buttons

This code triggers a midi note when a button is pressed.

#include <MIDI.h>

const int buttonPin = 13;
int buttonstate = 0; //Variable. Is the key currently pressed?
int noteisplaying = 0; //Variable. Is the Note currently playing?


void  setup() //The Setup Loop
{

  pinMode(buttonPin, INPUT);  //Set pin 13 to input to detect the key pre

  MIDI.begin(); //initialise midi library
}
//---------------------------------------------
void loop() //the main loop
{
  buttonstate = digitalRead(buttonPin); //read pin 13
  if (buttonstate == HIGH) { //the key on the board is pressed
    MIDI.sendNoteOn(36, 127, 1); // Send a Note (pitch 36 (C2), vel.127  ch. 1)
    noteisplaying = 1; // set the note playing flag to TRUE

  }
  else {

    if (noteisplaying) { //if the note is currently playing, turn it off
      MIDI.sendNoteOff(36, 0, 1); // Stop the note
      noteisplaying = 0; // clear the note is playing flag
    }
  }


}

It works great but I need to expand the number of buttons for my synthesizer project and whenever I try to add buttons the resulting MIDI notes sounds all messed up or just dont play at all. Heres the modified code I tried:

#include <MIDI.h>

const int buttonPin = 13;
const int buttonPin1 = 12;
int buttonstate = 0; //Variable. Is the key currently pressed?
int buttonstate1 = 0;
int noteisplaying = 0; //Variable. Is the Note currently playing?


void  setup() //The Setup Loop
{

  pinMode(buttonPin, INPUT);  //Set pin 13 to input to detect the key press
  pinMode(buttonPin1, INPUT); //Set pin 12 to input to detect the key press

  MIDI.begin(); //initialise midi library
}
//---------------------------------------------
void loop() //the main loop
{
  buttonstate = digitalRead(buttonPin); //read pin 13
  buttonstate1 = digitalRead(buttonPin1); //Read pin 12
  if (buttonstate == HIGH) { //the key on the board is pressed
    MIDI.sendNoteOn(36, 127, 1); // Send a Note (pitch 36 (C2), vel.127  ch. 1)
    noteisplaying = 1; // set the note playing flag to TRUE

  }
  else {

    if (noteisplaying) { //if the note is currently playing, turn it off
      MIDI.sendNoteOff(36, 0, 1); // Stop the note
      noteisplaying = 0; // clear the note is playing flag
    }
  }

  if (buttonstate1 == HIGH) { //the key on the board is pressed
    MIDI.sendNoteOn(37, 127, 1); // Send a Note (pitch 36 (C2), vel.127  ch. 1)
    noteisplaying = 1; // set the note playing flag to TRUE

  }
  else {

    if (noteisplaying) { //if the note is currently playing, turn it off
      MIDI.sendNoteOff(37, 0, 1); // Stop the note
      noteisplaying = 0; // clear the note is playing flag
    }
  }


}

Hi jeffd

You have one noteisplaying variable shared by the code for two switches. Does it help if you change to separate variables for each switch?

EDIT

You are also sending repeated note on messages for as long as a switch is pressed. Is that what you intended? If not, you could change the condition in the if statements to look for switch HIGH [u]and[/u] note is not currently playing.

Regards

Ray

It doesnt seem to matter if I add additional noteisplaying variables, the result is still all messed up. Could you elaborate on the repeated note on messages? Im not sure how to make those edits.

Your code sends a note on message each time round the loop for as long as the switch is closed. I guess you only want to send note on if a note is not already playing.

Change to this for each button (and use the relevant version of noteisplaying:

if ((buttonstate == HIGH) && !noteisplaying) { //the key on the board is pressed AND a note is not already playing

I tried your mod but it didn’t seem to work. Maybe I’ll make a video later showing the problem Im having. Essentially I just wanted to use this code that I found online and expand it to include more than one button, I didnt figure it would be so difficult! ha

#include <MIDI.h>
 
#define KEY 8
#define LED 13
 
int keyispressed = 0; //Variable. Is the key currently pressed?
int noteisplaying = 0; //Variable. Is the Note currently playing?
 
void  setup() //The Setup Loop
{
  pinMode(LED, OUTPUT);  //Set pin 13 , the led, to output
  pinMode(KEY, INPUT);  //Set pin 8 to input to detect the key press
  MIDI.begin(); //initialise midi library
}
//---------------------------------------------
void loop() //the main loop
{
  keyispressed = digitalRead(KEY); //read pin 8
 
  if (keyispressed == HIGH){ //the key on the board is pressed
    digitalWrite(LED, HIGH); //set the LED to on. 
    if(!noteisplaying){ //if the note is not already playing send MIDI instruction to start
       MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 36 (C2), vel.127  ch. 1)
       noteisplaying = 1; // set the note playing flag to TRUE
    }
  }
  else{
    digitalWrite(LED,LOW); // the key is not pressed. Turn off the LED
    if(noteisplaying){ //if the note is currently playing, turn it off
      MIDI.sendNoteOff(36,0,1);   // Stop the note
      noteisplaying = 0; // clear the note is playing flag
    }
  }
}

How are the input buttons wired ? Are you using any pulldown resistors so that when a button is not pressed the input voltage is not floating and possibly causing spurious inputs ?