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

Thread: LED brightness controlled by MIDI CC value

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    1

    LED brightness controlled by MIDI CC value

    Hello, I've spent a few months on and off on this project and finally decided to post in the forums for some guidance. I'm building a MIDI controller using a Teensy 3.6 and some encoders, pots, LEDs, etc. Using tttapa's (Pieter's) Control Surface library I've been able to get a very simple setup working with ease (thanks to the documentation and examples provided). I've had success mapping and controlling parameters within Ableton Live.

    Here's where my tiny, tiny peabrain fails me: I'd like to be able to return CC values from Ableton and have the actual value of the parameter correspond to an LED (i.e. if the virtual knob (macro) is at CC value 0 then LED is 'off' or zero brightness, if CC value is 63 then LED is at 50% brightness, if CC value is 127 then LED is at full brightness). I would like to have one of these set up for every encoder so that I can get a quick idea of the position of each macro within Ableton.

    I have a maxforlive patch that sends CC values which can be mapped to macros, so I have a way to send the information back out to the Teensy (the macro itself will dictate the CC values, not the encoder). I'm unsure how to turn this information into illuminating the LED.

    Here's my code:

    Code:
    #include <Encoder.h>
    #include <Control_Surface.h>
    
    USBMIDI_Interface midi; //designate that this is a USBMIDI interface and the name
    
    int LED1 = 35;
    int brightness = 0;
    
    CCRotaryEncoder enc = { {32, 31}, MIDI_CC::Channel_Volume, 2, 4,};
        // create rotary encoder object called enc1 on pins 31, 32
        //sends midi to Channel_Volume CC (byte 0x10) on midi channel 1
        //the step multiplier is 2, encoder sends out 4 pulses per step
    
    void LEDCC(byte channel, byte note, byte velocity) {
      int vel1 = velocity;
      vel1 = map(vel1, 0, 127, 0, 255);
      brightness = vel1;
      analogWrite(LED1, brightness);
    }
    
    void setup() {
      // put your setup code here, to run once:  
        Control_Surface.begin();
        pinMode(LED1, OUTPUT);
        usbMIDI.setHandleControlChange(LEDCC);
    }
      
    void loop() {
      // put your main code here, to run repeatedly:
    
        Control_Surface.loop();
        
    }
    If this is actually something that's dead easy please point me to some examples so I can pick them apart and try learn this for myself. Thank you

  2. #2
    Junior Member
    Join Date
    Aug 2017
    Posts
    13
    Hey,
    since nobody replied thus far, I'm going to take a stab at it. I can't test your code right myself right now but on paper this looks fine, no? (I'll defer to anyone more knowledgeable) You didn't mention, is it not working at all right now? Can you test if your LEDCC callback gets called at all (maybe just by toggling a pin for starters)?
    Quick suggestion on the code that won't help you with the actual issue however: if you're only using PWM for these LEDs and non-pitch bend CC or if overall you can live with a 7 bit (values 0 to 127) PWM resolution, I'd do the following :

    Code:
    void LEDCC(byte channel, byte note, byte velocity) {
    /* No need to do this with a 7-bit PWM resolution 
      int vel1 = velocity;
      vel1 = map(vel1, 0, 127, 0, 255);
    */ 
    
      brightness = velocity;
      analogWrite(LED1, brightness);
    // or just analogWrite(LED1,velocity); if you don't need the global var "brightness" elsewhere
    
    }
    
    void setup() {
      // put your setup code here, to run once:  
        Control_Surface.begin();
      // According to arduino's analogwrite doc, setting pinMode isn't actually needed for analogwrite, maybe it is for Teensy though?
      //  pinMode(LED1, OUTPUT);   
    
    // This sets the PWM range to be between 0 and 127
    analogWriteResolution(7);
    
        usbMIDI.setHandleControlChange(LEDCC);
    }
    Since I am also playing around with MIDI controllers and mostly using Ableton , I'd be curious about the MaxForLive patch if you don't mind sharing. Also I don't quite understand what you need it for yet, it was my understanding that Ableton sends out CC messages anyway to the appropriately configured MIDI output devices whenever a change happens?

    Hope this helps at least a little, best of luck,
    Jazz

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,148
    @icecold: I can't get your code to compile. I haven't got Control_Surface.h so I just commented that out.
    Although there is an Encoder.h in Teensyduino, the compiler doesn't know what "CCRotaryEncoder" is, so I commented that as well.
    I'm compiling this with USB type "MIDI" but the compiler doesn't know what "USBMIDI_Interface" is.
    Does that code compile for you?

    Pete

  4. #4
    Senior Member crees's Avatar
    Join Date
    Dec 2016
    Location
    Utah
    Posts
    165
    Also Keep in mind it all depends on how your LED is wired. A 0 Value may be full on where a 255 Value will be off or Opposite. Can you elaborate on how the LED's annode and cathode is wired to your teensy pins? Also a schematic of the LED, resistor and Teensy?

    Depending on how its wired will dictate the values you need to compute before sending them to analogwrite.

Posting Permissions

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