So currently I'm trying interface three Potentiometers with processing. I'm using a library from Arduino Playground - Processing in processing to talk to firmata firmware. I get readings from all of the Potentiometers in processing, and processing is able to interpret them just fine.
My code:
/* Library found at http://www.arduino.cc/playground/Interfacing/Processing
Reference:
Arduino.list(): returns a list of the available serial devices. If your Arduino board is connected to the computer when you call this function, its device will be in the list.
Arduino(parent, name, rate): create an Arduino object. Parent should be "this" (without the quotes); name is the name of the serial device (i.e. one of the names returned by Arduino.list());
rate is the speed of the connection (115200 for the v2 version of the firmware, 57600 for v1). Note that in the v2 library, the rate parameter is optional.
pinMode(pin, mode): set a digital pin to input or output mode (Arduino.INPUT or Arduino.OUTPUT).
digitalRead(pin): returns the value of a digital pin, either Arduino.LOW or Arduino.HIGH (the pin must be set as an input).
digitalWrite(pin, value): writes Arduino.LOW or Arduino.HIGH to a digital pin.
analogRead(pin): returns the value of an analog input (from 0 to 1023).
analogWrite(pin, value): writes an analog value (PWM wave) to a digital pin that supports it (pins 3, 5, 6, 9, 10, and 11); value should be from 0 (always off) to 255 (always on).
***These functions are in the Processing Arduino Library and communicate (from Processing) with a Arduino, upon which the Firmata sketch has been installed!!!***
--------Combining this and a cool looking thingy ----
* Space Junk
* By Ira Greenberg
* zoom suggestion
* By Danny Greenberg
*
* Rotating cubes in space using
* a custom Cube class. Color controlled
* by light sources.
*/
/*need to import opengl library to use OPENGL
rendering mode for hardware acceleration
includes the arduino library to interface w/ arduino*/
import processing.serial.*;
import cc.arduino.*;
import processing.opengl.*;
Arduino arduino;
//Sets the input pins
int anaPinA = 2;
int anaPinB = 1;
int anaPinC = 0;
//used to get rid of annoying incorrect readings from pins
int lastPinA = 0;
int lastPinB = 0;
int lastPinC = 0;
//cube count-lower/raise to test P3D/OPENGL performance
int limit = 500;
//array for all cubes
Cube[]cubes = new Cube[limit];
void setup(){
//Set up the arduino to get input on the right pin
arduino = new Arduino(this, Arduino.list()[0], 115200);
arduino.pinMode(anaPinA, Arduino.INPUT);
arduino.pinMode(anaPinB, Arduino.INPUT);
arduino.pinMode(anaPinC, Arduino.INPUT);
//try substituting P3D for OPENGL
//argument to test performance
//size(640, 480, OPENGL);
size(1280, 960, OPENGL);
background(0);
//instantiate cubes, passing in random vals for size and postion
for (int i = 0; i< cubes.length; i++){
cubes[i] = new Cube(int(random(-10, 10)), int(random(-10, 10)),
int(random(-10, 10)), int(random(-140, 140)), int(random(-140, 140)),
int(random(-140, 140)));
}
}
void draw(){
background(0, 102, 200);
fill(200);
//set up some different colored lights
pointLight(51, 102, 255, 65, 60, 100);
pointLight(200, 40, 60, -65, -60, -150);
//raise overall light in scene
ambientLight(70, 70, 10);
//Check if the pin A has spiked, if so don't let it.
if (anaPinA >= 941){
anaPinA = lastPinA;
}
/*center geometry in display windwow.
you can change 3rd argument ('0')
to move block group closer(+)/further(-)*/
translate(width/2, height/2, -200+arduino.analogRead(anaPinA));
//Check if the pin B or C have spiked, if so don't let em
if (anaPinB >= 512){
anaPinB = lastPinB;
}
if (anaPinC >= 512){
anaPinC = lastPinC;
}
//rotate around y and x axes
rotateY(radians(arduino.analogRead(anaPinB)));
rotateX(radians(arduino.analogRead(anaPinC)));
//draw cubes
for (int i = 0; i< cubes.length; i++){
cubes[i].drawCube();
}
//sets a good value for the last analog reading
lastPinA = anaPinA;
lastPinB = anaPinB;
lastPinC = anaPinC;
}
//simple Cube class, based on Quads
class Cube {
//properties
int w, h, d;
int shiftX, shiftY, shiftZ;
//constructor
Cube(int w, int h, int d, int shiftX, int shiftY, int shiftZ){
this.w = w;
this.h = h;
this.d = d;
this.shiftX = shiftX;
this.shiftY = shiftY;
this.shiftZ = shiftZ;
}
/*main cube drawing method, which looks
more confusing than it really is. It's
just a bunch of rectangles drawn for
each cube face*/
void drawCube(){
beginShape(QUADS);
//front face
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
//back face
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
//left face
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
//right face
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
//top face
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
//bottom face
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
vertex(w + shiftX, h + shiftY, d + shiftZ);
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
endShape();
//add some rotation to each box for pizazz.
rotateY(radians(1));
rotateX(radians(1));
rotateZ(radians(1));
}
}
Now I think my code is fine, but I put it up just in case. The problem is I get random changes in the values from the Potentiometers to the point the generated objects shake and flicker. Now I'm using this schematic per Potentiometer:
With the variable resistor replaced with a pot set up like a variable resistor (http://sound.westhost.com/pots-f6.gif).
Halp. Why does it fluctuate?