Go Down

Topic: RGB led... what gives????????? - (SOLVED) (Read 585 times) previous topic - next topic

Ruffsta

Feb 05, 2013, 07:01 pm Last Edit: Feb 05, 2013, 08:51 pm by Ruffsta Reason: 1
ok, i am using an RGB led (4 pins)..

this code works with "pinMode":
Code: [Select]
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..
Code: [Select]
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);
 }

MarkT

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)?
[ I won't respond to messages, use the forum please ]

PaulS

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

Common anode or common cathode?

Ruffsta

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..


AWOL

What does happen with digitalWrite, and have you spotted a pattern?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ruffsta

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

Grumpy_Mike

#7
Feb 05, 2013, 07:17 pm Last Edit: Feb 05, 2013, 07:20 pm by Grumpy_Mike Reason: 1
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?
Code: [Select]
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.

Ruffsta

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

Delta_G

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.

HazardsMind

These should be reversed if you have the anode HIGH.
Code: [Select]
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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Ruffsta

#11
Feb 05, 2013, 08:51 pm Last Edit: Feb 05, 2013, 09:04 pm by Ruffsta Reason: 1
yep... the low's and high's had to be reversed... thank you

and i shortened the code as well..
Code: [Select]
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..

AWOL

Quote
just need to learn how to fade them
try analogWrite, on appropriate pins
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


AWOL

Feel free to add any details you feel necessary.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up