Urgent! - how to send multiple serial streams to Arduino

Hi, I need to try to figure this out in the next 2 hours!

I'm trying to send 3 variables from a piece of software over serial to the arduino to control the pwm value of three leds independently.

I've got it working successfully with one led being controlled but dont know how to do the same with more than one.

In the software I'm using I can format the serial in any way and I was thinking something like -

a x b x c x (where x is the variable value)

then the Arduino will send the value after 'a' to led 1, the value after 'b' to led 2 etc.....

Can anyone help? :astonished:

a,x,b,x,c,x,

so I need the arduino to

Can anyone help?

A usual good place to start is to post the code you are using that partly works for your situation.

const int Pin10 = 10;      // the pin that the LED is attached to


void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(Pin10, OUTPUT);

}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
if (brightness = 0) {
      analogWrite(Pin10, 0);
}
      else{ 
      analogWrite(Pin10, 255);        
      }      
  
    delay(10);
  }
}

a x b x c x

Assuming x is a byte -> you expect 6 bytes

void loop()
{
  if (Serial.available() >=6)
  {
     Serial.read(); // skip the letter a
     analogWrite(PINA, serial.read());

     Serial.read();  // skip letter b
     analogWrite(PINB, serial.read());

     Serial.read(); // skip letter c
     analogWrite(PINC, serial.read());
  }
}

a more intelligent design could be :

void loop()
{
  if (Serial.available() >=2)
  {
    char c = Serial.read(); // skip the letter a
    int val = serial.read(); // read the value byte
    switch (c)
    {
      case  'a' : analogWrite(PINA, val); break;
      case  'b' : analogWrite(PINB, val); break;
      case  'c' : analogWrite(PINC, val); break;
      default: break;
    }
  }
}

this latter would accept more complex strings

If the value is send in digits e.g. "a123b234c3" you need do something more, that is left as an exercise for you :wink:

succes,
Rob

How can I make sure that the right x value for a goes to the pin for a?

Ie that it is syncronised.

Can I send a carriage return after every message and get the Arduino to use this to begin reading through the bytes each time?

Many Thanks!!

mrboni:
if (brightness = 0) {

brightness will always be zero.

That "if" test will always be false.

@Nick
sharp vision !

Is that because it's a byte, not an int?

When receiving serial data to the arduino, does it always come in byte form?

Is that because it's a byte, not an int?

Who/what are you responding to? Use the quote function (the button with the yellow icon next to the button with the # icon) to quote what you are referring to.

When receiving serial data to the arduino, does it always come in byte form?

Yes. Remember, though, that chars and bytes are the same size, so character data can be retrieved easily. The return type for the Serial.read() function is an int, so that the function can return a -1 to indicate that an error occurred (which it could not do if the return type was byte), not so that it can return ints (double bytes).