QUESTION about using Lightuino channels for PWM, controlled by Max MSP

183 views
Skip to first unread message

Justin L

unread,
Jun 14, 2012, 12:46:52 AM6/14/12
to toasted-circu...@googlegroups.com
Hi Andrew,

My name is Justin and I just purchased a lightuino board from you. I am very excited to incorporate it into my project, but as a beginner with Arduino stuff, I am a little confused here and there. I was hoping to get a little advice on how to format a sketch to allow me to individually control each sink channel via PWM, and get those values through the serial port from max msp in real time. The project is basically scaling audio amplitude to LED brightness, emulating a digital VU meter (except with many leds). I was able to do it on the Uno with 6 leds and it looked great. I was using Maxuino to achieve it, and noticed that the Max Patch ends in a Java code object, before going to the serial port. I'm having trouble on the lightuino because the "start here" sketch is just beyond my skill level. I'm not sure what to use and what to take out. I keep experimenting with no luck. I have read many tuts on the arduino site, and have been reading up on the Arduino coding language, and have stared at all the example sketches, so I understand the basics. I am an audio engineer and sound designer who is just learning basic coding for my LED art project, so any help would be very appreciated! 

Thanks!

Justin

Andrew Stone

unread,
Jun 14, 2012, 1:06:12 AM6/14/12
to toasted-circu...@googlegroups.com
The begin_here sketch is a lot fancier then you need to do what you want.  I'll send you something tomorrow; its quite late here.  But I am not sure of integration with max msp.  Can you post your 6 led sketch so I can see what the serial comm looks like?

Cheers!
Andrew

Justin L

unread,
Jun 14, 2012, 10:02:28 AM6/14/12
to toasted-circu...@googlegroups.com
Thanks Andrew,

I used the standard Firmata in conjunction with Maxuino on the UNO, utilizing the 6 PWM pins on that board.

Best,
Justin


On Wednesday, June 13, 2012 10:06:12 PM UTC-7, Andrew Stone wrote:
The begin_here sketch is a lot fancier then you need to do what you want.  I'll send you something tomorrow; its quite late here.  But I am not sure of integration with max msp.  Can you post your 6 led sketch so I can see what the serial comm looks like?

Cheers!
Andrew


Justin L

unread,
Jun 14, 2012, 9:43:36 PM6/14/12
to toasted-circu...@googlegroups.com
Hi Andrew,
First off, thanks for dealing with a noob- I know it can be frustrating. To answer your question in your email; of corse I don't want you to do that! I'm not interested in using Maxuino or the Standard Firmata-sorry for the confusion. I was just trying to learn from it like I was trying to learn from your help sketch. I want to set up the lightuino to have all the pins PWM and give a selected channel a 0-255 value through the serial port. I got the max side covered. I'm just trying to learn how to write a sketch to set the pinmodes and to initiate the serial port to listen for commands, and give those commands in the right format.  I don't have a sketch to show you because I was trying to take pieces of your help sketch and make it work, but every time I altered it in any way, it would not work. I'm just looking for clues, or a point in the right direction. I don't expect a sketch to be written for me. Thank you so much for your time and patience. I'll post this to the forum tonight-I'm at work now. 

Cheers Andrew,
Justin

Andrew Stone

unread,
Jun 16, 2012, 9:25:59 PM6/16/12
to toasted-circu...@googlegroups.com
Hi Justin,

You just need to remove a lot of stuff since the example sketch runs through everything.  Try something like this:


#include <lightuino5.h>

// Create the basic Lightuino 70 LED sink controller (the pins in the 2 40-pin IDE connectors)
LightuinoSink sinks;  
// Create the Lightuino 16 channel source driver controller (the 16 pin connector)
LightuinoSourceDriver sources;

// This object PWMs the Lightuino sinks allowing individual LED brightness control, and provides array-based access to the Leds
FlickerBrightness pwm(sinks);

void setup(void)
{
}

void loop()
{
  pwm.StartAutoLoop(4096);
 
  pwm.brightness[0] = Lightuino_MAX_BRIGHTNESS-1;
  delay(30);
  for (int i=1;i<70;i++)  { pwm.brightness[i] = Lightuino_MAX_BRIGHTNESS-1; pwm.brightness[i-1] = 0; delay(30); }
  pwm.brightness[69] = 0;
}


I've seen one person who had an issue with StartAutoLoop and Max Msp.  "StartAutoLoop" uses a timer interrupt to periodically call the "pwm.loop()" function.  So if you have wierd serial issues, try to comment out "StartAutoLoop" and replace the delay function calls with something like "for (int j=0;j<3000;j++) pwm.loop();"

Cheers!
Andrew

Justin L

unread,
Jun 18, 2012, 11:25:59 PM6/18/12
to toasted-circu...@googlegroups.com
Thanks Andrew!

That was a tremendous help! I was able to run some sketches and gain control of the sink channels successfully, thank you. I did run into a catch- I'm having trouble getting serial.println to work when I send data from the serial monitor window. I'm just trying to get a simple call and response to work. Here's my sketch (it worked when I loaded it on the Uno):

#include <lightuino5.h>
// If you have a shield, include L4 instead
//#include <lightuino4.h>

// These "print" wrappers just output to BOTH USB and UART serial ports (code at the bottom)
void println(char*s);
void print(char*s);
void print(int i,char format=DEC);
void println(int i,char format=DEC);
int serialData = 0;

// Standard Arduino "setup" function
void setup(void)
  {
  // Start up the serial port.  This is not required for the lightuino, I'm just doing it so I can print stuff.
  Serial.begin(9600);
     Usb.begin();
}
  
  void loop()
   {
  if (Serial.available() > 0) // I am attempting to type an int from the serial monitor and get a return print to verify a working serial connection
  {
    serialData = Serial.read();
    Serial.print(serialData);
    }
     }
  
  void println(char*s)
{
  Serial.println(s);
#ifdef Lightuino_USB
  Usb.println(s);
#endif
}

void print(char*s)
{
  Serial.print(s);
#ifdef Lightuino_USB
  Usb.print(s);
#endif
}
void print(int i,char format)
{
  Serial.print(i,format);
#ifdef Lightuino_USB
  Usb.print(i,format);
#endif
}

void println(int i,char format)
{
  Serial.println(i,format);
#ifdef Lightuino_USB
  Usb.println(i,format);
#endif
}



........any ideas why this might work on the Uno but not the Lighuino?
Thank you,

JUstin

On Wednesday, June 13, 2012 9:46:52 PM UTC-7, Justin L wrote:

Andrew Stone

unread,
Jun 19, 2012, 9:11:57 AM6/19/12
to toasted-circu...@googlegroups.com
Hi Justin,

The Lightuino's USB "serial" is not the same as the Uno's "Serial".  The Lightuino uses SPI to talk to a USB->serial chip... this means that you need to use the "Usb.XXXX" functions instead of the "Serial.XXXX" functions, if you want to go out the USB port on the Lightuino.  It also means that you can simultaneously use the UART serial port to talk to something else.  You can get to the actual UART serial port using D0 and D1; just like on the Uno these are the serial TX and RX lines.  Also if you look below the digital header you will see an unpopulated 6 pin header labelled FTDI.  If you want to use a FTDI cable you can populate this header.

Cheers!
Andrew

Justin L

unread,
Jun 19, 2012, 10:59:18 AM6/19/12
to toasted-circu...@googlegroups.com
Thanks Andrew!

That did the trick. Thanks for helping me get started, I'm very excited to make this project!

Cheers,
Justin

Nicolas Marechal

unread,
Jun 17, 2012, 4:55:23 AM6/17/12
to toasted-circu...@googlegroups.com
HI Justin, Hi Andrew,

Here is what we used for a Max project when with a Lightuino 3. Our Lightuino 5 is on its way at the moment so I haven't had the time to try it with the new version. Maybe Andrew can help removing the fat from it ?  

We were using the messenger library : http://www.arduino.cc/playground/Code/Messenger. You'd better use the Arduino 0022 IDE with this library at the moment. 

The patch we used is here : https://github.com/imi/IMI-Max-patches-for-Max6/tree/master/Sensor_Interfaces/7.%20Lightuino. It is a 6x6 matrix using matrixset (so you can record multiple matrix and create an animation) because that was the project we had but you might be able to make it bigger !

I hope you can work it out !

best,
Nicolas

// Max4lightuino - install this on the lightuino and use the messenger libary to read information from Max/MSP

#include <Messenger.h>
#include <lightuino.h>
#include "avr/pgmspace.h"

int myClockPin =     6; //6;                // Arduino pin that goes to the clock on all M5451 chips
int mySerDataPin =   5; //4; // 7; //9;     // Arduino pin that goes to data on one M5451 chip
int mySerDataPin2 =  7; //7; //8; //10;     // Arduino pin that goes to data on another M5451 chip (if you don't have 2, set this to an unused digital pin)
int myBrightnessPin = 10;                   // What Arduino pin goes to the brightness ping on the M5451s
int ledPin = 13;                            // The normal arduino example LED

unsigned long previousMillis = 0;
unsigned long interval = 20;

Lightuino board(myClockPin,mySerDataPin,mySerDataPin2, myBrightnessPin);   
FlickerBrightness leds(board);


Messenger message = Messenger(); 

void messageCompleted() {
int pin = 0;

while ( message.available() ) {
int pin = message.readInt();
int state = message.readInt();
leds.brightness[pin] = state;
}
}

void setup() {
Serial.begin(115200); 
delay(100);
board.flags = Lightuino_FASTSET;
message.attach(messageCompleted);
leds.StartAutoLoop();   
}



void loop() {

while ( Serial.available( ) ) message.process(Serial.read( ) );

if ( millis() - previousMillis > interval ) {
previousMillis = millis();
for ( byte i = 0; i < 6; i++) {
Serial.print( analogRead(i) );
Serial.print(' ');
}
Serial.println();
}

}

Justin L

unread,
Jun 26, 2012, 1:07:00 PM6/26/12
to toasted-circu...@googlegroups.com
Thank You Nicolas,

Hi Andrew,

Heres where I'm at. I've read through Beginning Arduino and the Arduino Cookbook, and have read pretty much every post (it seems) on Arduino and serial comm. My angle of attack on this project is to send char strings and pars them, in order to send multiple variables to the lightuino at once (obviously its still serial). Heres my patch that works great on the uno (using Serial.xxxx class), but not on the lightuino. When i switch the Serial.xxxx class to Usb.xxxx class for the lightuino it will only print the first character of the string then stops working at that point. Is there something different about the Usb.xxxx and using SPI that I need to account for? I'm not going to be controlling one rgb led like in this example, but the concept of parsing strings is what I'm after because I can translate that into my project. Thanks so much for your help! Heres the code- Cheers! Justin


#include <lightuino5.h>
LightuinoSink sinks;   
LightuinoSourceDriver sources;
FlickerBrightness pwm(sinks);




// Project 10 - Serial controlled mood lamp///// This is the project out of Beginning Arduino that Im studying
char buffer[18];
char val[0];
int red, green, blue; /////// these parts of the code aren't involved with what I'm doing
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
{pwm.StartAutoLoop(4096);}
  
  Serial.begin(9600); ///or Usb.begin(); for the lightuino

}
void loop()
{
if (Serial.available() > 0) {  ///on lightuino Usb.available() etc. throughout the code
        int index=0;
        delay(100); // let the buffer fill up
        int numChar = Serial.available();
        if (numChar>15) {
                numChar=15;
         
       }
        while (numChar--) {
                buffer[index++] = Serial.read();
        }
        splitString(buffer);
}
void splitString(char* data) {
        Serial.print("Data entered: ");
        Serial.println(data);
        char* parameter;
        parameter = strtok (data, " ,");
        while (parameter != NULL) {
          

        setLED(parameter);
        delay(30);
        parameter = strtok (NULL, " ,");
        }
 // Clear the text and serial buffers
for (int x=0; x<16; x++) {
        buffer[x]='\0';
 }
Serial.flush();
}
void setLED(char* data) {
        if ((data[0] == 'r') || (data[0] == 'R')) {
                int Ans = strtol(data+1, NULL, 10);
                
                Ans = constrain(Ans,0,255);
               
                 analogWrite(GreenPin, Ans); ////on the lightuino I'd use something like  pwm.brightness[variable 1 from string] = (variable 2 from string)
                Serial.print("Red is set to: ");
                Serial.println(Ans);
        }
        if ((data[0] == 'g') || (data[0] == 'G')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                analogWrite(GreenPin, Ans);
                Serial.print("Green is set to: ");
                Serial.println(Ans);
        }
        if ((data[0] == 'b') || (data[0] == 'B')) {
          int Ans = strtol(data+1, NULL, 10);
          Ans = constrain(Ans,0,255);
          analogWrite(BluePin, Ans);
          Serial.print("Blue is set to: ");
          Serial.println(Ans);
Reply all
Reply to author
Forward
0 new messages