Go Down

Topic: Mega MIDI Keyboard jittery noise (interference) (Read 1 time) previous topic - next topic

Sam423

Nov 21, 2019, 06:10 pm Last Edit: Nov 22, 2019, 01:43 pm by Sam423
Hi, I'm building a custom midi keyboard with an Arduino and I'm hoping to get some help with some kind of noise or interference problem I'm having.

The keyboard has 28 keys, so I'm using an Arduino Mega and a sensor shield. The keys are just push buttons I've connected to ground and input pin via dupont connectors (I'm using the built in pullup resistors). The only other connection is to a 5pin midi connector. This then goes to usb via this midi to usb cable.

It works fine when I set it up to work with just one button. But when I have all 28 buttons set up in code, the notes come out jittery, like I'm getting some kind of interference. Interestingly, if I remove the line of code that turns notes off, then the jittering goes away (but the instrument doesn't work as intended anymore).

One possibility was that the numerous buttons are somehow interfering (maybe the internal pullup resistors are too weak). However, when I check the serial plotter (set to 9600 baud rate), I don't see any noise. I would try the midi baud rate of 31250 but that's not an option in the plotter. I did try 38400 and that also showed no noise.

Does anyone have any idea what might be causing this jittery sound (perhaps interference)? And any suggestions on how to further trouble shoot it?

Thanks,Sam

Code:
Code: [Select]
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int ledPin =  13;
int i;
int Pins[28] = { 2, 3, 4, 5, 6, 7, 14, 15, 16, 17, 18, 19, 20, 21, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45 };
int Notes[28] = { 62, 74, 61, 73, 60, 72, 75, 63, 76, 64, 77, 65, 78, 76, 72, 60, 71, 59, 70, 58, 69, 57, 68, 56, 67, 55, 66, 54 };

void setup() {
 MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to all incoming messages
 //Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
 for (i = 0; i < 28; i++){
   pinMode(Pins[i], INPUT_PULLUP);  
 }
}

void loop() {        
   for (i=0; i < 28; i++){      
     if (digitalRead(Pins[i]) == LOW) {
       //Serial.print(1);        
       MIDI.sendNoteOn(Notes[i], 127, 1);    // Send a Note (pitch 42, velo 127 on channel 1)  
     }
     else{
       //Serial.print(0);        
       MIDI.sendNoteOn(Notes[i], 0, 1);    // Send a Note (pitch 42, velo 127 on channel 1)  
     }          
   }
   //Serial.println("");
}

slipstick

#1
Nov 21, 2019, 08:36 pm Last Edit: Nov 21, 2019, 09:30 pm by slipstick
Don't you mean Pins[ i ] and Notes[ i ] in your read and MIDI send?

Steve

PieterP

#2
Nov 21, 2019, 08:56 pm Last Edit: Nov 21, 2019, 09:03 pm by PieterP
You need state change detection.
You can find it in the examples that come with the IDE, or you could use the Control Surface library that handles this for you:
example

31250 is the only correct baud rate if you want to use a MIDI to USB adapter.

Pieter

Grumpy_Mike

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code. Not only that you are having your code mangled by the forum software and turning it in to italics.

What I notice about your code is that you seem to be using arrays and not using any index, just the array name.

Also you seem to be turning a note on or turning a note on, so that is not going to work.
You need to send the note on only when your switch becomes on for for the first time. Likewise when your switch is off for the first time then send a MIDI note off message.

Look at the IDE at the "state change" example to see how to detect what a switch changes, do not simply look at the switch state. Remember you loop will run perhaps 1000 time a second, which with the code you have going to cause all sorts of trouble flooding the MIDI sound generator all sorts of problems.

Sam423

#4
Nov 22, 2019, 01:56 pm Last Edit: Nov 22, 2019, 02:58 pm by Sam423
slipstick, my code was being broken by the forum software (as Grumpy_Mike rightly pointed out). Now that I've labelled it as code, it does show correctly with the index in square brackets. Good eye.

PieterP, thanks for pointing this out, I'm going to give this a try!

Grumpy_Mike, besides not using code tags, was there some other rule I was breaking? I searched through your forums rules link and couldn't find anything else, my sincere apologies if I'm missing something.

I'll look into the state change example and control surface library this morning. Thank you all for your help!

Sam423

Control Surface is working great for me, thanks guys!

If you don't mind, I have a very quick related question: I had been using "sendControlChange" in the Arduino Midi Library, and I can't seem to find the equivalent in the Control Surface library. Do any of you happen to know if it exists?

Thank you all again kindly for your help!

PieterP

#6
Nov 22, 2019, 05:04 pm Last Edit: Nov 22, 2019, 05:05 pm by PieterP
Control Surface is working great for me, thanks guys!

If you don't mind, I have a very quick related question: I had been using "sendControlChange" in the Arduino Midi Library, and I can't seem to find the equivalent in the Control Surface library. Do any of you happen to know if it exists?
You can use midi.sendCC({controller_number, channel, cable_number}, value).
The channel and cable number are optional. If you only specify the controller number, you don't need braces.

For example:
Code: [Select]
USBMIDI_Interface midi;

[...]

midi.sendCC({0x01, CHANNEL_3, 10}, 0x7F); // Controller #1, Channel 3, Cable 10, Value 0x7F
midi.sendCC({0x01, CHANNEL_3}, 0x7F);     // Cable 0 (first and default cable)
midi.sendCC(0x01, 0x7F);                  // Channel 1, Cable 0

Documentation



Go Up