Go Down

Topic: help fading two rgb leds odd out come. (Read 1 time) previous topic - next topic

Warwickben

so iam still learn this whole setup and i cant figure this out for the life of me.
i got stuck on the error code for having some thing plugged in to pins 0,1 for over a hour.
i know this is probably  easy,i want to light up a chess board. but a little differently.
i want two use rgb leds and just have them fade thru different hues.
and just reverse the code for the white vs black squares. so all the squares wont be the same colors at the same time. how to explain this stupid easy.the white would start a blue and fade to red. the black would start at red and fade to blue. figure test it with just 2 leds then move on.
i found this bit of code at http://make.larsi.org/electronics/ATmegaX8/led_rgb/
last night, im using the one on the very bottom RGB_analog_hue.

i had no problem getting it to work fading one rgb led thru different colors ets.
so i modded the code to do the same thing but for  two different leds. useing pins 5-7 and 9-11.
9-11 led fades fine but the led on pins 5-7 fades color jumps fades jumps.
see you tube link. http://www.youtube.com/watch?v=7Lne4pBFdq4
is it cause only 9-11 pins can do this? i can get both leds to fade right if i connect them all to ins 9-11 so i wired it right.

my code i // out what i changed. i could get the copy to forum thing to work right.

// RGB_analog_hue
// requires RGB Module

// Output Pins
#define PIN_RED            5    // red   LED, connected to digital pin  9
#define PIN_GREEN          6    // green LED, connected to digital pin 10
#define PIN_BLUE           7    // blue  LED, connected to digital pin 11
//#define PIN_RED2           9    // red   LED, connected to digital pin  2
//#define PIN_GREEN2        10   // green LED, connected to digital pin 3
//#define PIN_BLUE2         11    // blue  LED, connected to digital pin 4

#define WAIT               10   // 3ms delay

void color(int r, int g, int b)
{
  analogWrite(PIN_RED,   r);    // write current values to LED pins
  analogWrite(PIN_GREEN, g);
  analogWrite(PIN_BLUE,  b);
  //analogWrite(PIN_RED2,   r);    // write current values to LED pins
  //analogWrite(PIN_GREEN2, g);
  //analogWrite(PIN_BLUE2,  b); 
}

void hsv(float H, float S, float V)
{
  // http://www.easyrgb.com/index.php?X=MATH&H=21#text21

  int var_i;
  float R, G, B, var_1, var_2, var_3, var_h;

  if (S == 0) {
    R = V;
    G = V;
    B = V;
  }
  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) {
      R = V;
      G = var_3;
      B = var_1;
    }
    else if (var_i == 1) {
      R = var_2;
      G = V;
      B = var_1;
    }
    else if (var_i == 2) {
      R = var_1;
      G = V;
      B = var_3;
    }
    else if (var_i == 3) {
      R = var_1;
      G = var_2;
      B = V;
    }
    else if (var_i == 4) {
      R = var_3;
      G = var_1;
      B = V;
    }
    else {
      R = V;
      G = var_1;
      B = var_2;
    }
  }

  color(255 * R, 255 * G, 255 * B);  // RGB results from 0 to 255
}

void setup()
{
  pinMode(PIN_RED,   OUTPUT);   // sets the RGB pins as output
  pinMode(PIN_GREEN, OUTPUT);   
  pinMode(PIN_BLUE,  OUTPUT);
  //pinMode(PIN_RED2,   OUTPUT);   // sets the RGB pins as output
  //pinMode(PIN_GREEN2, OUTPUT);   
  //pinMode(PIN_BLUE2,  OUTPUT);
}

// Main program
void loop()
{
  for (float f = 0; f < 1; f += 0.0005) {
    hsv(f, 1, 1);
    delay(WAIT);
  }
}


i reverse my wiring order for the second led and was able to get them to fade in reverse but i would like to be able to have control over the black and white squares so i cant really use "my wiring hack"
i wont need to control every square Independently  just all the white squares do one thing and all the black do another.

johnwasser

Not all pins can do PWM.  See: analogWrite()

The pins without PWM go full off for values below 128 and full on for values above 127.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Warwickben

ok ok. 3,5,6        9,10,11 funny how little things screw me up so much.


Go Up