P-Comp: Sine Wave of Doom AKA the Poser Theremin

In this week’s lab, we learned about Serial Duplex and continued to learn more about how to get Arduino and Processing to talk to each other.

In my spin on the lab, I hooked up 2 potentiometers through the Arduino to control the frequency and panning of a sine wave in Processing that is manifested on screen and as audio.  I don’t know why I chose red and blue for the sine waves.  Maybe I’ve been watching too much TV coverage of the presidential campaign.  In any case, the effect is that of a sine wave of doom or a poser theremin.

To get Processing to play sound, I used the Minim library.  I basically poached some example code from the Minim site and tweaked it so the sound responds to analog ins from the Arduino instead of mouseX and Y in the original sketch.

IMG_4826

I hope to use more exciting sensors later on to make it more expressive and musical when I get back from fall break next week, but it took me awhile to tweak the software side of things and the computer store was already closed by the time I got around to this. Here it is in action:

My digital camera didn’t pick up the audio very well, so here is an MP3 of some “music” I made.  And here is another attempt.

And finally, here is the Processing code:

import processing.serial.*;     // import the Processing serial library
import ddf.minim.*;
import ddf.minim.signals.*;

Serial myPort;                  // The serial port
AudioOutput out;
SineWave sine;
int sensors[] = new int[2];

int passX;
int passY;

void setup(){
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(‘\n’);
size(512, 200);
// always start Minim before you do anything with it
Minim.start(this);
// get a line out from Minim, default sample rate is 44100, bit depth is 16
out = Minim.getLineOut(Minim.STEREO, 512);
// create a sine wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate 44100 to match the line out
sine = new SineWave(440, 0.5, 44100);
// set the portamento speed on the oscillator to 200 milliseconds
sine.portamento(200);
// add the oscillator to the line out
out.addSignal(sine);
passX = 0;
passY = 0;
}

void draw()
{
background(0,50);
stroke(255);
// draw the waveforms
for(int i = 0; i < out.left.size()-1; i++)
{
stroke(#FF0000); //left is red
line(i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50);
stroke(#0023FC); //right is blue
line(i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50);
}
float freq = map(passY, 0, 1023, 1500, 60);
sine.setFreq(freq);
// pan always changes smoothly to avoid crackles getting into the signal
// note that we could call setPan on out, instead of on sine
// this would sound the same, but the waveforms in out would not reflect the panning
float pan = map(passX, 0, 1023, -1, 1);
sine.setPan(pan);
}

void stop()
{
out.close();
super.stop();
}

void serialEvent(Serial myPort)
{
// read the serial buffer:
String myString = myPort.readStringUntil(‘\n’);
myString = trim(myString);
int sensors[] = int(split(myString, ‘,’));
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++)
{
// print(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “\t”);
}
passX = sensors[0];
passY = sensors[1];
//println();
//print(“PassX= ” + passX + “, PassY= ” + passY);
//println();

}

P-Comp: Serial Output

Serial Output Lab

I worked on this week’s lab with Meredith.  First we set everything up as specified in the lab.  No real problems, pretty straight forward as the video below shows.

Then we wanted to visualize the signal from the serial in a more personal, creative way, so I adapted the big orange circle and blue background from an old ICM project and tried to link the pot reading to the diameter of the circle.  Something stopped working though.  We started double checking wires and scanning every line of the code, but we still could not get the Arduino to talk to Processing.  Then we had a “duh” moment nearly an hour later and just ran the Arduino setup program again.  Then it worked.  Still don’t know why Arduino “forgot” how to output to Processing, even though we didn’t do anything.

The circle diameter did move along with the pot, but there was this nasty flicker that really bothered me, which made me not want to mess with the sketch anymore.  In any case, I think we got the hang of Serial Output.

Here is the code we used:

import processing.serial.*;

Serial myPort; // The serial port
float bigOrangeCircleDiameter = 1; //the big orange circle starts out really small
int bigOrangeCircleGrowthRate = 5;

void setup ()
{
size(800, 800); // window size
myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
}

void serialEvent(Serial myport)
{
rectMode(CORNER);
noStroke();

// get the byte:
int inByte = myPort.read();
// print it:
println(inByte);

//Draw the big orange circle that grows and shrinks
fill(247,101,42); //Orange fill
bigOrangeCircleDiameter = map (inByte, 0, 255, 0,800);
ellipse(width/2,height/2,bigOrangeCircleDiameter,bigOrangeCircleDiameter);
background(68,66,211,20); //turn the background blue
}

Physical Computing Week 4: Analog Output & Servo Motor

Week 4 of Intro to Physical Computing @ ITP involved learning about analog output on the Arduino microprocessor and getting familiar with servo motors.


Physical Computing Week 4 – Evil Servobot from lee-sean on Vimeo.

I decided to take a riff on the lab assignment and attempt to create pure evil with limited resources (and skills for that matter).  I hooked up the servo to a photocell via the Arduino, which controls the movement of the servo based on how much light the photocell detects.  Then I added an LED that dims/brightens, also following the analog reading on the photocell.  And for the final step (and the one the really unleashed pure evil), I added a small speaker that modulates its audio output, also according to the photocell reading.  With all the wires going into the LED and servo acting as anchors, the Evil Servobot is able to dance and shake its demonic robot hips.

Here is the code:

int servoPin = 2;     // Control pin for servo motor
int buzzerPin = 5;
int ledPin = 11;  // Control pin for buzzer
int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo
int brightness = 0;        //pitch of the

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor’s on

void setup() {
pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
pinMode(buzzerPin, OUTPUT); //set buzzer pin as output
pinMode(ledPin, OUTPUT);  //set LED as output
pulse = minPulse;           // Set the motor position value to the minimum
Serial.begin(9600);
}

void loop() {

analogValue = analogRead(analogPin);      // read the analog input
brightness = map(analogValue,0,800,0,255);
pulse = map(analogValue,0,800,minPulse,maxPulse);    // convert the analog value
// to a range between minPulse
// and maxPulse.
// pulse the servo again if rhe refresh time (20 ms) have passed:
Serial.println(analogValue);
if (millis() – lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH);   // Turn the motor on
delayMicroseconds(pulse);       // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW);    // Turn the motor off
analogWrite(ledPin,brightness);
analogWrite(buzzerPin,analogValue*10);
lastPulse = millis();           // save the time of the last pulse
}
}

Physical Computing: Week 2 Analog In

I worked with my classmate Meredith for the Analog In lab in week 2 of Intro to Physical Computing @ ITP.

First, we reconstructed the potentiometer and LED breadboard & Arduino setup according to the lab documentation.  After getting familiar with the hardware last week, this week was much easier, and we were able to get the everything to work right very quickly.  The LED gets brighter or dimmer depending on the position of the potentiometer.

Then we decided to do a riff on the theme.  We replaced the pot with a photocell, which required an extra resistor to work correctly, since the photocell only has 2 prongs, while the pot has 3 connections.  We then tweaked the Arduino program by adding an if-else statement to create a kind of nightlight.  When there is a light shining on the photocell, the LED turns off.  When the lamp is turned off, the LED turns on.  The aluminum foil around the red LED is just for aesthetic effect.

Here is the code we used:

int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
potValue = analogRead(potPin); // read the pot value
Serial.println(potValue); // print the pot value back to the debugger pane
if(potValue>70)
{
analogWrite(led, potValue); // PWM the LED with the pot value
}
else
{
analogWrite(led, 0); // this turns the LED off
}
//delay(10); // wait 10 milliseconds before the next loop
}