Description
For this week's lab we used photocells and FSRs to initially control our LEDs. For my assignment I customized the Reflection sketch of Simon Greenwold. Instead of changing the reflection by moving the mouse, I controlled the "brightness" of the sphere using analog sensors: the photocell and the force sensitive resistor.
Here's the youtube link to my code:
https://www.youtube.com/watch?v=eK6zei_hWQ4
Components
1 Arduino Uno
1 Breadboard
1 force sensitive resistor/ photocells
1 10k resistor
1 220 Ohm resistor
cables
Code
Arduino
/*
* one sensor (photocell/force sensitive resistor) pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
int sensor_val = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
sensor_val = analogRead(sensorPin); // read the value from the sensor, between 0 - 1024
Serial.println(sensor_val);
analogWrite(ledPin, sensor_val/4); // analogWrite can be between 0-255
}
Processing
/*
I customized the Reflection sketch of Simon Greenwold. Instead of changing the reflection by moving the mouse,
I controlled the "brightness" of the sphere using analog sensors: the photocell and the force sensitive resistor.
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodemfa131"; // or "COM5"
Serial port;
void setup() {
size(640, 360, P3D);
noStroke();
colorMode(RGB, 1);
fill(0.4);
import processing.serial.*;
port = new Serial(this, portname, 9600);
// don't generate a serialEvent() unless you get a newline character:
port.bufferUntil('\n');
}
void draw() {
background(0);
translate(width / 2, height / 2);
// Set the specular color of lights that follow
lightSpecular(1, 1, 1);
directionalLight(0.8, 0.8, 0.8, 0, 0, -1);
sphere(120);
}
void serialEvent (Serial port) {
// get the ASCII string:
String inString = port.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
println (inByte);
float s = inByte/ float (height);
specular(s, s, s);
}
}
- Login to post comments