Recitation 7: Neopixel Music Visualization

TASK #1:

I successfully connected the Neopixel to the Arduino and used the library in Processing to test the LED.

 

TASK #2:

 

By downloading the SerialRecord library in Arduino and utilizing relevant codes in both Arduino and Processing, I managed to light up Neopixel using my computer.

 

TASK #3:

I visualized the music Lavender Haze by Taylor Swift (which is my favorite recently:) High recommend it.). 

To be honest, modifying and combining the codes of Processing and Arduino took me a lot of efforts. I set a new parameter call “volume”, a number between 0.0 and 1.0, which is used for detecting the volume changes and sending signals based on amplitude. Besides, I used “frameCount” to control the background color and send the value once every four seconds.

Full codes of Task #3 are attached below:

Processing :

import processing.sound.*;
import processing.serial.*;
import osteele.processing.SerialRecord.*;
float v1 = 0, volume;

SoundFile sample;
Amplitude analysis;
Serial serialPort;
SerialRecord serialRecord;

int NUM = 60;  //amount of pixels
int[] r = new int[NUM]; //red of each tile
int[] g = new int[NUM]; //green of each tile
int[] b = new int[NUM]; //blue of each tile
int red, green, blue;

void setup() {
  size(640, 480);
 
  // load and play a sound file in a loop
  sample = new SoundFile(this, "lavender haze.mp3");
  sample.loop();

  // create the Amplitude analysis object
  analysis = new Amplitude(this);
  // analyze the playing sound file
  analysis.input(sample);
  
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 1);
  //serialRecord.logToCanvas(false);
}

void draw() {
  if (frameCount%4==1) {
          serialRecord.values[0] = floor(map(volume, 0, 1, 0, 60));     // which pixel we change (0-59)
      serialRecord.send();            // send it!
  }
  println(analysis.analyze());
  //background(125, 255, 125);
 if (frameCount < 100) {
   background(155);
 } else if ((frameCount>=150) && frameCount<200) {
   background(255);
 } else {
   background(55);
 }
  noStroke();
  fill(255, 0, 150);

  // analyze the audio for its volume level
  volume = analysis.analyze();
  if (abs(volume - v1) > 0.05) {
    red = floor(random(0, 255));
      //serialRecord.values[0] = floor(map(volume, 0, 1, 0, 60));     // which pixel we change (0-59)
      //serialRecord.send();            // send it!
     v1 = volume; 
  }
  

  // 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);
}

 

Arduino: 

#include <Adafruit_NeoPixel.h>



#include "SerialRecord.h"
#include <FastLED.h>

#define NUM_LEDS 60  // How many leds in your strip?
#define DATA_PIN 9   // 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(1);

int prevolume = 0;
int n;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // Initialize
  FastLED.setBrightness(99);                             // BEWARE: external power for full (255)
                                                        //further info at https://learn.adafruit.com/adafruit-neopixel-uberguide/powering-neopixels
}


void loop() {
  if (millis()%5==1) {
    FastLED.clear(true);
  }
  if (reader.read()) {
    n = reader[0];
    // leds[reader[0]] = CRGB(reader[1], reader[2], reader[3]);  //  Prepare the color information using CRGB( Red, Green, Blue
    for (int i=0; i<=n;i++) {
      leds[i]=CRGB(i, i, i);
    FastLED.show();     
    delay(10);    
    }
    // prevolume = n;
                                        //  Pass the information of color to the LED
  }
  // if (millis() % 100 < 25) {
  // Serial.println("here");
  // FastLED.show();  //  Pass the information of color to the LED
                   //}
}

Leave a Reply

Your email address will not be published. Required fields are marked *