Controlling different leds over serial

Im looking to make a holiday lights setup for my home. I have multiple strips Id like to use but Im not sure how to code it. How do I change between outputs on the arduino? Say I wanna control my Santa displays' individual eyes each with an RGB led, how would I switch control between the two eyes?

Use a port extender chip or shift register to give you more I/O signals.

Thanks.. As far as coding goes, how exactly do I switch between control of the two leds?(Santa's eyes) Would assigning each led a char work with an if/else statement? Like char 'A' would control the left eye while char 'B' would control the right eye, or am I completely off?

Each output on the shift register would be connected to a seprate LED. So you just set that output high to turn it on. Look at the shift out tutorial. It is just a matter of sending the shift register the pattern of zeros and ones, in the form of a number to turn the LEDs you want on and off.

that wiring seems excessive right now. I think I'll just go with transistors

Ok Im just trying to walk before I run so to speak. Right now I have two rgb leds hooked up to my arduino, each represent one of Santa's eyes. Im looking to control each one individually with a simple AI app I will build but Im having trouble with code. I'm fairly new to Arduino, but I found this code in another related post(http://arduino.cc/forum/index.php/topic,124977.0.html) for BT communication:

//loop
  char c;
  if (mySerial.available()) 
  {
    c = (char)mySerial.read();
    if (c=='H')
       {
         ///// if the key is a letter H do your code here
         //comprende?
       }
  }

In this example code, would I replace the 'H' with a 'L' (for left eye) then have make a similiar if statement for the right eye?

//loop
char c;
if (mySerial.available()){
c = (char)mySerial.read();
if (c=='L')
   {
     redvalueL=BTSerial.read(); 
      greenvalueL=BTSerial.read();
      bluevalueL=BTSerial.read();
   }
  analogWrite(redpin, redvalueL);
  analogWrite(greenpin, greenvalueL);
  analogWrite(bluepin, bluevalueL);
}

for BT communication

Why? Are you using Blue tooth?

You need to say exactly how you have the LEDs wired up in order to get any help.

In this example code, would I replace the 'H' with a 'L' (for left eye) then have make a similiar if statement for the right eye?

No, that example is crap.

if (mySerial.available()){
c = (char)mySerial.read();
if (c=='L')
   {
     redvalueL=BTSerial.read(); 
      greenvalueL=BTSerial.read();
      bluevalueL=BTSerial.read();
   }

Likely, this code won't even compile. But, think about what this is doing even if you change BTSerial to mySerial. If there is at least one byte of serial data, read which eye the byte is for, then read the next three bytes which most likely haven't arrived yet.

Not a good idea.

Grumpy_Mike:

for BT communication

Why? Are you using Blue tooth?

You need to say exactly how you have the LEDs wired up in order to get any help.

I have a single rgb led in pwm pins 3, 5, 6 and 9, 10, 11. I'd like to try BT out if possible.

I'd like to try BT out if possible.

You can do that AFTER you learn about serial data and wiring LEDs.

I have a single rgb led in pwm pins 3, 5, 6 and 9, 10, 11.

The LED has 4 legs that you have wired to 6 pins. How? You need to post a schematic or a picture of that trick.

PaulS:

In this example code, would I replace the 'H' with a 'L' (for left eye) then have make a similiar if statement for the right eye?

No, that example is crap.

if (mySerial.available()){
c = (char)mySerial.read();
if (c=='L')
   {
     redvalueL=BTSerial.read(); 
      greenvalueL=BTSerial.read();
      bluevalueL=BTSerial.read();
   }

Likely, this code won't even compile. But, think about what this is doing even if you change BTSerial to mySerial. If there is at least one byte of serial data, read which eye the byte is for, then read the next three bytes which most likely haven't arrived yet.

Not a good idea.

I didnt catch that typo, sorry. So how would I change control between one led and the other? Should I have it setup like this: if(mySerial.available() >3)...making sure there's 4bytes in the buffer? Or is that no good either?

Why are you reluctant to say how you have wired things up? What value resistors are you using? Is it because you are hiding something you don't want people to know?

PaulS:

I'd like to try BT out if possible.

You can do that AFTER you learn about serial data and wiring LEDs.

I have a single rgb led in pwm pins 3, 5, 6 and 9, 10, 11.

The LED has 4 legs that you have wired to 6 pins. How? You need to post a schematic or a picture of that trick.

You can do that AFTER you learn about serial data and wiring LEDs.

your right... lol, they're common Anode leds so the anode is hooked to 5V

Blood out of stone time.

And what value resistors are you using!

Should I have it setup like this: if(mySerial.available() >3)…making sure there’s 4bytes in the buffer? Or is that no good either?

That’s better, but still not perfect. I’ll try to explain why. Reading and acting on 4 values read from the serial port is fine, up to a point. Suppose that the serial buffer contains ‘L’, 120, 34, 95, ‘R’, 14, 126, 240. Now, if you test that there are 4 or more bytes, and read the first 4 bytes, you have all the data needed to control one LED, and you know which one to control.

But, this relies on serial data never getting lost, which is a bad thing to rely on. Suppose that the buffer contained ‘L’, 120, 34, 95, ‘R’, 14, 126, ‘L’, 120, 34, 95, ‘R’, 14, 126, 240.

Now, you’ll read the L, 120, 34, and 95, and set the appropriate LED to the appropriate levels. There are still 4 or more bytes, so, you’ll set collect the next 4 bytes, and set the right LED to 14, 126, and ‘L’.

Hmmm, that might not be a really good idea. There are still more than 4 bytes, so, next you’d set the LED whose ID was 120 (there isn’t one) to 34, 95, and ‘R’. That doesn’t look too good, either.

There are several ways around the problem. You could read 4 bytes, peek at the next one, that you might need to wait for, and, if it’s a ‘R’ or ‘L’, you’d have reasonable confidence that the 4 bytes you read were good, and you’d use them. If not, you’d discard the 4 bytes, without using them, and read and discard any byte that was not a ‘R’ or ‘L’.

The problem here, though, is that ‘L’ is 76 and ‘R’ is 82, both of which are perfectly reasonable values for the LED color level.

You could send the values, as packets, as strings, rather than bytes. “<R, 120, 34, 95>”. Here, the start and end of the packet are clearly defined. If a start or end marker get lost, you have a way of recovering. If a byte within the packet is lost, it is either a ID, a delimiter, or part of a value. If a comma is lost, that is easy to detect. If the ID is lost, that is easy to detect. If a part of a value is lost, that is harder to detect (120 could become 12 or 20), but not impossible. You could add a length to the packet, and/or a checksum.

The tradeoff with sending values as ASCII strings is that it requires more bytes (which take longer to transmit) and requires more processing to turn “120” back into 120.

It comes down to the speed you need, and the risk you are willing to accept.

Grumpy_Mike: Why are you reluctant to say how you have wired things up? What value resistors are you using? Is it because you are hiding something you don't want people to know?

well I do want alot of leds for my display like I stated in the first post but Im also trying to hack this big santa display. I think Im moving too fast, so I just want to get the code down and functioning on a single 3mm led. But these 3mm will be replaced with high powered leds(not there yet though) Then I'll figure out how to expand the circuit from that with more leds with either transistors or shift registers and possibly BT. I found a tutorial on Adafruit similiar to what Im looking to do: http://www.ladyada.net/products/rgbledstrip/ but the strips would be replaced with a high powered led. Im using 100 ohm resistors right now. How can I draw out my circuit with the arduino and breadboard like in the tutorial?

Im using 100 ohm resistors right now.

A bit low but should be ok.

You need to add code to drive those.

Then I'll figure out how to expand the circuit from that with more leds

Fine that is a project in itself with diffrent ways to go however you might want to look at this:- ShiftPWM library http://www.elcojacobs.com/shiftpwm/

How can I draw out my circuit with the arduino and breadboard like in the tutorial?

fritzing.org/

But please post a real schematic.

PaulS:

Should I have it setup like this: if(mySerial.available() >3)...making sure there's 4bytes in the buffer? Or is that no good either?

That's better, but still not perfect. I'll try to explain why. Reading and acting on 4 values read from the serial port is fine, up to a point. Suppose that the serial buffer contains 'L', 120, 34, 95, 'R', 14, 126, 240. Now, if you test that there are 4 or more bytes, and read the first 4 bytes, you have all the data needed to control one LED, and you know which one to ...

hmm, I see what your saying, thank you for the detailed explanation. Will flushing the buffer and changing the baud rate help if I choose to send bytes?

I’m sure you’ll get your C program sorted out eventually.

Meanwhile, if you just want to bench test the LEDs, you can install Bitlash (http://bitlash.net) and define little test functions like this to debug your circuit:

// Bitlash functions to independently control two RGB LEDs on pins 3/5/6 and 9/10/11
function setup { pinmode(3,1); pinmode(5,1); pinmode(6,1); pinmode(9,1); pinmode(10,1); pinmode(11,1); }
function setleft  { a3=arg(1); a4=arg(2); a5=arg(3); }
function setright { a9=arg(1); a10=arg(2); a11=arg(3); }

Then, in the serial monitor, you can tell bitlash to set the left one red and the right one yellow:

> setleft(255,0,0); setright(0,255,255);

Without writing any parsing code.

-br

Will flushing the buffer and changing the baud rate help if I choose to send bytes?

No. Flushing the buffer, in the pre-1.0 sense of dumping random amounts of unread data, hardly seems like a good idea. Flushing the buffer, in the post-1.0 sense of blocking until the outgoing buffer is empty, hardly seems useful.

Changing the baud rate won't affect noise, poor connections, etc. that are the general cause of serial data corruption/loss.