Workshop 9 - Speaking to Arduino from Max MSP

We have controlled Max via hardware with the Arduino, but what if we want to go the other way? For example, maybe we want to make a nice control panel for the lighting in our room that will be on a tablet on the wall. Lets have a look at controlling an RGB LED with Max. 

We already know how to fade an LED, and an RGB LED is just 3 LEDs in one, a Red, Green and Blue. So, we should be able to fade them in different amounts to mix them and make any colour.

Lets rig up the following circuit:

 
RGB-LED_bb.png
 

... load up the following code:

/*
Receive a list of RGB values from MaxMSP 
and parse (seperate) them to control an RGB LED.

Luke Woodbury 16 January 2016

*/

//variables to store RGB values
int redVal = 0;
int greenVal = 0;
int blueVal = 0;

//constants (i.e. they won't change) to store LED RGB pins
const int greenPin = 11;
const int bluePin = 10;
const int redPin = 9;

void setup() {
//start serial to listen for Max data
Serial.begin(9600);

//set the pins as PWM output to fade LEDs
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(redPin, OUTPUT);
}

void loop() {
  //parse (split) the incoming list of 3 values
  //and store them in the variables
  redVal = Serial.parseInt();
  greenVal = Serial.parseInt();
  blueVal = Serial.parseInt();
  
  //write out the values to the LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);    
  analogWrite(redPin, redVal);     

}

... download and open the Max patch.

Follow the instructions in the patch to control the colour of the LED with the sliders or the colour swatch. Have a good look at the workings and see if you can work out what is going on. 

NEXT>>


Workshop 1- Hello World!

Simple use of a button (a digital input) to turn an LED on

Workshop 2 - Analog inputs/outputs

Reading a fader/knob/sensor (analog input) to fade an LED

Workshop 3 - PWM and servo motors

Control a servo motor using Pulse Width Modulation

Workshop 4 - Fritzing! 

Having a look at circuit design using Fritzing

Workshop 5 - Soldering

Some tips on soldering

Workshop 6 - Serial Communication

Exploring how to communicate with the computer via serial

Workshop 7 - Controlling Max MSP

A look at Max MSP and how we can speak to it from Arduino

Workshop 8 - Making a video system

A look at some of the built in video modules of Max and using Arduino to control them

Workshop 9 - Max to Arduino

Going the other way and using a Max user interface to control Arduino hardware