MIDI CONTROLLER

Hi everyone ! I'm sorry for my bad english.
I'm having some troubles with the midi.h library, i hope i'm in the right place. I,m trying to build a midi controller for ableton but i can't get the signals i want.
I used send cc function of the midi.h library and i'm moniting the midi signal thanks to midi monitor.
The problem is that when i send the signal trough a bottom i get sometimes the signal i want but more often i get random pitchnotes signals , i have no clue where they come from( they are sometimes even in a different channel).
Any suggestion?
I will post the code if u want
Have a nice day !

We can't do much without seeing the code. Please post it using </> code tags as described in "How to use this forum- please read" at the top of the forum page.

In my experience MIDI.h has never done anything except what my code told it to do.

Steve

Thanks for your answer
Here is my code </>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
#define midiChannel (char)1
MIDI_CREATE_DEFAULT_INSTANCE();

const int buttonPin1 = 6; // the pin that the pushbutton is attached to
const int buttonPin2 = 7;

int button1State = 0; // current state of the button
int lastButton1State = 0; // previous state of the button
int button2State = 0;
int lastButton2State = 0;

void setup() {
Serial.begin(115200);
// initialize the button pin as an input:
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);

// begin MIDI communication:
MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
// read the pushbutton input pin:
button1State = digitalRead(buttonPin1);
button2State = digitalRead(buttonPin2);

// compare the buttonState to its previous state:
// primo bottone
if (button1State != lastButton1State) {
if (button1State == LOW) {
delay(300);
if (button1State == LOW) {
MIDI.sendControlChange(105, 127, 1);
delay(150);

lastButton1State = button1State;
}
else if (button1State != lastButton1State) {
if (button1State == HIGH){
delay(200);
if (button1State == HIGH){
MIDI.sendControlChange(105, 0, 1);
delay(300);
lastButton1State = button1State;
}
}
}

}
}

//secondo bottone

if (button2State != lastButton2State) {
if (button2State == LOW) {
MIDI.sendControlChange(106, 127, 1);
delay(200);
lastButton1State = button1State;
}
else if (button2State != lastButton2State) {
if (button2State == HIGH);
MIDI.sendControlChange(106, 0, 1);
delay(200);
lastButton2State = button2State;
}
}
}

What very odd code

if (button1State != lastButton1State) {
    if (button1State == LOW) {
      delay(300);
      if (button1State == LOW) {
      MIDI.sendControlChange(105, 127, 1);
      delay(150);

Could be rewritten

if (button1State != lastButton1State && button1state == LOW) {
      delay(300);
      MIDI.sendControlChange(105, 127, 1);
      delay(150);

Which in turn is an odd thing to want to do. You normally want as low a latency as possible not a long 300mS delay before anything happens.

Also that else clause will never be run because it is the same condition as the first part of the if.

Please read the how to use this forum sticky post on how to post code correctly.

Thank you i will try it later !

MIDI.begin overrides the baud rate set by Serial.begin. The proper way to set the baud rate is:

MIDI library baud rate setting:

---



```
#include <MIDI.h>

struct MySettings : public midi::DefaultSettings {
  static const long BaudRate = 115200;
};

MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings)

void setupcolor=#000000[/color] {
  MIDI.begincolor=#000000[/color];
}

void loopcolor=#000000[/color] {
  // put your main code here, to run repeatedly:
}
```

|

And like Grumpy_Mike said, your logic is very strange.

If you want to do it the easy way, take a look at my MIDI Controller library.
The following code replaces your entire sketch:

MIDI Controller code:

---



```
#include <MIDI_Controller.h> // Include the library

HairlessMIDI_Interface midi;

DigitalCC buttons[] = {
 {6, 105, 1}, // pin 6, controller 105, channel 1
 {7, 106, 1}
}

void setupcolor=#000000[/color] {}

void loopcolor=#000000[/color] {
 // Refresh the buttons (check whether a button's state has changed since last time, if so, send it over MIDI)
 MIDI_Controller.refreshcolor=#000000[/color];
}
```

|

Pieter