AnalogWrite LED problems MAX/MSP to Arduino.

I have 3 LEDs, (Red/Green/Blue) each LED is Connected to a Pin (9, 10 & 11 in this case.) on the Arduino. I also have 3 Faders in MAX/MSP, each Fader outputs a value between (0-255) I understand that in order to control each of my LEDs, I can send data via the serial object in max. No problems so far, as I have managed to send a value (0-255) from a fader to a single pin (LED) using analogWrite. My problem is that I want to write to each pin separately & independently. How can I get Arduino to recognize which pin I am sending the (0-255) value for?

I think that I may have to send data in from MAX/MSP in the format.. R255 G255 B255 (or any number from 0-255)

I just need to be pointed in the right direction with this. Thanks in advance.

Code so far..

int outputPinR = 9 ; int outputPinG = 10 ; int outputPinB = 11 ; int val;

void setup() { Serial.begin(19200);

pinMode(outputPinR, OUTPUT); pinMode(outputPinG, OUTPUT); pinMode(outputPinB, OUTPUT); digitalWrite(outputPinR, HIGH); digitalWrite(outputPinG, HIGH); digitalWrite(outputPinB, HIGH); delay(5000); digitalWrite(outputPinR, LOW); digitalWrite(outputPinG, LOW); digitalWrite(outputPinB, LOW); // just to test that the LEDs are working }

void loop() { if (Serial.available()) { val = Serial.read(); { analogWrite(outputPinR, val);} // the bit that recognizes my 0-255 fader value from Max } }

maybe just send the three bytes one after the other, so first bye is the red value, then the green value and then the blue value. You must indicate the beginning of a "color message", so I suggest: use values 0..254 for the colors and use 255 for the sync if this is possible with max...

then the (untested) code might look like this:

void loop()
{ 
  if (Serial.available())
  {
    val = Serial.read();
    if(val == 255)//START or SYNC
    {
      //now read three values (rgb), each in the range 0..254
      while(!Serial.available()){};
      analogWrite(outputPinR, Serial.read());
      while(!Serial.available()){};
      analogWrite(outputPinG, Serial.read());
      while(!Serial.available()){};
      analogWrite(outputPinB, Serial.read());
    }
  }
}

Wow! That was a speedy response, Thank you I shall try it out and report back if it works.

By the way, is there a tutorial available that outlines this process. I have a feeling that I will be tinkering in this area for a while.

I don't know a tutorial... it is protocol design. And this protocol is bad, because it steals a valid value from each color... but I am sure, you will not notify any difference between red 254 and 255. Maybe you find some information about (communication) protocol design in google.

And maybe you can skip the while(!Serial.available()){}; lines... but I am not sure right now. I hope that Serial.read() is blocking, but I am not sure at the moment.

No - it didn't work Max/Msp is sending out the 255 value followed by the three 0-255 values in quick succession to the serial port, but alas no luck. I am new to this so I shall keep looking for an answer.

int outputPinR = 9 ; int outputPinG = 10 ; int outputPinB = 11 ; int val;

void setup() { Serial.begin(19200);

pinMode(outputPinR, OUTPUT); pinMode(outputPinG, OUTPUT); pinMode(outputPinB, OUTPUT); digitalWrite(outputPinR, HIGH); digitalWrite(outputPinG, HIGH); digitalWrite(outputPinB, HIGH); delay(5000); digitalWrite(outputPinR, LOW); digitalWrite(outputPinG, LOW); digitalWrite(outputPinB, LOW); // just to test that the LEDs are working }

void loop() { if (Serial.available()) { val = Serial.read(); if(val == 255)//START or SYNC { //now read three values (rgb), each in the range 0..254 while(!Serial.available()){}; analogWrite(outputPinR, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinG, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinB, Serial.read()); } } }

is max transmitting bytes or ascii encoded characters? Does it send the value 100 or the string "100"?

And limit the output for the colors, 255 is no longer a valid value for any coloe... in max you must limit the faders to 0..254

Bohne, I thank you for attention and help.

I just needed to reset the arduino after I opened my Max patch.

Thanks again - I have been stuck with this for a while now.

Peace. ;D

One thing leads to another.. as always..

During testing (which involves swiching all leds from a mixed high value to OFF or 0) every now and again ( there doesn't seem to be a strict pattern to it) i'll get just the blue led or just the red led , even though the values that I am sending remain the same. Any ideas? :o

right! got everything working now. One more question.. if I wanted to control more LEDs.. would the code below be along the right lines? (I am currently working on this now)

int outputPinR1 = 3 ; int outputPinG1 = 5 ; int outputPinB1 = 6 ; int outputPinR = 9 ; int outputPinG = 10 ; int outputPinB = 11 ; int val;

void setup() { Serial.begin(19200);

pinMode(outputPinR, OUTPUT); pinMode(outputPinG, OUTPUT); pinMode(outputPinB, OUTPUT); pinMode(outputPinR1, OUTPUT); pinMode(outputPinG1, OUTPUT); pinMode(outputPinB1, OUTPUT); digitalWrite(outputPinR, HIGH); digitalWrite(outputPinG, HIGH); digitalWrite(outputPinB, HIGH); digitalWrite(outputPinR1, HIGH); digitalWrite(outputPinG1, HIGH); digitalWrite(outputPinB1, HIGH); delay(5000); digitalWrite(outputPinR, LOW); digitalWrite(outputPinG, LOW); digitalWrite(outputPinB, LOW); digitalWrite(outputPinR1, LOW); digitalWrite(outputPinG1, LOW); digitalWrite(outputPinB1, LOW); // just to test that the LEDs are working }

void loop() { if (Serial.available()) { val = Serial.read(); if(val == 255)//START or SYNC { //now read three values (rgb), each in the range 0..254 while(!Serial.available()){}; analogWrite(outputPinR, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinG, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinB, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinR1, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinG1, Serial.read()); while(!Serial.available()){}; analogWrite(outputPinB1, Serial.read()); } } }