Lab 5: Serial Communication
For this week’s lab we get to make the arduino communicate with another device for the first time! In this case we’re using serial communication to send data from the arduino to a processing sketch where magic ensues. I’ve been working on a processing sketch with some colored gradient objects that I’ve been manipulating along with some audio processing, which I thought would be a good sketch to add some sensor input to. So far I’ve only been able to get it working with a potentiometer, though, I’d like to get a potentiometer and accelerometer controlling different parameters of the objects eventually. I was running into issues getting reliable readings from the potentiometer (it was sending a wild range of numbers when it wasn’t being moved at all), but after a helpful talk with Professor Igoe the answer was rather simple – soldering. Since I hadn’t soldered the pins to the accelerometer there wasn’t consistent contact. So, even though I am still fairly bad at it, I learned a good lesson – solder, solder, solder. In any event here are a couple of videos demonstrating the very basic pot circuit controlling my processing sketch. The first shows the circuit and provides a better idea of the audio processing while the second one is a nice demo from Neil:
Serial Lab – Circuit and Sound from Chris Alden on Vimeo.
Serial Lab – Neil’s Demo from Chris Alden on Vimeo.
Also, for those interested, here’s the processing code:
//Load audio and serial libraries
import ddf.minim.*;
import ddf.minim.effects.*;
import processing.serial.*;
Serial myPort; // The serial port
//Declare audio objects
Minim minim;
AudioPlayer groove;
BandPass bpf;
//Initialize frame objects and frame object variables
Frame[] frames = new Frame[4];
int frameWidth = 333;
int frameHeight = 800;
int frameX = 0;
int frameY = 0;
int colorsRange = 60;
int rLowest = 40;
int gLowest = 140;
int bLowest = 150;
int shiftDirection = 1;
//Declare variable for serial data from arduino
int inByte;
void setup(){
size(1300,800);
noStroke();
noCursor();
//Load music file
minim = new Minim(this);
groove = minim.loadFile("music.mp3", 2048);
// play the file
groove.play();
groove.loop(1);
// make a band pass filter with a center frequency of 440 Hz and a bandwidth of 20 Hz
bpf = new BandPass(440, 60, groove.sampleRate());
groove.addEffect(bpf);
groove.setGain(20);
//Initialize serial communication
myPort = new Serial(this, Serial.list()[0], 9600);
//Initialize frames
for(int i = 0; i < frames.length; i++){
frames[i] = new Frame((int)random(rLowest,rLowest+colorsRange), (int)random(gLowest,gLowest+colorsRange), (int)random(bLowest,bLowest+colorsRange), frameX, frameY, frameWidth, frameHeight);
}
}
void draw(){
background(255);
//Draw frames
int gradeX = frameX;
int xShift = (int)map(inByte, 70, 100, 0, width/4);
for(int i = 0; i < frames.length; i++){
frames[i].selectShift();
frames[i].drawGradient(gradeX);
gradeX = gradeX + xShift;
}
}
void serialEvent (Serial myPort) {
// get the byte:
inByte = myPort.read();
// map the pot values to the filter's center frequency
float passBand = map(inByte, 70, 100, 200, 2000);
bpf.setFreq(passBand);
float bandWidth = 250;
bpf.setBandWidth(bandWidth);
}
void stop()
{
// always close audio I/O classes
groove.close();
// always stop your Minim object
minim.stop();
super.stop();
}
//Frame Object
class Frame {
int r;
int g;
int b;
int frameX;
int frameY;
int frameWidth;
int frameHeight;
int shift = 50;
Frame(int r_, int g_, int b_, int frameX_, int frameY_, int frameWidth_, int frameHeight_){
r = r_;
g = g_;
b = b_;
frameX = frameX_;
frameY = frameY_;
frameWidth = frameWidth_;
frameHeight = frameHeight_;
}
void drawGradient(int newX){
int y = frameY;
int rval = r;
int gval = g;
int bval = b;
while (y < frameHeight + frameY) {
noStroke();
fill(rval,gval,bval);
rect(newX,y,frameWidth, 5);
y = y + 5;
rval = rval + 1;
gval = gval + 1;
bval = bval + 1;
}
}
void selectShift(){
if(mouseX > 0 && mouseX < width/3){
b = b + shiftDirection;
}
else if(mouseX > width/3 && mouseX < 800){
g = g + shiftDirection;
}
else{
r = r + shiftDirection ;
}
if( r > 255 || r < 0 || g > 255 || g < 0 || b > 255 || b < 0){
shiftDirection = -shiftDirection;
}
}
}
No comments yet.