Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / HSV to RGB conversion in Processing on: March 17, 2011, 08:32:27 am
I have a project where I'm triggering rgb led's to the beat of music. Initially I was doing it all on the arduino, with limited success, but due to time constraints I am now using the minim library in processing to do the beat detection. I have the beat detection working well now but I can't seem to get the hsv to rgb code working in processing. It seems trivial but I haven't done this sort of programming for some time. Almost thinking of just using the arduino to do hsv to rgb as it was working on there. 

Here is the code below, the hsv to rgb part is based on https://processing-js.lighthouseapp.com/projects/41284/tickets/788-hsv-to-rgb-function
Any help would be greatly appreciated.

Code:
import processing.serial.*;
import cc.arduino.*;
import ddf.minim.*;
import ddf.minim.analysis.*;

Arduino arduino;
int ledPinRed = 6;
int ledPinBlue = 7;
int ledPinGreen = 8;
int delayVal = 50;
int blnFade = 0;
int h_int;

int R=0, G=0, B=0;
int var_i;
float S=1, V, lightness, var_1, var_2, var_3, var_h, var_r, var_g, var_b;
float H;
float L =0.5;
float val = 0;


Minim minim;
AudioInput in;
BeatDetect beat;
 
float eRadius;

void setup()
{
//    println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[4], 57600);
  arduino.pinMode(ledPinBlue, Arduino.OUTPUT);
  minim = new Minim(this);
  // get a line in from Minim, default bit depth is 16
  in = minim.getLineIn(Minim.STEREO, 512);
  // a beat detection object song SOUND_ENERGY mode with a sensitivity of 10 milliseconds
  beat = new BeatDetect();
}

void draw()
{
    beat.detect(in.mix);

  if ( beat.isOnset() ) {
    int val = Math.round(random(360));
    hsvl2rgb(val, L, R, G, B);
 
    arduino.analogWrite(ledPinRed, R);
    arduino.analogWrite(ledPinBlue, G);
    arduino.analogWrite(ledPinGreen, B);
    delay(50);
  }
  else {
    //arduino.digitalWrite(ledPinBlue, Arduino.HIGH);
  }
}

void hsvl2rgb(float H, float L, int R, int G, int B)
{
  int var_i;
  float S=1, V, lightness, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  V = L * 2;                         // For the "darkness" of the LED
  if ( V > 1 ) V = 1;

  if ( S == 0 )                      //HSV values = 0 ÷ 1
  {
    R = Math.round(V * 255);
    G = Math.round(V * 255);
    B = Math.round(V * 255);
  }
  else
  {
    var_h = H * 6;
    if ( var_h == 6 ) var_h = 0;  //H must be < 1
    var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    if ( L > 0.5 )         // Adjusting the Lightness (whiteness)
    {
      lightness = ( L - 0.5 ) / 0.5;
      var_r += ( lightness * ( 1 - var_r ) );
      var_g += ( lightness * ( 1 - var_g ) );
      var_b += ( lightness * ( 1 - var_b ) );
    }

    R = Math.round((1-var_r) * 255);     // RGB results = 0 ÷ 255. Reversed for common anode RGB LED's
    G = Math.round((1-var_g) * 255);
    B = Math.round((1-var_b) * 255);
  }
}
2  Forum 2005-2010 (read only) / Interfacing / Re: Connecting an array of Common Anode 3W RGB LEDs on: July 08, 2010, 04:12:23 am
They are meant to be the AMC7135. Reading the data sheet it seems they the AMC7135 needs the 5V input and a capacitor on the led cathode. The led would then be pwm through the 5V input to the AMC7135 as shown below for one of the cathodes.

Would this work? Would I need the 1uF input capacitance used in the data sheet between 5V and ground?

Cheers for your help  smiley
3  Forum 2005-2010 (read only) / Interfacing / Re: Connecting an array of Common Anode 3W RGB LEDs on: July 08, 2010, 01:47:41 am
Here is a paint sketch ( smiley-grin )of my circuit idea to drive them.  

4  Forum 2005-2010 (read only) / Interfacing / Connecting an array of Common Anode 3W RGB LEDs on: July 08, 2010, 01:47:21 am
Hi. I'm planning to build a lighting array using the 3W RGB leds from dealextreme.

The plan is to wire one of these "Quality AMC7135 350mA Regulated Circuit Board for DIY Flashlights 20-Pack"  to each cathode to limit the current.

Hoping someone with more experience could say if the circuit makes sense before I order all the parts.  I'm not sure what switching transistors to use as I want to control groups of up to about 10 leds in parallel at a time, which would need to drain a lot of current. Any suggestions?

Cheers  
Pages: [1]