Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: usbMIDI read of analog values uncooperative while multimeter read voltages OK

  1. #1

    usbMIDI read of analog values uncooperative while multimeter read voltages OK

    I am simply trying to read the value of a linear potentiometer with the Teensy 3.1, and convert this to MIDI.

    Click image for larger version. 

Name:	photo.jpg 
Views:	153 
Size:	177.9 KB 
ID:	1576

    here is the code
    _____________________
    int a0 =0;
    void setup(){
    pinMode(15, INPUT);
    }

    void loop(){
    a0 = analogRead(15);
    usbMIDI.sendControlChange(1, a0, 1);
    Serial.println(a0);
    }

    ______________________

    when I read the voltage of pin 15 - it linearly increases as I slide the pot. as its supposed to.
    when I look at the values on the arduino serial monitor - it linearly increases (from 0 -1023) as I slide the pot. as its supposed to.

    but the actual MIDI data that is sent is very hectic. It seems as though the slider is scrolling through random numbers 0-127...(except when all the way down it stays at 0, and all the way up, at 127) it isnt linear and is constantly jumping around. the only trend is that i tend to see values>100 mostly when the slider is in the middle of its track



    any ideas??

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,073
    I see in another thread you discovered the need to divide the number by 8, so the 0 to 1023 range from analogRead gets mapped into the 0 to 127 range for MIDI.

    In case anyone else with the same problem finds this thread, that's the solution.

    There are also some helpful examples under File > Examples > Teensy > USB_MIDI

  3. #3
    Senior Member
    Join Date
    Feb 2013
    Posts
    179
    Most likely it's because MIDI only allows values from 0-127, and your analog value is in the range of 0-1023. It won't just automatically downscale it for you.

    Instead of the line

    a0 = analogRead(15);

    try

    a0 = (analogRead(15) >> 3);

    The >> is the binary 'right shift' operator. Basically, if 1023 is (in binary) 1111111111 (ten ones), the right shift operator moves everything to the right by 3, so it becomes 0001111111.

    For a better example: 765 in 10 bits is 1011111101 -- you can't represent this in a 7-bit number like MIDI expects. If you lop off the last 3 numbers, so you've got 1011111, you get the decimal number 95. 765/1023 = 0.74780059, while 95/127 = 0.7480315. You lose some accuracy (3 bits, to be precise), but the RATIO is basically the same.

    EDIT: Whoops, guess I was writing this at the same time Paul was writing his (much quicker) reply. I still maintain that bitshifting is better than dividing, though...

  4. #4
    yepyep thanks guys - I'm thinking if I had looked close enough maybe the first 127 numbers of the stream of 0-1023 would have been linear in the MIDI stream, and then crazy after that.

    Mushoo - what makes you say that bitshifting is better? In this case aren't they exactly the same? When you take off three bits (8) it is as though you are dividing by 8 - yes?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •