Buttons too responsive in guitar rig

Hi i wondered if there was a way of pausing after a button is pressed. When im running my 4 button controller though guitar rig and ableton the buttons double click sometimes.

// include MIDI library
#include <MIDI.h>

// read digital pins number 2, 4, 7, 8
int pushButton2 = 2;
int pushButton4 = 4;
int pushButton7 = 7;
int pushButton8 = 8;

// checks if the button is pressed
int buttonState2 = 0;
int buttonState4 = 0;
int buttonState7 = 0;
int buttonState8 = 0;

// play/stop notes in relation to buttons pressed
int note2 = 0;
int note4 = 0;
int note7 = 0;
int note8 = 0;

// read potentiometer value
int analogValue = 0;
// maximum MIDI value is 127, first/previous potentiometer
// move has to always be different than previous value
int lastAnalogValue = 128;

void setup() {
delay (1000);
// slows connection so you dont have to reset board
MIDI.begin(4);
// 115200 hairless MIDI
Serial.begin(115200);
pinMode(pushButton2, INPUT);
pinMode(pushButton4, INPUT);
pinMode(pushButton7, INPUT);
pinMode(pushButton8, INPUT);
}

void loop() {
// read state of buttons
int buttonState2 = digitalRead(pushButton2);
int buttonState4 = digitalRead(pushButton4);
int buttonState7 = digitalRead(pushButton7);
int buttonState8 = digitalRead(pushButton8);

// Button 2

// when button pressed:
if (buttonState2 == HIGH) {
// if note not playing
if (note2 == 0) {
// play note (note number, velocity, channel)
// more info: Arduino MIDI Library: MIDI_Class Class Reference
// MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
// 55 = G3, 127 = trigger note with max volume
MIDI.sendControlChange(80,127,1);
// note is playing
note2 = 1;
}
// when button released
} else {
// if note playing
if (note2 == 1) {
// if playing - stop
MIDI.sendControlChange(80,0,1);
}
// if button released note is off
note2 = 0;
}

// Button 4

// when button pressed:
if (buttonState4 == HIGH) {
// if note not playing
if (note4 == 0) {
// play note (note number, velocity, channel)
// more info: Arduino MIDI Library: MIDI_Class Class Reference
// MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
// 55 = G3, 127 = trigger note with max volume
MIDI.sendControlChange(81,127,1);
// note is playing
note4 = 1;
}
// when button released
} else {
// if note playing
if (note4 == 1) {
// if playing - stop
MIDI.sendControlChange(81,0,1);
}
// if button released note is off
note4 = 0;
}

// Button 7

// when button pressed:
if (buttonState7 == HIGH) {
// if note not playing
if (note7 == 0) {
// play note (note number, velocity, channel)
// more info: Arduino MIDI Library: MIDI_Class Class Reference
// MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
// 55 = G3, 127 = trigger note with max volume
MIDI.sendControlChange(82,127,1);
// note is playing
note7 = 1;
}
// when button released
} else {
// if note playing
if (note7 == 1) {
// if playing - stop
MIDI.sendControlChange(82,0,1);
}
// if button released note is off
note7 = 0;
}

// Button 8

// when button pressed:
if (buttonState8 == HIGH) {
// if note not playing
if (note8 == 0) {
// play note (note number, velocity, channel)
// more info: Arduino MIDI Library: MIDI_Class Class Reference
// MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
// 55 = G3, 127 = trigger note with max volume
MIDI.sendControlChange(83,127,1);
// note is playing
note8 = 1;
}
// when button released
} else {
// if note playing
if (note8 == 1) {
// if playing - stop
MIDI.sendControlChange(83,0,1);
}
// if button released note is off
note8 = 0;
}

delay(1);
}

Need pulldowns or recode for pullups, and debounce

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You will probably need some debounce components on your buttons and possibly in your code.

Thanks.. Tom... :slight_smile:

Attached is the circuit. The resistors are 10k

Do you realize that you don't need those resistors? The Arduino has internal pull-up resistors.

What you need is button debouncing. It means that you ignore any changes the first tens of milliseconds after the button is pressed or released.

Take a look at my MIDI Controller library, button debouncing is built-in.

Pieter

I didn't know i didn't need the resistors. I just copied someones instructions but then found these issues. I dont know too much about coding yet

Is adding delay (130); on each button ok. It seems to have worked

Generally, no. It might work in some simple cases, but it will block the entire processor.

The right approach is to use millis() for timing, as demonstrated here:

Or here:
https://tttapa.github.io/Arduino/MIDI/Chap04-MIDI-Controller.html#buttons

How do i incorporate that in my code as its a bit beyond me

// include MIDI library
#include <MIDI.h>

// read digital pins number 2, 4, 7, 8
int pushButton2 = 2;
int pushButton4 = 4;
int pushButton7 = 7;
int pushButton8 = 8;

// checks if the button is pressed
int buttonState2 = 0;
int buttonState4 = 0;
int buttonState7 = 0;
int buttonState8 = 0;

// play/stop notes in relation to buttons pressed 
int note2 = 0;
int note4 = 0;
int note7 = 0;
int note8 = 0;

// read potentiometer value
int analogValue = 0;
// maximum MIDI value is 127, first/previous potentiometer
// move has to always be different than previous value
int lastAnalogValue = 128;

void setup() {
  delay (1000);
// slows connection so you dont have to reset board
  MIDI.begin(4);
  // 115200 hairless MIDI
  Serial.begin(115200);
  pinMode(pushButton2, INPUT);
  pinMode(pushButton4, INPUT);
  pinMode(pushButton7, INPUT);
  pinMode(pushButton8, INPUT);
}

void loop() {
  // read state of buttons
  int buttonState2 = digitalRead(pushButton2);
  int buttonState4 = digitalRead(pushButton4);
  int buttonState7 = digitalRead(pushButton7);
  int buttonState8 = digitalRead(pushButton8);
  

  // Button 2
  
  // when button pressed:
  if (buttonState2 == HIGH) {
   // if note not playing
    if (note2 == 0) {
      // play note (note number, velocity, channel)
      // more info: http://arduinomidilib.sourceforge.net/a00001.html
      // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
      // 55 = G3, 127 = trigger note with max volume
      MIDI.sendControlChange(80,127,1);
      // note is playing
      note2 = 1;
    }
  // when button released
  } else {
    // if note playing
    if (note2 == 1) {
      // if playing - stop
      MIDI.sendControlChange(80,0,1);
    }
    // if button released note is off
    note2 = 0;
  }
  

 // Button 4
  
  // when button pressed:
  if (buttonState4 == HIGH) {
    // if note not playing
    if (note4 == 0) {
      // play note (note number, velocity, channel)
      // more info: http://arduinomidilib.sourceforge.net/a00001.html
      // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
      // 55 = G3, 127 = trigger note with max volume
      MIDI.sendControlChange(81,127,1);
      // note is playing
      note4 = 1;
    }
  // when button released
  } else {
    // if note playing
    if (note4 == 1) {
      // if playing - stop
       MIDI.sendControlChange(81,0,1);
    }
    // if button released note is off
    note4 = 0;
  }

  
    // Button 7
  
  // when button pressed:
  if (buttonState7 == HIGH) {
    // if note not playing
    if (note7 == 0) {
      // play note (note number, velocity, channel)
      // more info: http://arduinomidilib.sourceforge.net/a00001.html
      // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
      // 55 = G3, 127 = trigger note with max volume
      MIDI.sendControlChange(82,127,1);
      // note is playing
      note7 = 1;
    }
  // when button released
  } else {
    // if note playing
    if (note7 == 1) {
      // if playing - stop
       MIDI.sendControlChange(82,0,1);
    }
    // if button released note is off
    note7 = 0;
  }

   // Button 8
  
  // when button pressed:
  if (buttonState8 == HIGH) {
    // if note not playing
    if (note8 == 0) {
      // play note (note number, velocity, channel)
      // more info: http://arduinomidilib.sourceforge.net/a00001.html
      // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
      // 55 = G3, 127 = trigger note with max volume
      MIDI.sendControlChange(83,127,1);
      // note is playing
      note8 = 1;
    }
  // when button released
  } else {
    // if note playing
    if (note8 == 1) {
      // if playing - stop
       MIDI.sendControlChange(83,0,1);
    }
    // if button released note is off
    note8 = 0;
  }
 
  delay(1);
}

Did you look at the examples that come with the MIDI Controller library?

Nuffdirty:
Is adding delay (130); on each button ok. It seems to have worked

If it seems to be working how you want it to work, you're fine.
Some people just like to argue their favorite way to skin a cat.

Nuffdirty:
Is adding delay (130); on each button ok. It seems to have worked

Not the most elegant way but if it works, it works. You should be able to lower that number, buttons typically bounce for 10-20 ms or so. You just have to try.

Just be aware that upon every button press the processor just sits there and is not doing anything, that's why this is not such a great solution. So if you notice hiccups or so the moment you press a button, this is where you have to look at first.

Its not perfect. Still presses twice sometimes

Try a little longer delay or search for "Arduino debounce".

You might want to ignore the button release... When you release the button it's probably going to bounce on & off again and another delay (and ignoring the "fake" button presses) after it's released should help.

Or in some cases, the initial button press is ignored, then there's a delay and action is taken then the button is released. (Mouse clicks work that way... Nothing happens 'till you release the button.)

At 130 ms delay it's quite surely not button bounce. It's crappy buttons, faulty wiring, or it's you not doing state detection of course (i.e. only react on state change of the button).