[newbie] from "dimmer" example to RGB dimmer (Max)

Hello, I'm new in this forum. nice to meet you guys.

I try to control an RGB LED from Max. I'm using pin 11, 10, 9 connected to RGB LED and try to fade in/out individual colors from Max GUI. It works fine when it's only one value but how can I send Serial value to 3 different places to control each color individually? my Arduino and Max code is based on the example "dimmer", basically I want to do the same thing with 3 Max faders with RGB LED controlled individually...

'hope that my question is clear.


// RGB LED contorolled by SC+Max synths

const int gPin = 11;
const int bPin = 10;
const int rPin = 9;

void setup(){
  pinMode(gPin, OUTPUT);
   pinMode(bPin, OUTPUT);
    pinMode(rPin, OUTPUT);

void loop(){
  byte dimR;
  byte dimG;
  byte dimB;
  dimR = Serial.read();
  dimG = Serial.read();
  dimB = Serial.read();
  analogWrite(gPin, dimG);
  analogWrite(bPin, dimB);
  analogWrite(rPin, dimR);

  dimR = Serial.read();
  dimG = Serial.read();
  dimB = Serial.read();
  analogWrite(gPin, dimG);
  analogWrite(bPin, dimB);
  analogWrite(rPin, dimR);


The Serial.available() function's return type is an int. It tells you how many bytes are available to read.

The way you are using it, the body of the if block will be executed if there is at least one byte available to read.

Suppose there is only 1 byte. You then happily read all 3 of them. Oops.

Suppose there is only 1 byte. You then happily read all 3 of them. Oops.

Exactly. The reason why I'm doing this is because I don't know how to use this byte to control three different pins.

If Serial.read can read bytes from only one channel then there's no way to use Serial byte to 3 different places?

I have this max patch with 3 sliders each one outputs 0-255 and send to arduino.

then I want that arduno interprete these 3 individual values to map to three different pins to fade in/out three RGB values individually.

please help me....it's so frustrating... :'(

Since Serial.available() tells you how many bytes are available, and you know that you need 3 bytes:

if(Serial.available()[glow] >= 3[/glow])
   // Read the 1st three bytes

well, I'm sorry to be this bad newbie and insist, but I try my best to be clear again.

I need an ID of incoming data. ok, there's 3 "bytes" available. (I don't know really what this byte means..)

Serial.read(0); Serial.read(1); Serial.read(2);

will return error:

error: no matching function for call to 'HardwareSerial::read(int)

So it seems that Serial.read() does not take parameter as an ID. How do I divide the incoming data to map to three different pins?

The Serial.print doesn't do anything for me, nothing is printed not in error console nor in monitor.

So, I don't know what is the format of the incoming data. I need to know at least the format of incoming data to be able to map them.

incoming data: ID/value

will be perfect.

then I can,

when the ID is 0 (if ID == 0) valueR = Serial.read() and read only from ID 0 alanogWrite(ledR, valueR)

when ID is 1, valueG = Serial.read() and please read only from ID 1 alanogWrite(ledG, valueG)

....and so on.

is it possible?

when some application send arduino the serial data, 0-255, it means one set of values 0 - 255? or can it be multiple?

can I send

0 - 255 number one

0 - 255 number two

0 - 255 number three

and so on?

for example, if I use analogWrite, than I can define each pins, and these pins will have "ID"s.

analogWrite(pin, value)

so that I can use, for example, three potentiometers to control three different LEDs because Arduino will send out values to digital outs only when pin == x, and NOT pin == some illegal pins.

I feel that there MUST be way that one can use Serial values in various places in code, I can't believe that only ONE SERIAL value is available for one arduino board. then how it's possible people do such wonderful projects with this board??

can I use Serial.read as

Serial.read(ID, value)?

I know that it's not like that, but is there a way to "simulate"?

I really really hope that my question is clear this time. I tried my best...

thank you very much.

Serial data is placed in a buffer when it arrives at the Arduino. The Serial.available() function tells you how many bytes are in the buffer.

Serial.read() removes a value from the buffer, returning the byte if removed.

So, to get 3 values:

if(Serial.available() >= 3)
   byte r = Serial.read();
   byte g = Serial.read();
   byte b = Serial.read();

   // Set all 3 pins

It is up to you, then, to make sure that MAX sends all three values whenever one changes.

You could have MAX send something like color:value (R45, B118, G1). Then, you’d read 2 bytes. The 1st byte (R, G, or B) would tell you which pin to send the value to, and the 2nd byte would tell you the value.

if(Serial.available() >= 1)
   byte p = Serial.read();
   byte v = Serial.read();

   if(p == 'R')
      // set red pin to v
   else if(p == 'G')
      // set green pin to v
   else if(p == 'B')
      // set blue pin to v