RGB led... what gives????????? - (SOLVED)

ok, i am using an RGB led (4 pins)..

this code works with "pinMode":

int red = 8;
int green = 9;
int blue = 10;


void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
//RED
pinMode(red, HIGH);
pinMode(green, LOW);
pinMode(blue, LOW);
delay(500);
//OFF................
pinMode(red, LOW);
pinMode(green, LOW);
pinMode(blue, LOW);
delay(50);
//GREEN
pinMode(red, LOW);
pinMode(green, HIGH);
pinMode(blue, LOW);
delay(500);
//OFF................
pinMode(red, LOW);
pinMode(green, LOW);
pinMode(blue, LOW);
delay(50);
//BLUE
pinMode(red, LOW);
pinMode(green, LOW);
pinMode(blue, HIGH);
delay(500);
//OFF................
pinMode(red, LOW);
pinMode(green, LOW);
pinMode(blue, LOW);
delay(50);
}

now this is the same EXACT code, but with "digitalWrite" - but it DOESN'T work..

int red = 8;
int green = 9;
int blue = 10;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop() {
  //turn red on
  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
//OFF................
    digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  //turn green on
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, LOW);
  delay(500);
//OFF................
      digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  //turn blue on
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, HIGH);
  delay(500);
//OFF................
      digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  }

The first example is nonsense - you shouldn't pass "LOW" or "HIGH" to pinMode(). Or rather you'd have to look at the definitions of LOW, HIGH, INPUT, OUTPUT to work out what's actually happening.

How is this LED connected? You are using current limiting resistors per LED aren't you (otherwise you can burn out the LED and/or Arduino)?

ok, i am using an RGB led (4 pins)…

Common anode or common cathode?

my connections are fine.. and yes i am using resistors.. i may be new, but i'm not stupid but it's odd that the lights come on EXACTLY the way i want them to with pinMODE but not with digitalWrite.. just can't figure out WHY it's doing that..

common anode..

What does happen with digitalWrite, and have you spotted a pattern?

colors are off and i tried moving the wires around.. but nada

What you are doing with pinMode is setting the pins to be outputs or inputs, even when you write high and low with them, So they will turn to outputs, sink current and light the LED. When they are inputs they are high impedance and they don't sink current and so the LED is off. It is not the real way to do it though.

How is it wired up?

pinMode(red, HIGH);
pinMode(green, LOW);
pinMode(blue, LOW);

Will not turn on just red if it wired correctly. Anode to +5V, red cathode to resistor to arduino output, same for the other two colours.

my connections are as follows..

red connected to resistor to arduino pin 8 common anode connected to 5v green connected to resistor to arduino pin 9 blue connected to resistor to arduino pin 10

What are you expecting vs what you are getting. As written the second code should give you blue-green then white then purple then white then yellow. The way you describe the hookup the LEDs will light when written LOW and turn off when written HIGH.

These should be reversed if you have the anode HIGH.

void loop() {
  //turn red on
  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
//OFF................
    digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  //turn green on
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, LOW);
  delay(500);
//OFF................
      digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  //turn blue on
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, HIGH);
  delay(500);
//OFF................
      digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
  delay(500);
  }

Flip the Highs and Lows

yep... the low's and high's had to be reversed... thank you

and i shortened the code as well..

int red = 8;
int green = 9;
int blue = 10;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop() {
  //turn red on
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  delay(500);

  //turn green on
  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, HIGH);
  delay(500);

  //turn blue on
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(blue, LOW);
  delay(500);

  }

just need to learn how to fade them instead of them just blinking the colors..

just need to learn how to fade them

try analogWrite, on appropriate pins

nothing happens...

Feel free to add any details you feel necessary.

All your wires must be in the PWM pins. 8 is not PWM

on my mega 2560... 8-13 are pwm's..

Is this going to be a "20 questions" thread?

nope, it can be locked now… i’ll do fading another day, just glad i finally have this working the way i wanted it to…

oh, found this:

#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 20

void setup() {

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
}

int redVal;
int blueVal;
int greenVal;
 
void loop() {
 
  int redVal = 255;
  int blueVal = 0;
  int greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    greenVal += 1;
    redVal -= 1;
    analogWrite( GREEN, 255 - greenVal );
    analogWrite( RED, 255 - redVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blueVal += 1;
    greenVal -= 1;
    analogWrite( BLUE, 255 - blueVal );
    analogWrite( GREEN, 255 - greenVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 255;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    redVal += 1;
    blueVal -= 1;
    analogWrite( RED, 255 - redVal );
    analogWrite( BLUE, 255 - blueVal );

    delay( delayTime );
  }
}

yeah it fades through all the colors… but i wanted to control the colors individually…
ex:

  //turn red on
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  delay(500);

but say i wanted orange or pink or pure yellow… is there a chart or something… like put numbers in there instead of HIGH/LOW?

but say i wanted orange or pink or pure yellow.. is there a chart or something.. like put numbers in there instead of HIGH/LOW?

The digitalWrite() function only turns pins on or off. The analogWrite() function, on the other hand, allows you to put numbers in there.

On an RGB LED, there is no such thing as "pure yellow". You need to get three potentiometers, and adjust them to get colors you like. Make note of the numbers, and you can get "pure yellow" or pink or orange.

There are charts that show what r, g, and b values make up colors, for web pages, etc. Those charts assume perfectly linear response to change in r, g, and b values. An LED will NOT exhibit a linear response, nor will the human eye. That's why you need to experiment to get the colors you want.