Forums > MaxMSP

Controlling 2 LEDs using Max and Arduino

March 17, 2014 | 7:41 pm

I’ve been researching for a while on how to control 2 leds using Max and Arduino. I just want to control 2 at this point in time.

I’ve seen this topic: Here and I have tried everything that is in there. I can get the dimmer tutorial posted there to work fine, but I don’t even know where to begin so as to control a second led.

Also I have tried Maxuino but I can not find any good tutorials, and it confuses me to no end.

See I don’t just want to control leds, I want to put the two on the sliders, and then control the slider using my kinect. I have figured that part out, now I just need to be able to do it for 2 leds instead of one.


March 17, 2014 | 11:17 pm

Are you looking to turn the lights on and off, or get them to vary brightness?


March 18, 2014 | 9:30 am

I am trying to control the brightness.


March 18, 2014 | 10:38 am

I tend to pack the values I want to send to LEDs/servos/motors/whatever in a list, ended by a ‘\n’ so I can parse them at the other end easily. I’ve also heard good things about the Cmd Messenger library for parsing comma-separated messages.

Patch for controlling brightness of two LEDs:

– Pasted Max Patch, click to expand. –

Arduino code:

int led1 = 3;
int led2 = 5;

int led1Brightness;
int led2Brightness;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
led1Brightness = Serial.parseInt();
led2Brightness = Serial.parseInt();
if (Serial.read() == ‘\n’) {
analogWrite(led1, led1Brightness);
analogWrite(led2, led2Brightness);
}
delay(10);
}
}

EDIT: The delay/speedlim times are pretty arbitrary in those examples. I limit the speed of the messages being sent to the Arduino to prevent the slowdown you get when you fire too many messages at serial, and the delay in the Arduino is a ‘best practice’ to let the CPU rest.


March 18, 2014 | 11:55 am

Medd I’m trying to use your arduino code but it is giving me an error message saying there is an extra \ in the code and highlighting this part:

if (Serial.read() == ‘\n’)

What do I need to do to get that fixed?

Edit:
So I got the code to upload into the arduino, but I can’t get the max patcher to communicate to the arduino.

On another note I got The code below to work but I need to control the brightness of the leds, not just turn them on and off.

/*
Serial RGB LED controller
by Tom Igoe
adaboed by Scott Fitzgerald
Controls three LEDs whose legs are
connected to pins 9, 10, and 11.
*/
// constants to hold the output pin numbers:
const int LEDone = 9;
const int LEDtwo = 10;
const int LEDthree = 11;
int currentPin = 0; // current pin to be faded
int brightness = 0; // current brightness level
void setup() {
// initiate serial communication:
Serial.begin(9600);
// initialize the LED pins as outputs:
pinMode(LEDone, OUTPUT);
pinMode(LEDtwo, OUTPUT);
pinMode(LEDthree, OUTPUT);
}
void loop() {
// if there’s any serial data in the buffer, read a byte:
if (Serial.available() > 0) {
int inByte = Serial.read();
Serial.write(inByte);
// respond to the values ‘a’, ‘b’, ‘c’, or ’0′ through ’9′.
// you don’t care about any other value:
if (inByte == ‘a’) {
currentPin = LEDone;
}
if (inByte == ‘b’) {
currentPin = LEDtwo;
}
if (inByte == ‘c’) {
currentPin = LEDthree;
}
if (inByte >= ’0′ && inByte < = ’9′) {
// map the incoming byte value to the range of the analogWrite() command
brightness = map(inByte, ’0′, ’9′, 0, 255);
// set the current pin to the current brightness:
analogWrite(currentPin, brightness);
}
}
}

– Pasted Max Patch, click to expand. –

March 18, 2014 | 8:19 pm

After playing around all day with the codes and upon further research I was able to get everything to communicate just as I wanted them too. Below is the resulting code.

const int LEDone = 9;
const int LEDtwo = 10;

int currentPin = 0;

int brightness = 0;

void setup(){
Serial.begin(9600);
pinMode(LEDone, OUTPUT);
pinMode(LEDtwo, OUTPUT);
}

void loop(){
if(Serial.available()>0){
int inByte = Serial.read();
Serial.write(inByte);

if(inByte =='a'){
currentPin = LEDone;
}

if(inByte == 'b')
{ currentPin = LEDtwo; }

if (inByte>= 0 && inByte<= 255){
brightness = inByte;
analogWrite(currentPin,brightness);
}
}

}

Attachments:
  1. Screen-Shot-2014-03-18-at-10.18.30-PM

March 19, 2014 | 6:35 am

Glad you got it working.

The error you mentioned in my code happened to me when I pasted my code across as there was weird formatting on the apostrophes in ‘\n’ added by the forum!

Not sure what problem you’re having with the Max patch? Did you add the serial port as an argument to the serial object? It all works fine for me.


Viewing 7 posts - 1 through 7 (of 7 total)