DESCRIPTION
In this lab, I replaced a pot from last lab first with a photocell and 10kΩ resistor then with an FSR and 10kΩ resistor to control the blinking rate of an LED based on light intensity and force application respectively. Then, I ran a “Bounce” code in Processing. Lastly, I loaded the provided “LEDfadeFSR” into my Arduino then used the “BouncingBallPaint” code (provided to us) in Processing to control the number and size of circles bouncing across the screen (size was based on the force applied to the FSR).
For the programming part of the homework, I modified an Arduino code I found in the public domain to send 3 resistive sensor input values (from an FSR, photocell, and pot) to the serial port for use in Processing. The code also allows an LED for each sensor to dim based on the value from the corresponding sensor. Then, I modified the Processing code I found in the public domain which corresponded with the Arduino code to have a line move up the window, then once it reaches the top of the window, reset to the bottom of the window and keep moving up with the process repeating indefinitely. My setup used a pot to control the speed of the line moving up the window, an FSR to control the hue of the line, and a photocell to control the background hue of the window. The idea (and some code modifications) for a line moving up/down on the screen was taken from the File -> Examples -> Motion -> Linear sketch provided with the Processing download.
For the mechanical part of the homework, I used an old “Renuzit” container. Since it had a small, circular top similar in size to the FSR, I taped a folded tissue to the top (for better surface contact with the FSR) then inverted the Renuzit container and placed it on my FSR. It seemed to work well and was a lot easier to use when applying force to the FSR compared with no apparatus.
COMPONENTS USED
1- Arduino Uno (and laptop/USB cable)
1- photocell
2- 10 kΩ Resistors
3- 220 Ω Resistors
3- LEDs
1- Breadboard
1-FSR
1-Pot
22 gauge wire
ARDUINO CODE
/*
3 resistive sensor input values (from an FSR, photocell, and pot) are sent to the serial port for use in Processing.
LEDs in the circuit also dim accordingly based on the values from the sensors.
This code was modified from an example code in the public domain:
http://arduino.cc/en/Tutorial/SerialCallResponse
which was:
created 26 Sept. 2005
by Tom Igoe
modified 24 April 2012
by Tom Igoe and Scott Fitzgerald
and modified 5 October 2013
by Christina Yee
*/
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int led1 = 13;
int led2 = 12;
int led3 = 11;
int inByte = 0; // incoming serial byte
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(A1)/4;
delay(10);
secondSensor = analogRead(A2)/4;
delay(10);
thirdSensor = analogRead(A0)/4;
analogWrite(led1,firstSensor);
analogWrite(led2,secondSensor);
analogWrite(led3,thirdSensor);
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(10);
}
}
PROCESSING CODE
/*
A line moves up the window, then once it reaches the top of the window,
it resets to the bottom of the window. A pot controls the speed of the line,
an FSR controls the hue of the line, and a photocell controls the background hue
of the window.
This Processing sketch was modified from the sketch written posted in the public domain to run with the corresponding Arduino example and both codes were taken from:
http://arduino.cc/en/Tutorial/SerialCallResponse
The idea for a line moving up/down on the screen was taken from the File -> Examples -> Motion -> Linear sketch provided with the Processing download.
*/
import processing.serial.*;
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
float a, b, c; // Starting position of line + color
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
size(640, 360);
smooth();
background(40,40,40);
// Set the starting position of the ball (middle of the stage)
a = height/2;
b = 255;
c = 51;
stroke(b);
String portName = "COM6";
myPort = new Serial(this, portName, 9600);
}
void draw() {
//background(51);
line(0,a,width,a);
a = a - 0.5;
if (a<0) {
a = height;
}
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
a = a-serialInArray[0]/10;
b = 1.5*serialInArray[1];
c = serialInArray[2];
stroke(b);
background(c);
// print the values (for debugging purposes only):
println(a + "\t" + b + "\t" + c);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
IMAGES
The attached images show my circuit, mechanical construction apparatus for my FSR, and a screenshot of my Processing program visualization.
- Login to post comments