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

Thread: Data access light on a teensy lc?

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Location
    Denver, CO
    Posts
    3

    Data access light on a teensy lc?

    Hey, noobie here.
    Is there a way to attach a 3v LED to a teensy lc board and have it flicker corresponding to the data passing through? Iíve made a MIDI controller and it would be cool to add an led that flickers any time a button is pushed and/or a pot is turned. If this is possible, what code would I need? Thx in advance

    Here it is:

    https://www.facebook.com/midwestjess...16177328725172

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,915
    Lots of ways to handle it. Depending on what hardware you are already using easy choice may be pin 13 which already has an LED

    In terms of what you drive it from one place may to to turn it on when your Midi stream function is called and turn it off when it ends. If you want more information out of the LED, or drive it from button activity then you could do something like use
    https://www.pjrc.com/teensy/td_timin...pedMillis.html
    and within each UI element zeroise a timer and within the main loop test if the timer is < 500, if so have LED on.

    Key idea is to ensure that you do not have code that does a delay(500); to keep the LED on, you need to let the rest of the processing run and keep checking back to see if the LED needs to go off. Since it is just a UI element the exact timing does not matter so no need to dig into interrupts or similar low latency things.

    If you want a sequence of blinks or similar than life gets more complex since you need to handle what happens if another element activates while the last one is still in the display cycle, though you can do things like use https://www.arduino.cc/reference/en/...rators/modulo/ to test your timer value/100%2 ==0 to get an on off flicker. at 5hz.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Location
    Denver, CO
    Posts
    3
    Hey So Im not getting it. would you be able to add the first thing you had in mind to what I have?

    here is what I have....

    Code:
    /* Simple Teensy DIY USB-MIDI controller.
      Created by Liam Lacey, based on the Teensy USB-MIDI Buttons example code.
    
       Contains 8 push buttons for sending MIDI messages,
       and a toggle switch for setting whether the buttons
       send note messages or CC messages.
    
       The toggle switch is connected to input pin 0,
       and the push buttons are connected to input pins 1 - 8.
    
       You must select MIDI from the "Tools > USB Type" menu for this code to compile.
    
       To change the name of the USB-MIDI device, edit the STR_PRODUCT define
       in the /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/cores/usb_midi/usb_private.h
       file. You may need to clear your computers cache of MIDI devices for the name change to be applied.
    
       See https://www.pjrc.com/teensy/td_midi.html for the Teensy MIDI library documentation.
    
    */
    
    
    #include <Bounce.h>
    
    //The number of push buttons
    const int NUM_OF_BUTTONS = 12;
    
    // the MIDI channel number to send messages
    const int MIDI_CHAN = 1;
    
    int led = 13;
    // Create Bounce objects for each button and switch. The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.
    // 5 = 5 ms debounce time which is appropriate for good quality mechanical push buttons.
    // If a button is too "sensitive" to rapid touch, you can increase this time.
    
    //button debounce time
    const int DEBOUNCE_TIME = 50;
    
    Bounce buttons[NUM_OF_BUTTONS + 1] =
    {
      Bounce (0, DEBOUNCE_TIME),
      Bounce (1, DEBOUNCE_TIME),
      Bounce (2, DEBOUNCE_TIME),
      Bounce (3, DEBOUNCE_TIME),
      Bounce (4, DEBOUNCE_TIME),
      Bounce (5, DEBOUNCE_TIME),
      Bounce (6, DEBOUNCE_TIME),
      Bounce (7, DEBOUNCE_TIME),
      Bounce (8, DEBOUNCE_TIME),
      Bounce (9, DEBOUNCE_TIME),
      Bounce (10, DEBOUNCE_TIME),
      Bounce (11, DEBOUNCE_TIME),
      Bounce (12, DEBOUNCE_TIME),
    };
    
    const int MIDI_MODE_NOTES = 0;
    const int MIDI_MODE_CCS = 1;
    
    //Variable that stores the current MIDI mode of the device (what type of messages the push buttons send).
    int midiMode = MIDI_MODE_NOTES;
    
    //Arrays the store the exact note and CC messages each push button will send.
    const int MIDI_NOTE_NUMS[NUM_OF_BUTTONS] = {72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83};
    const int MIDI_NOTE_VELS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127};
    const int MIDI_CC_NUMS[NUM_OF_BUTTONS] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35};
    const int MIDI_CC_VALS[NUM_OF_BUTTONS] = {127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127};
    
    //==============================================================================
    //==============================================================================
    //==============================================================================
    //The setup function. Called once when the Teensy is turned on or restarted
    
    void setup()
    {
      // Configure the pins for input mode with pullup resistors.
      // The buttons/switch connect from each pin to ground.  When
      // the button is pressed/on, the pin reads LOW because the button
      // shorts it to ground.  When released/off, the pin reads HIGH
      // because the pullup resistor connects to +5 volts inside
      // the chip.  LOW for "on", and HIGH for "off" may seem
      // backwards, but using the on-chip pullup resistors is very
      // convenient.  The scheme is called "active low", and it's
      // very commonly used in electronics... so much that the chip
      // has built-in pullup resistors!
    
      for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
      {
        pinMode (i, INPUT_PULLUP);
        pinMode(led, OUTPUT);  
      }
    
    }
    
    //==============================================================================
    //==============================================================================
    //==============================================================================
    //The loop function. Called over-and-over once the setup function has been called.
    
    void loop()
    
    {
    
    
      //==============================================================================
      // Update all the buttons/switch. There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
      for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
      {
        buttons[i].update();
      }
    
      //==============================================================================
      // Check the status of each push button
    
      for (int i = 0; i < NUM_OF_BUTTONS; i++)
      {
    
        //========================================
        // Check each button for "falling" edge.
        // Falling = high (not pressed - voltage from pullup resistor) to low (pressed - button connects pin to ground)
    
        if (buttons[i + 1].fallingEdge())
        {
          //If in note mode send a MIDI note-on message.
          //Else send a CC message.
          if (midiMode == MIDI_MODE_NOTES)
            usbMIDI.sendNoteOn (MIDI_NOTE_NUMS[i], MIDI_NOTE_VELS[i], MIDI_CHAN);
          else
            usbMIDI.sendControlChange (MIDI_CC_NUMS[i], MIDI_CC_VALS[i], MIDI_CHAN);
        }
    
        //========================================
        // Check each button for "rising" edge
        // Rising = low (pressed - button connects pin to ground) to high (not pressed - voltage from pullup resistor)
    
        else if (buttons[i + 1].risingEdge())
        {
          //If in note mode send a MIDI note-off message.
          //Else send a CC message with a value of 0.
          if (midiMode == MIDI_MODE_NOTES)
            usbMIDI.sendNoteOff (MIDI_NOTE_NUMS[i], 0, MIDI_CHAN);
          else
            usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, MIDI_CHAN);
        }
    
      } //for (int i = 0; i < NUM_OF_BUTTONS; i++)
    
      //==============================================================================
      // Check the status of the toggle switch, and set the MIDI mode based on this.
      if (buttons[0].fallingEdge())
      {
        midiMode = MIDI_MODE_NOTES;
      }
      else if (buttons[0].risingEdge())
      {
        midiMode = MIDI_MODE_CCS;
      }
    
      //==============================================================================
      // MIDI Controllers should discard incoming MIDI messages.
      // http://forum.pjrc.com/threads/24179-...ses-midi-crash
      while (usbMIDI.read())
      {
        // ignoring incoming messages, so don't do anything here.
      }
      
    }
    Last edited by Theremingenieur; 04-22-2018 at 08:57 AM. Reason: Added the required CODE tags for better readability

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,915
    Looking at your code easy option does not work since the midi send is hidden off in the midi library, not being called by the main loop and no easy way to see if a send is in progress.

    So looks like option B,
    declare up the top of your code
    Code:
    elapsedMillis sinceMessageSent;
    in loop()
    Code:
    if (sinceMessageSent<500) digitalWrite(led,HIGH); else digitalWrite(led,LOW);
    and inside each UI call that sends a message
    Code:
    sinceMessageSent=0;

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,211
    Code:
    Bounce buttons[NUM_OF_BUTTONS + 1] =
    {
      Bounce (0, DEBOUNCE_TIME),
      Bounce (1, DEBOUNCE_TIME),
      Bounce (2, DEBOUNCE_TIME),
      Bounce (3, DEBOUNCE_TIME),
      Bounce (4, DEBOUNCE_TIME),
      Bounce (5, DEBOUNCE_TIME),
      Bounce (6, DEBOUNCE_TIME),
      Bounce (7, DEBOUNCE_TIME),
      Bounce (8, DEBOUNCE_TIME),
      Bounce (9, DEBOUNCE_TIME),
      Bounce (10, DEBOUNCE_TIME),
      Bounce (11, DEBOUNCE_TIME),
      Bounce (12, DEBOUNCE_TIME),
    };
    why +1? your "for loops" are effectively being told to handle pin 13, which is the led...
    You should only specify +1 for the character arrays which contain a null, but for loops in this case, your doing pins 0->13 inclusive, NOT 0-12...

    EXAMPLE:

    Code:
      for (int i = 0; i < NUM_OF_BUTTONS + 1; i++) // this is even worse as your explicitly including pin 12 !
      {
        buttons[i].update();
      }
    
      //==============================================================================
      // Check the status of each push button
    
      for (int i = 0; i < NUM_OF_BUTTONS; i++) // your calling pins here 0 -> 11, good 
      {
    This: i < 12 + 1 is essentially pins 0->12
    Code:
      for (int i = 0; i < NUM_OF_BUTTONS + 1; i++)
      {
        pinMode (i, INPUT_PULLUP);
        pinMode(led, OUTPUT);  
      }
    Also, you realize in several parts your handling pin 12 but not actually using it? 12 buttons from 0 is 0->11, so 13 (led) is not set as input and your randomly using pin 12 throughout your code...
    Last edited by tonton81; 04-22-2018 at 02:42 PM.

  6. #6
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,297
    The plus 1 is to debounce the toggle (unnecessarily IMHO) but it looks like it is on pin 0 while the momentary button loop indexes from zero too.

  7. #7
    Junior Member
    Join Date
    Apr 2018
    Location
    Denver, CO
    Posts
    3
    Quote Originally Posted by GremlinWrangler View Post
    Looking at your code easy option does not work since the midi send is hidden off in the midi library, not being called by the main loop and no easy way to see if a send is in progress.

    So looks like option B,
    declare up the top of your code
    Code:
    elapsedMillis sinceMessageSent;
    in loop()
    Code:
    if (sinceMessageSent<500) digitalWrite(led,HIGH); else digitalWrite(led,LOW);
    and inside each UI call that sends a message
    Code:
    sinceMessageSent=0;
    That worked! Thank you so much. I've applied a 20ms flash to a note on message as well as a note off message.

Posting Permissions

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