This week I’m using analog input to the Arduino (heat and light sensors) to manipulate an image in Processing. I started off by prgramming a very basic ellipse in Processing. My goal is to make the color of the ellipse warm as the thermistor registers warmer temperatures and to make it brighten as less light is sensed by the photocell. If I have time I’ll add a force sensing resistor to control the size of the ellipse.
I hooked up a photocell and an LED, based on the Analog In Lab. The LED was glowing, but it wasn’t reacting to the changes in light the photocell was sending to the serial port. I hooked up a potentiometer just to trouble shoot, and the LED dimmed and grew lighter appropriately. So, there’s a problem with the numbers coming in via the photocell. The pot is sending values from 0 to 1023, but the photocell is sending values in the range of 20-60. I multiplied the photocell values by 20, which affected the LED well enough for my purposes, since I won’t be using the LED as the final output.
I hooked up the thermistor and switched the photocell values back to being divided by 4 so that its value falls below 255. The values coming through the serial monitor for both the thermistor and the photocell look good, so I’m ready for the next phase of the project: setting up the information to work with Processing…
After a few minor hurdles and some help from Dan Shiffmann I got my code working. The Arduino code looks like this:
/*
Sensor Reader
Language: Wiring/Arduino
Reads two analog inputs
and outputs their values.
Connections:
analog sensors on analog input pins 0 and 1
*/
int analogOne = 0; // analog input– thermistor
int analogTwo = 1; // analog input– photo cell
int analogThree = 2; // analog input– flex
int sensorValue = 0; // reading from the sensor
void setup() {
// configure the serial connection:
Serial.begin(9600);
}
void loop() {
// read the sensor:
sensorValue = analogRead(analogOne);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(“,”);
// read the sensor:
sensorValue = analogRead(analogTwo);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(“,”);
// read the sensor:
sensorValue = analogRead(analogThree);
// print the results:
Serial.println(sensorValue, DEC);
}
And the Processing code looks like this:
import processing.serial.*;
Serial myPort;
Orb myOrb;
int r = 0;
int g = 125;
int b = 0;
int a = 0;
float bgcolor = 0;
void setup() {
size(1300,800);
smooth();
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(‘n’);
myOrb = new Orb (color(r,g,b,a));
}
void draw() {
background(bgcolor);
myOrb.setColor(color(r,g,b,a));
myOrb.display();
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(‘n’);
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ‘,’));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “t”);
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
r = sensors[0];
b = sensors[1];
a = sensors[2];
}
}
}
With the Orb class written separately as this:
class Orb {
color c;
float orbXPos = width/2;
float orbYPos = height/2;
float orbWidth = height;
float orbHeight = height;
Orb(color tempC){
c = tempC;
}
void display() {
noStroke();
fill(c);
ellipse(orbXPos, orbYPos, orbWidth, orbHeight);
}
void setColor(color tempC) {
c= tempC;
}
}