Controlling rgb LED via Arduino

Hello,

I'm trying to turn on different colors according to the input from Processing.

  • Processing sends to arduino a byte value (x) from 0 to 222. This value is changing by my mouseX movement
  • Arduino needs to light up red when x<70, green when 70<x<140 and blue when x>140

I have managed to make the "dimmer" example work. Here is my adjusted "dimmer":

int rpin = 9;
//int gpin = 10;
//int bpin = 11;

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  pinMode(rpin, OUTPUT);
  //pinMode(gpin,OUTPUT);
  // pinMode(bpin, OUTPUT);
}

void loop() {  
  byte x;

  if (Serial.available()) {
    x = Serial.read();

    analogWrite(rpin, x);

  }
}

The red color lights up and dims when x increases.

But I'd like to divide 222 pixels into 3 zones (as described at the top) and light up colors according to the x value.

I have tried with digitalWrite method and if statements...didn't really work. Can post them if needed.

Seems like analogWrite() works because 9,10 and 11 are PWM pins? SHould I be looking into digitalWrite at all?

Thank you!

M

Below is my attempt for red and green colors only. What happens is that led starts yellow (red + green) at x above 50, and the when I move x down towards 50, it changes to green.
Then it stays green even if I move x above 50.
It's weird.
What's wrong with the code?
(Again, I'd like red light up until x=70, green 70<x<140, and blue x>140.)

int rpin = 9;
int gpin = 10;
//int bpin = 11;

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  pinMode(rpin, OUTPUT);
  pinMode(gpin,OUTPUT);
  //pinMode(bpin, OUTPUT);
}

void loop() {  
  

  if (Serial.available() > 0) {
    
    if (Serial.read() < 50) {
    analogWrite(rpin, Serial.read());
    }
    else if (Serial.read() > 50) {
    analogWrite(gpin, Serial.read());
    }
  }
}

I think you need to change this part

if (Serial.available() > 0) {
    
    if (Serial.read() < 50) {
    analogWrite(rpin, Serial.read());
    }
    else if (Serial.read() > 50) {
    analogWrite(gpin, Serial.read());
    }

The situation is you likely get 1 byte in, but then read the in-buffer on the UART twice.
Better would be to read it once & store it, then base your decision & writes on the stored #,
and add the other condition you wanted.

if (Serial.available() > 0) {
[glow]incomingByte = Serial.read;[/glow]   
    if (incomingByte < 70) {
    analogWrite(rpin, incomingByte);
    }
    
    else if (incomingByte >= 70 && incomingByte < 140) {
    analogWrite(gpin, incomingByte);}
    
    else if (incomingByte >= 140) {
    analogWrite(bpin, incomingByte);}
    }

Seems like analogWrite() works because 9,10 and 11 are PWM pins? SHould I be looking into digitalWrite at all?

Do you want the LED's color segments to have their brightness correlated to the values you are passing from your processing sketch? If yes, then analogWrite() and PWM are your only options.

If you just want to turn on the color segments based on the value then you can use digitalWrite() and if statements.

As CrossRoads points out, you can't make multiple calls to Serial.read(). Each time you do, the buffer gets flushed.

Thanks so much CrossRoads. That totally did it!

I might have already been there, but I haven't noticed the difference in the lights.
The byte value was visually not very noticeable, I had to optimize the code with some values ie for example multiplied by 3 for incomingbyte<70:
if (incomingByte < 70) {
analogWrite(rpin, incomingByte*3);
}

multiplied by 1.5 for the 70<incomingbyte<140
and no change (multiply by 1) for >140.

@ James C45

Do you want the LED's color segments to have their brightness correlated to the values you are passing from your processing sketch? If yes, then analogWrite() and PWM are your only options.

If you just want to turn on the color segments based on the value then you can use digitalWrite() and if statements.

Thanks James. That helped. When I move the mouseX, the red led lights up first gradually increasing in brightness, then after 1/3 of the screen the green one starts lighting up and then the blue. So yes, I'm using pwm.

But I think I might want to simply make them light up based on their value --> digitalWrite(). They shine brighter and might divide the area into 6 parts instead of three.

The setup I have is a live camera capture of a fish in a tank, using Processing. When the fish moves, different leds illuminate the tank. leds are very low brightness as you can imagine, finding a more powerful alternative will also be the next step.

Matjaz

Brightness - what is your LED set up now?
You can have strings of LEDs in series, with a 12V source typically 5 with a current limit resister controlled via NPN transistor or N channel FET that the arduino drives, can put several strings in series.
You can get very bright LEDs here
www.superbrightleds.com
component LEDs with thousands of mCD, very bright!

Brightness - what is your LED set up now?

I'm using red, green and blue leds from RadioSHack:
3.2V, 20mA ultra high brightness, with intensity 8000 mcd.
220 Ohm resistor for each led. Circuit: pwm pin 9--> 220ohm resistor --> led ground pin --> led anode into 5V pin. Same for green and blue.

But they don't shine brightly (they look they could shine brighter). Perhaps because I'm powering them without the external supply with only 5V (3.2V)?

Have never tried that. Do you simply plug in 9 or 12V adapter into the Arduino? I'd have to use different resistors wouldn't I (I use resistor calculator to get the proper values)?

The superbrightleds you're recommending include those for the boats or bed lamps - that kind of brightness would be exactly what I'd like.

Any recommendations?

Thank you!

Matjaz

I don't think you're getting 20mA.
If you have 3.2V across the LED, that means only 1.8V across the resister.
1.8V/220 ohm = 8mA.

1.8/20mA = 90 ohm

Anyway,
You could set up an array like the 20 LEDs I have at the upper right here with 1 transistor controlled by the arduino to turn them on/off. The arduino is powered by 5v, the LEDs by 12v.

http://www.crossroadsfencing.com/Extension_light.jpg