Big newbie here, I am trying to use MAX to control a led strip with an Arduino UNO.
I've got as far as being able to change the colours of all the LEDs with an array of 3 integers coming from MAX
#include <FastLED.h>
#define NUM_LEDS 144
#define DATA_PIN 5
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // GRB ordering is assumed
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
}
void loop() {
// Read array of 3 integers from Max MSP via serial communication
if (Serial.available() >= 3) {
byte red = Serial.read();
byte green = Serial.read();
byte blue = Serial.read();
// Set all LEDs to the same color
CRGB color = CRGB(red, green, blue);
fill_solid(leds, NUM_LEDS, color);
FastLED.show(); // Update LED strip
}
}
I would like to be able to send 3 arrays of 144 integers representing the CRGB of each led now but I think I reached my limits
Any help would be super appreciated, and please do bear in mind I'm really not in my element with programming!
This is more a question of getting MAX MSP to send you those arrays first, then you need to be able to deal with then using the Arduino.
You might run into memory problem here. They will not be integers because they only contain two bytes, you will need to cope with long ints with four bytes per number. Even if you sent them as individual bytes like you do now you will have to transfer and store 144 * 3 * 3 = 4329 bytes. That can't be done with a Uno you would have to use a Mega.
Is this a matrix made from your addressable LED strip?
No MAX MSP is a sound generation language program that runs on your computer. You can use it to talk to the Arduino through the serial port.
Seems I interpreted this differently to you, Mike! Sounded to me like 1 array of 144 integers for the red value for each led, then 1 array of 144 for green and so on.
144 arrays of 3 integers would be simpler, if MAX MSP can do that, but if not, it can still be done.
I thought about doing it this way
void loop() {
// Read 3 arrays of 144 integers from Max MSP via serial communication
if (Serial.available() >= 3*144) {
for (byte j=0; j<144; j++) leds[j].red = Serial.read();
for (byte j=0; j<144; j++) leds[j].green = Serial.read();
for (byte j=0; j<144; j++) leds[j].blue = Serial.read();
FastLED.show(); // Update LED strip
}
}
But I don't think the Arduino's serial buffer will hold that much data (3*144 bytes), so something a little smarter will be needed, or change the way MAX MSP send the data.
void loop() {
static int led = 0;
static int channel = 0;
// Read 3 arrays of 144 integers from Max MSP via serial communication
if (Serial.available() >= 0) {
leds[led][channel] = Serial.read();
led++;
if (led>=NUM_LEDS) {
led = 0;
channel++;
if (channel>=3) {
channel = 0;
FastLED.show(); // Update LED strip
}
}
}
}
Another problem, though: what if the arduino gets out-of-sync with MAX MSP? Some kind of start/end marker in the data would fix this. But there is no value that could be used for a marker that could not possibly be valid colour data.
From the arduino website:
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.
Please stop trying to give advice you are way way out of your depth on this topic.
The OP is using an application called MAX MSP on his computer. The communication between his computer and the Arduino is through the USB port.
Therefore in order to use software serial he would have to figure out how to connect some Arduino pins to the USB of his computer. Where as at the moment he simply connects the computer's USB port to the Arduino's USB port for application to Arduino.
When he wants to program the Arduino he simply closes down his application, thus freeing up the port and programs it normally through the IDE.
Thank you for all the help so far, i am still wrapping my head around the arduino part but here is how Max MSP sends the array in its simplest form. Of cours a lot of things can be done to format it how Arduino prefers if this is an issue.
As you already know, I'm not familiar with MAX MSP at all, so the screen photo you posted means very little to me. But if that list of numbers in the black box under the graph is what would be sent for a single channel (e.g. red) then the code I suggested in post #13 might work. I assume it would be followed by a similar list of values for green and finally blue? The code I suggested will wait until 3*144 values are received.
Yeah sorry it was meant to be a narrated video but I could not upload it.
You're correct that would be the "red" channel for each single LED.
Thank you i'll test it as soon as I'm back home!
And to make it clear I am connecting MAX MSP (software on PC) with USB to the Arduino Uno board. From Arduino board 3 pins go to the LED strip: a data pin, a gound and a 5v.
Can you fix the MAX MSP project so that the value 255 is never sent as part of the colour data? If so, you could use that value to synchronise the Arduino with the MAX MSP data. Just send a single 255 value before the red data.
void loop() {
static int led = 0;
static int channel = 0;
// Read 3 arrays of 144 integers from Max MSP via serial communication
if (Serial.available() >= 0) {
byte value = Serial.read();
if (value==255) {
led = 0;
channel = 0;
}
else {
leds[led][channel] = value;
led++;
if (led>=NUM_LEDS) {
led = 0;
channel++;
if (channel>=3) {
channel = 0;
FastLED.show(); // Update LED strip
}
}
}
}
}