Wednesday, October 20, 2010

read value from one analog input in Processing

import processing.serial.*;

Serial port;
float val;

void setup() {
size(800, 600);
String arduinoPort = Serial.list() [0];
port = new Serial(this, arduinoPort, 9600);
}

void draw() {
if (port.available() > 0) {
val = port.read();
// val = map(val, 0, 255, 0, height);
}

background(val);
}

read one analog input in Arduino

const int firstPin = A0; // sensor to control value on analog input 0


void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.print(analogRead(firstPin));
}

Wednesday, September 22, 2010

Rough Noise Sketch


float inc = 0.008;
int density = 4;
float znoise = 0.0;

void setup() {
size(720, 480);
noStroke();
}

void draw() {
float xnoise = 0.0;
float ynoise = 0.0;
for (int y = 0; y < width; y += density) {
for (int x = 0; x < height; x += density) {
float n = noise(xnoise, ynoise, znoise) * 200;
//noise(x, y, z);
if (mouseX>360) {
fill(n+mouseY); }
else {
fill(n); }
rect(y, x, density, density);
xnoise += inc;
}
xnoise = 0;
ynoise += inc;
}
znoise +=inc;
}

Sparkfun Electronics

http://www.sparkfun.com

Tuesday, September 21, 2010

Light Into Sound!

Alex and Matthew perform their custom-built Phototheremins...

Monday, September 20, 2010

Arduino Sketch, Phototheremin





//Example 07: Send to the computer the values read from
//analogue input 0
//Make sure you click on "Serial Monitor"
//after you upload

#define SENSOR 0 // select the input pin for the
// sensor resistor
const int numReadings = 10;

//int val = 0; //variable to store the value coming from the sensor
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = SENSOR;


void setup() {

Serial.begin(9600); // open the serial port to send data back to the computer
// at 9600 bits per second
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

}

void loop() {

// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)

//val = analogRead(SENSOR); //read the value from the sensor

Serial.println(average, DEC); //print the value to the serial port

//delay (10); // wait 10 ms between each send
}

Max Patch Part 2, for Phototheremin




{
"boxes" : [ {
"box" : {
"maxclass" : "newobj",
"text" : "select 0 1",
"numinlets" : 1,
"numoutlets" : 3,
"patching_rect" : [ 296.0, 45.0, 62.0, 20.0 ],
"fontname" : "Arial",
"id" : "obj-30",
"outlettype" : [ "bang", "bang", "" ],
"fontsize" : 12.0
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "close",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 278.0, 77.0, 39.0, 18.0 ],
"fontname" : "Arial",
"id" : "obj-25",
"outlettype" : [ "" ],
"fontsize" : 12.0
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "port a",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 339.0, 76.0, 41.0, 18.0 ],
"fontname" : "Arial",
"id" : "obj-26",
"outlettype" : [ "" ],
"fontsize" : 12.0
}

}
, {
"box" : {
"maxclass" : "multislider",
"numinlets" : 1,
"candicane5" : [ 0.584314, 0.827451, 0.431373, 1.0 ],
"numoutlets" : 2,
"candicane4" : [ 0.439216, 0.619608, 0.070588, 1.0 ],
"patching_rect" : [ 378.0, 363.0, 125.0, 91.0 ],
"id" : "obj-1",
"setminmax" : [ 0.0, 1023.0 ],
"peakcolor" : [ 0.498039, 0.498039, 0.498039, 1.0 ],
"outlettype" : [ "", "" ],
"candicane3" : [ 0.290196, 0.411765, 0.713726, 1.0 ],
"candicane8" : [ 0.027451, 0.447059, 0.501961, 1.0 ],
"setstyle" : 3,
"candicane2" : [ 0.145098, 0.203922, 0.356863, 1.0 ],
"candicane7" : [ 0.878431, 0.243137, 0.145098, 1.0 ],
"candicane6" : [ 0.733333, 0.035294, 0.788235, 1.0 ]
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click here to get a list of serial ports",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 327.0, 138.0, 191.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-2",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Here's your frequency",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 193.0, 293.0, 112.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-3",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert ASCII to symbol",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 380.0, 271.0, 215.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-4",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Convert integer to ASCII",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 380.0, 248.0, 215.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-5",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "number",
"numinlets" : 1,
"htextcolor" : [ 0.870588, 0.870588, 0.870588, 1.0 ],
"numoutlets" : 2,
"patching_rect" : [ 312.0, 292.0, 35.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-6",
"outlettype" : [ "int", "bang" ],
"bgcolor" : [ 0.866667, 0.866667, 0.866667, 1.0 ],
"fontsize" : 9.0,
"triscale" : 0.9
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "fromsymbol",
"numinlets" : 1,
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numoutlets" : 1,
"patching_rect" : [ 312.0, 271.0, 62.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-7",
"outlettype" : [ "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "itoa",
"numinlets" : 3,
"color" : [ 1.0, 0.890196, 0.090196, 1.0 ],
"numoutlets" : 1,
"patching_rect" : [ 312.0, 248.0, 40.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-8",
"outlettype" : [ "int" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "zl group 4",
"numinlets" : 2,
"numoutlets" : 2,
"patching_rect" : [ 312.0, 225.0, 55.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-9",
"outlettype" : [ "", "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "select 10 13",
"numinlets" : 1,
"numoutlets" : 3,
"patching_rect" : [ 256.0, 187.0, 67.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-10",
"outlettype" : [ "bang", "bang", "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "toggle",
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 256.0, 11.0, 15.0, 15.0 ],
"id" : "obj-11",
"outlettype" : [ "int" ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "metro 10",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 222.0, 78.0, 52.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-12",
"outlettype" : [ "bang" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "print",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 291.0, 138.0, 32.0, 15.0 ],
"fontname" : "Arial",
"id" : "obj-13",
"outlettype" : [ "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial a 9600",
"numinlets" : 1,
"numoutlets" : 2,
"patching_rect" : [ 256.0, 163.0, 71.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-14",
"outlettype" : [ "int", "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Read serial input buffer every 10 milliseconds",
"linecount" : 2,
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 61.0, 116.0, 189.0, 27.0 ],
"fontname" : "Arial",
"id" : "obj-15",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "If you get newline (ASCII 10), send the list. If you get return (ASCII 13) do nothing. Any other value, add to the list",
"linecount" : 3,
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 329.0, 187.0, 215.0, 38.0 ],
"fontname" : "Arial",
"id" : "obj-16",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to start",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 275.0, 12.0, 100.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-17",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "digital-to-analog converter makes sounds",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 109.0, 312.0, 196.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-18",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to stop frequency generator",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 412.0, 333.0, 215.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-19",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "comment",
"text" : "Click to start frequency generator",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 412.0, 312.0, 215.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-20",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "stop",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 379.0, 333.0, 29.0, 15.0 ],
"fontname" : "Arial",
"id" : "obj-21",
"outlettype" : [ "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "start",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 370.0, 312.0, 33.0, 15.0 ],
"fontname" : "Arial",
"id" : "obj-22",
"outlettype" : [ "" ],
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "dac~",
"numinlets" : 2,
"numoutlets" : 0,
"patching_rect" : [ 312.0, 356.0, 31.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-23",
"fontsize" : 9.0
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "cycle~",
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 312.0, 312.0, 40.0, 17.0 ],
"fontname" : "Arial",
"id" : "obj-24",
"outlettype" : [ "signal" ],
"fontsize" : 9.0
}

}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-26", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 348.5, 128.0, 265.5, 128.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-25", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 287.5, 128.5, 265.5, 128.5 ]
}

}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-30", 0 ],
"hidden" : 0,
"midpoints" : [ 265.0, 35.0, 305.5, 35.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-30", 0 ],
"destination" : [ "obj-25", 0 ],
"hidden" : 0,
"midpoints" : [ 305.5, 70.5, 287.5, 70.5 ]
}

}
, {
"patchline" : {
"source" : [ "obj-30", 1 ],
"destination" : [ "obj-26", 0 ],
"hidden" : 0,
"midpoints" : [ 327.0, 70.0, 348.5, 70.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-11", 0 ],
"destination" : [ "obj-12", 0 ],
"hidden" : 0,
"midpoints" : [ 265.0, 51.5, 231.5, 51.5 ]
}

}
, {
"patchline" : {
"source" : [ "obj-12", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 231.5, 128.5, 265.5, 128.5 ]
}

}
, {
"patchline" : {
"source" : [ "obj-13", 0 ],
"destination" : [ "obj-14", 0 ],
"hidden" : 0,
"midpoints" : [ 300.5, 158.0, 265.5, 158.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-14", 0 ],
"destination" : [ "obj-10", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 265.5, 216.0, 321.5, 216.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-10", 2 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ 313.5, 228.0, 321.5, 228.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-9", 0 ],
"destination" : [ "obj-8", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-8", 0 ],
"destination" : [ "obj-7", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-7", 0 ],
"destination" : [ "obj-6", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-24", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-23", 0 ],
"hidden" : 0,
"midpoints" : [ 388.5, 352.0, 321.5, 352.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-22", 0 ],
"destination" : [ "obj-23", 0 ],
"hidden" : 0,
"midpoints" : [ 379.5, 341.0, 321.5, 341.0 ]
}

}
, {
"patchline" : {
"source" : [ "obj-24", 0 ],
"destination" : [ "obj-23", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-1", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
]
}