Go Down

Topic: Controlling rgb LED via Arduino (Read 1 time) previous topic - next topic

Matjaz

Dec 20, 2010, 09:21 pm Last Edit: Dec 20, 2010, 11:06 pm by Matjaz Reason: 1
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":

Code: [Select]

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

Matjaz

#1
Dec 20, 2010, 11:02 pm Last Edit: Dec 20, 2010, 11:09 pm by Matjaz Reason: 1
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.)



Code: [Select]

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());
   }
 }
}

CrossRoads

#2
Dec 21, 2010, 03:06 am Last Edit: Dec 21, 2010, 03:07 am by CrossRoads Reason: 1
I think you need to  change this part
Code: [Select]
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.
Code: [Select]
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);}
   }




Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

James C4S

Quote
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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Matjaz

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

Go Up