HELP: Code for potentiometer.

I,m still struggling on how to code the pot to control the midi note in my recording software.

my setup was to hook the potentiometer in 0v-analog in-5v
control almost 5 midi notes in one potentiometer when i turn it. one midi note on each turn
please help me. all the tutorials I've read is only to control the digital ins using potentiometer but i tried searching how to control midi notes using potentiometer but i found none. please help me for i am a beginner. thank you so much. you are highly appreciated.

I would take the analog read value (0-1023) and map it to the number of notes you want (0-4) - hint, try the map() function.

Then you could use either an array to convert map values to MIDI notes, or use a switch construct to select different code depending on the value.

unsigned char PadNote[6] = {52,52,53,54,55};

int PadCutOff[5] = {100,100,100,100,100};

int MaxPlayTime[5] = {90,20,20,20,20};

#define midichannel 0;

boolean VelocityFlag = true;

boolean activePad[5] = {0,0,0,0,0,};
int PinPlayTime[5] = {0,0,0,0,0,};

unsigned char status;

int pin = 0;
int hitavg = 0;
const int pot = A0;

int lastPotVal = 0;
int prevPotVal = 0;

void setup() {
// Set MIDI baud rate:
Serial.begin(57600);
}

void loop() {
int number = 0;
for(int i =0; i <5; i++)
Serial.println(lastPotVal);
}
}
}
void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status = MESSAGE + midichannel;
Serial.print(status);
Serial.print(PITCH);
Serial.print(VELOCITY);
}

Please use code tags for posting code.

Is there a question you'd like to ask as regards that code? I can see that it is only partially written.

One immediate comment though: MIDI baud rate isn't 57600, it's 31250. And you can't use Serial.println() at the same time as using Serial for MIDI.

General point.

Whilst using a potentiometer as an input to an analog pin on the Arduino is a great party trick, it makes no sense in implementing an actual design.

Rotary encoders are almost as cheap as potentiometers, as cheap as decent (moulded carbon track or wire wound) potentiometers which would be suitable as input devices as against "set and forget" volume controls, and have infinite resolution (which is to say, you can make them do as many turns as you need to get the requisite number of steps, in addition to using variable stepping speed algorithms). In addition, you have a push switch for alternate functions, so one implements a complete menu system (such as on current medical equipment - equivalent to but much faster than three menu control buttons). They have replaced potentiometers as volume controls in quality and even most low-end equipment, for example car radios and of course are automatically adapted for remote control.

You do have to use two (or three for the function button) inputs, but apart from that it just makes no sense to feed a potentiometer into an analog pin.

but apart from that it just makes no sense to feed a potentiometer into an analog pin.

Unless of course you want the automatic positional non volatile memory that a pot offers with no software overhead nor worry about EEPROM ware levelling. Or the haptic feedback you get at the end of the useful adjustment range. Or the ability to use multiple inputs without complex interrupt programming. Or having the worry about contact bounce missing small movements or sending the adujutment reading the wrong way. Or having the ability to put a scale against a pointing knob.

Yes it makes no sense at all. :stuck_out_tongue:

Grumpy_Mike:
Unless of course you want the automatic positional non volatile memory that a pot offers with no software overhead nor worry about EEPROM wear levelling.

Unless of course, it makes more sense to have a switch-on default which cannot be "bumped" whilst switched off. Of course, you do not re-write an EEPROM for every step of adjustment anyway.

Grumpy_Mike:
Or the haptic feedback you get at the end of the useful adjustment range.

Which is the really great advantage of encoders - you can't break the end-stops or twist the knob loose!

Grumpy_Mike:
Or the ability to use multiple inputs without complex interrupt programming.

A rotary encoder is a "HID" - Human Interface Device. It does not run fast enough for interrupts to be relevant. :smiley: (If it did run that fast, you would not be using a potentiometer anyway.)

Grumpy_Mike:
Or having the worry about contact bounce missing small movements or sending the adjustment reading the wrong way.

It is quadrature encoded. Even if you did not debounce it, it inherently reads accurate against contact bounce (i.e., it dithers on a single step - as does a potentiometer).

Grumpy_Mike:
Or having the ability to put a scale against a pointing knob.

Which would then require calibration - something that "going digital" makes so much easier, more accurate and more durable.

... topic...?

Paul__B
I think you have lost the plot, few of those points make any sense.

A rotary encoder is a "HID" - Human Interface Device.

No it is not, it could be made into one but as it stands it is not.

Of course, you do not re-write an EEPROM for every step of adjustment anyway.

No then you just need extra circuitry to detect the removal of power and write to EEPROM before the power dies completely.

Which is the really great advantage of encoders - you can't break the end-stops or twist the knob loose!

Come on be serious.

It is quadrature encoded. Even if you did not debounce it, it inherently reads accurate against contact bounce (i.e., it dithers on a single step - as does a potentiometer).

It is clear by this that you have never use one, or at least never used a low cost one. The non switching output can and frequently does bounce. This screws up any of the nice gray code sequences. Of course you can get round it with latches and debounce circuitry but this is complicating things when all you need is a simple pot.

Which would then require calibration - something that "going digital" makes so much easier, more accurate and more durable.

OK so that is what you think, I would say that that opinion is very idealistic and naive and that you pile on technology for its own sake. You need calibration EVERY TIME you apply the power, how convenient is that (not).

majenko:
... topic...?

I quite agree, but he started it by giving really inappropriate advice for a beginner, and I would venture to say for anyone.

wow. thanks for the replies. i just want to make a continuous Hi-Hat [HH] pedal from arduino drum. i've search a lot but i found none. i badly need the code how to control midi notes from my pot and when i hit the piezo, i will get an open HH, slightly open HH, close HH and pedal HH.
i've seen a lot of topic that grumpy mike helps a lot of beginners in aspect of making an arduino project. thank you gramps. :slight_smile:

For that kind of project there's a lot of nuances you could really do with getting to grips with.

Yes, it's simple enough to map a pot position to a midi note, but really you want to do a lot more than that.

For open, closed, and part-open it's simple enough - when the piezo is hit read the pot, map the value, and play the note. But you also want to be continuously reading the pot when you're not hitting the piezo, so that when the pot transitions from anything that's "not closed" to "closed" you play the closing hi-hat note. And, on top of that, you want to measure the velocity at which the pedal is being pressed (rate of change in pot value), as that would determine the volume at which to play the closing hi-hat note.

So there's quite a bit to think about there...

it's too hard for me. i think it's way beyond my simple knowledge. thanks

johngovan:
Ii just want to make a continuous Hi-Hat [HH] pedal from arduino drum. i've search a lot but i found none. i badly need the code how to control midi notes from my pot and when i hit the piezo, i will get an open HH, slightly open HH, close HH and pedal HH.

So do you have the MIDI not numbers for that?
When you get a trigger read the pot.
It is quite simple split the 0 to 1023 range from the pot reading into five. Use if statements to see if a reading is within that range to select what you want to send. Then send it.
Try that and if you get stuck post the code.