Welcome to the MIDI Sprout forum

Author Topic: Is there a way to dim the LEDs? Adding Code?  (Read 1296 times)

Offline skoobert

  • Newbie
  • *
  • Posts: 6
    • View Profile
Is there a way to dim the LEDs? Adding Code?
« on: November 19, 2016, 05:02:36 PM »
Hello Sam,

I've got a MIDI Sprout kit that I've successfully moved from the breadboard to an Adafruit protoboard.  It's all in an enclosure now too (similar to the Sprout in the Kickstarter video).   I'm wondering if I can add some code to the chip to control the brightness of the LEDs.  I've got a good friend who is an Arduino / coding pro and she mentioned that adding some lines of code might work to control the overall brightness. What do you think?

Cheers,
Skooby

Offline sam

  • Sam
  • Moderator
  • Full Member
  • *****
  • Posts: 176
  • Understand How Your Tools Work!
    • View Profile
    • Electricity for Progress
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #1 on: November 20, 2016, 01:17:14 PM »
Hello Skooby, congratulations on building your Sprout, I am very glad you have it running on soldered protoboard!

I am using the excellent LEDFader library from jgillick, and we will want to provide a scaled (mapped) value when we set each LED.  There are three ways to adjust the brightness of the LEDs:
  • Use a Diffuser:  On the production MIDI Sprout, we use rice paper to diffuse the LEDs.  By taking sandpaper to the LED caps, you can rough up the crystal clear LEDs and create a softer diffusion.  Sorry, they are super bright leds!
  • Adjust the Resistor values:  It is possible to replace the current limiting resistors on each LED to be a higher value causing the LEDs to be more dim.  Each LED colour would have a different brightness and it would take some experimentation to find the best values.
  • Change the Brightness in Code:  Yep, you were right, the best way to do this adjust the maximum brightness in code!  I could've/should've placed a global variable to adjust the brightness level, but instead we will need to dig through the code a bit to find the right spot.  Here are two ways to do it... the LEDs are set during the setNote() function.  We can either change the setNote() send a new max value, or we can edit the rampUp() function to scale the value which it is being provided. 
      I recommend changing the rampUp() function (under Peripherals in the .ino) seen below:
Code: [Select]
void rampUp(int ledPin, int value, int time) {
LEDFader *led = &leds[ledPin];
//scale the value parameter against a new maxBrightness global variable
//  led->fade(value, time); 
led->fade(map(value,0,255,0,maxBrightness), time);
}
    Then add the new variable near the top of the program with the other global variable declarations
Code: [Select]
int maxBrightness = 155;
The maximum value is 255 and I've found the drop off of brightness to have a knee around 155.  But i would recommend a value of 25-55 (super low) if you want to have a calm light show in the dark.

When you are trying to reprogram your 328p chip, you may encounter an issue where there is no 'boot loader'  if you have an Arduino guru friend they will know what to do.  I recommend programming the sprout using the onboard ICSP  on the circuit board using a hardware programmer or an Arduino-as-ISP.

Please post about your experiences, and let us know how your project turns out!  Let me know if you have any other questions.

Offline skoobert

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #2 on: January 05, 2017, 11:33:34 AM »
Hello again @sam,

I'm meeting with my coding / Arduino buddy soon to add some code to the original MIDI Sprout chip to change the LED brightness and musical scale.  I was thinking that it might be cool to have a separate chip for each musical scale that I wanted to use in a performance instead of reprogramming it every time.  I've got a modular synth rig, so I would need to have the scale programmed on the chip (MIDI Sprout Tools won't help :) ).  I wanted to ask you if it was possible to flash the entire MIDI Sprout code to a brand new ATMEGA328-PU chip and then change them out when I need to?

Cheers,
Skooby

Offline sam

  • Sam
  • Moderator
  • Full Member
  • *****
  • Posts: 176
  • Understand How Your Tools Work!
    • View Profile
    • Electricity for Progress
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #3 on: January 05, 2017, 03:46:37 PM »
I am very glad that you are making progress Skooby, good luck reprogramming the chip!  Yeah you can flash a bunch of chips, with changes to the scale or code, and that should work great!  But... there is a better way!

I am working on a new 'Arduino Shield' kit which will include .... a button!   Now why would i be all excited about one silly button... In order to provide users access to set additional parameters, i'm using a button (connected to A1) in conjunction with the knob/potentiometer.  I'm working on a new series of MODES where the user can click the button, and the LEDs will flash to display the current Mode.  Modes will be: Scale, Threshold, MIDI channel, MIDI CC, Max Note, Min Note, LED brightness... etc.  It will be a couple weeks, but i'll be posting an update on the furm.

So with that, I suggest that you also try to add a tact switch/button to A1 and then pop in some code which changes the currently selected Scale with either the press of a button or include the knob for a value.  It should be pretty easy to toggle between Threshold and Scale using the button and turn the knob to make your selection. 

I'll share my code (of course) once i get the menu modes written up.

Offline skoobert

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #4 on: January 09, 2017, 01:34:16 PM »
Hello @sam,

Can you tell me what part of the code to change in order to limit the note range?  I'd like to play around with limiting the range to 2 octaves and then maybe even one octave.  Thanks, man!

Offline sam

  • Sam
  • Moderator
  • Full Member
  • *****
  • Posts: 176
  • Understand How Your Tools Work!
    • View Profile
    • Electricity for Progress
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #5 on: January 09, 2017, 02:08:38 PM »
I tried to put all of the main variable declarations at the top of the main file/tab in the .ino lines 34 and 35

int noteMin = 36; //C2  - keyboard note minimum
int noteMax = 96; //C7  - keyboard note maximum

One thing to consider when constraining note ranges, the Sprout is able to detect changes in conductivity at a very 'fine' detail level.  In order to preserve this detail, i have implemented a roll-over process:  As conductivity increases, notes will step downward, once the 'lowest' note is reached the program loops back around to the 'hightest' note.  If conductivity continues to increase, the note will move down until again it loops back.

I have been asked many times, why i would implement in this way.  Well since the variation in conductivity between plants, surfaces, fruit, and users can be quite large, the Sprout needs to react dynamically.  If i constrained the notes to, for example, a single octave and did not roll-over you would end up getting just a single note over and over again for a given plant.

I'm considering adding a mode where pitches are microtonal, by sending a base note and a bend range.

In a couple weeks i'll release a new version of the firmware which, if you add a simple button to A1, the sprout will have a menu to choose brightness, scale, MIDI channel, etc.

Offline skoobert

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #6 on: January 09, 2017, 03:22:20 PM »
@sam Thanks, man!  I must be blind! I see the code location now :)

Offline skoobert

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #7 on: January 11, 2017, 11:46:22 AM »
Hello @sam,

I've got some chips programmed with new musical scales and I'm working on the LED brightness now.  I'm getting a "scope" error when I verify the sketch.  I think I have the maxBrightness variable in the wrong spot.  Where should it go exactly?  Thanks again!

Offline sam

  • Sam
  • Moderator
  • Full Member
  • *****
  • Posts: 176
  • Understand How Your Tools Work!
    • View Profile
    • Electricity for Progress
Re: Is there a way to dim the LEDs? Adding Code?
« Reply #8 on: January 11, 2017, 04:15:34 PM »
Put it right under the noteMin and Max, and that should declare it as global

int maxBrightness = 255;

Anywhere in the first 80 lines should work.