The first step to the Neopixel Music Visualization recitation was gathering all the supplies. As shown in the picture, I needed my Arduino board, jumpers, power cable, laptop, and LED strip.
I then used the jumper wires to connect the LED strip to the Arduino. I connected them accordingly: 5V to power, ground to ground (etc.)
After connecting the LED strip, I tested to ensure it was running by running the example code for fastLED. It worked, so then I proceeded with the following steps. As shown in the picture below, the light strip functioned adequately.
Afterwards, I worked on lighting up more LEDs on the strip. I did this by using the code we used in class.
The Arduino Code:
/* This is a code example for Arduino, to be used on Recitation 7 |
|
You need to have installed SerialRecord and FastLED libraries. |
|
It requires NeoPixel WS2812 at pin 3 |
|
Interaction Lab |
|
IMA NYU Shanghai |
|
2022 Fall |
|
*/ |
|
#include "SerialRecord.h" |
|
#include <FastLED.h> |
|
#define NUM_LEDS 60 // How many leds in your strip? |
|
#define DATA_PIN 3 // Which pin are you connecting Arduino to Data In? |
|
CRGB leds[NUM_LEDS]; |
|
// Change this number to the number of values you want to receive |
|
SerialRecord reader(4); |
|
void setup() { |
|
Serial.begin(9600); |
|
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Initialize |
|
FastLED.setBrightness(10); // BEWARE: external power for full (255) |
|
//further info at https://learn.adafruit.com/adafruit-neopixel-uberguide/powering-neopixels |
|
} |
|
void loop() { |
|
if (reader.read()) { |
|
int n = reader[0]; |
|
int r = reader[1]; |
|
int g = reader[2]; |
|
int b = reader[3]; |
|
leds[reader[0]] = CRGB(reader[1], reader[2], reader[3]); // Prepare the color information using CRGB( Red, Green, Blue |
|
FastLED.show(); // Pass the information of color to the LED |
|
} |
|
} |
|
The Processing Code:
/* This is a code example for Processing, to be used on Recitation 7 |
|
You need to have installed the SerialRecord library. |
|
Interaction Lab |
|
IMA NYU Shanghai |
|
2022 Fall |
|
*/ |
|
import processing.serial.*; |
|
import osteele.processing.SerialRecord.*; |
|
Serial serialPort; |
|
SerialRecord serialRecord; |
|
int W; //width of the tiles |
|
int NUM = 60; //amount of pixels |
|
int[] r = new int[NUM]; //red of each tile |
|
int[] g = new int[NUM]; //red of each tile |
|
int[] b = new int[NUM]; //red of each tile |
|
void setup() { |
|
size(600, 200); |
|
W = width/NUM; |
|
// You can use this syntax and change COM3 for your serial port |
|
// printArray(Serial.list()); |
|
// serialPort = new Serial(this, "COM3", 9600); |
|
// in MacOS it looks like "/dev/cu.usbmodem1101" |
|
//or you can try to use this instead: |
|
String serialPortName = SerialUtils.findArduinoPort(); |
|
serialPort = new Serial(this, serialPortName, 9600); |
|
serialRecord = new SerialRecord(this, serialPort, 4); |
|
serialRecord.logToCanvas(false); |
|
rectMode(CENTER); |
|
} |
|
void draw() { |
|
background(0); |
|
for (int i=0; i<NUM; i ++) { |
|
fill(r[i], g[i], b[i]); |
|
rect(i * W + W/2, height/2, 10, 10); |
|
} |
|
if (mousePressed == true) { |
|
int n = floor(constrain(mouseX/W , 0, NUM-1)); |
|
r[n] = floor(random(255)); |
|
g[n] = floor(random(255)); |
|
b[n] = floor(random(255)); |
|
serialRecord.values[0] = n; // which pixel we change (0-59) |
|
serialRecord.values[1] = r[n]; // how much red (0-255) |
|
serialRecord.values[2] = g[n]; // how much green (0-255) |
|
serialRecord.values[3] = b[n]; // how much blue (0-255) |
|
serialRecord.send(); // send it! |
|
} |
|
} |
The code connected processing with Arduino so that how the user interacted in processing, the LEDs would react accordingly. It allowed the user to control how many LEDs to turn on and which ones to change colors via the processing canvas. The next step was to get processing to interact visually with music so that we could merge the Arduino and processing code to make the LED react to music. To get the song I wanted, I got the link to the song from YouTube, put it into an mp3 converter, and downloaded the song. I then dragged the file into the processing “Data” file, so it was in the library.
My code:
#include "SerialRecord.h"
#include <FastLED.h>
#define NUM_LEDS 60 // How many leds in your strip?
#define DATA_PIN 3 // Which pin are you connecting Arduino to Data In?
CRGB leds[NUM_LEDS];
// Change this number to the number of values you want to receive
SerialRecord reader(4);
void setup() {
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // Initialize
FastLED.setBrightness(10); // BEWARE: external power for full (255)
//further info at https://learn.adafruit.com/adafruit-neopixel-uberguide/powering-neopixels
}
void loop() {
println(analysis.analyze());
background(125, 255, 125);
noStroke();
fill(255, 0, 150);
// analyze the audio for its volume level
float volume = analysis.analyze();
// volume is a number between 0.0 and 1.0
// map the volume value to a useful scale
float diameter = map(volume, 0, 1, 0, width);
// draw a circle based on the microphone amplitude (volume)
circle(width/2, height/2, diameter);
}
if (reader.read()) {
int n = reader[0];
int r = reader[1];
int g = reader[2];
int b = reader[3];
leds[reader[0]] = CRGB(reader[1], reader[2], reader[3]); // Prepare the color information using CRGB( Red, Green, Blue
FastLED.show(); // Pass the information of color to the LED
}
}
Overall, the project wasn’t terrible. We did a lot of good practice during the lectures, but I got lost during the lectures most of the time. However, I think putting it into practice during the recitation helped a lot. I still have trouble getting processing to communicate properly with Arduino. Merging the code at the end was the most difficult step for me.
Leave a Reply