Assignment 4 : FSR and Photocells

Assignment: Sensing PART II: Force Sensitive Resistors and Photocells

Collaborators:

1. Programming : Drawing a Rainbow!

  • Drawing a rainbow by using FSR (color change), two Potentiometers (x-position and radians change)
  • Components Used
    • Arduino
    • 1 FSR
    • 2 Potentiometers
    • Resistors
  • Code
  1. Arduino
/*
* Use one FSR and two Potentiometers to dim three LEDs
*/
int FSR = 0;   // select the input pin for the FSR
int pot1Pin = 2;   // select the input pin for the potentiometer
int pot2Pin = 1;  //// select the input pin for the potentiometer
int fsrVal = 0;   // variable to store the value coming from FSR
int pot1Val = 0;   // variable to store the value coming from pot 1
int pot2Val = 0;  // variable to store the value coming from pot 2
int led1Pin = 9;   // select the pin for the LED 1
int led2Pin = 11; // select the pin for the LED 2
int led3Pin = 10; // select the pin for the LED 3
// Variables for comparing values between loops
int i = 0;            // Loop counter
int wait = (1000);    // Delay between most recent pot/FSR adjustment and output
int checkSum     = 0; // Aggregate input values
int prevCheckSum = 0;
int sens         = 3; // Sensitivity theshold, to prevent small changes in
// pot/FSR values from triggering false reporting
// FLAGS
int PRINT = 0; // Set to 1 to output values
int DEBUG = 0; // Set to 1 to turn on debugging output
void setup() {
pinMode(led1Pin, OUTPUT);  // declare the led1Pin as an OUTPUT
pinMode(led2Pin, OUTPUT);  // declare the led2Pin as an OUTPUT
// pinMode(led3Pin, OUTPUT); // declare the led2Pin as an OUTPUT
Serial.begin(9600);
}
void loop() {
fsrVal = analogRead(FSR)/2;   // read the value from FSR, between 0 - 1024, for dimming
pot1Val = analogRead(pot1Pin); // read the value from pot1, between 0 - 1024, for dimming
pot2Val = analogRead(pot2Pin)/2; // read the value from pot2, between 0 - 1024, for dimming
analogWrite(led1Pin, fsrVal); // dim LED to value from FSR
analogWrite(led2Pin, pot1Val); // dim LED to value from pot1
analogWrite(led3Pin, pot2Val); // dim LED to value from pot2
Serial.print(fsrVal); // print-out each FSR/Pot values with "," and "|" in the end.
Serial.print(",");
Serial.print(pot1Val);
Serial.print(",");
Serial.print(pot2Val);
Serial.print(",|");
if (i % wait == 0)

// If enough time has passed...

{
checkSum = fsrVal+pot1Val+pot2Val;      // ...add up the 3 values.
if ( abs(checkSum - prevCheckSum) > sens )   // If old and new values differ
// above sensitivity threshold
{
if (PRINT)                    // ...and if the PRINT flag is set...
{
Serial.print("A: ");        // ...then print the values.
Serial.print(fsrVal);
Serial.print("\t");
Serial.print("B: ");
Serial.print(pot1Val);
Serial.print("C: ");
Serial.print(pot2Val);
PRINT = 0;
}
}
else
{
PRINT = 1;  // Re-set the flag
}
prevCheckSum = checkSum;  // Update the values
if (DEBUG)   // If we want debugging output as well...
{
Serial.print(checkSum);
Serial.print("<=>");
Serial.print(prevCheckSum);
Serial.print("\tPrint: ");
Serial.println(PRINT);
}
}
}
====================================================================
2. Processing
/*
* Drawing Rainbow!
* (FSR, two Potentiometers)
* ----------------------
*
* Draw Rainbow on the screen,
* color controlled by a FSR,
* x-postion controlled by a Potentiometer,
* radians controlled by a Potentiometer.
* Receives three inputs from Arduino with an ASCII number over the serial port.
*
*
* Created 30 September 2009
* JinYoung, Baik
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A7006Sdv";
Serial port;
int var1=0; // Three inputs from Arduino
int var2=0;
int var3=0;
int x = 0; // Initiate x-postion as "0"
int colorR=255; // Set the first rectangle color White
int colorG=255;
int colorB=255;
void setup() {
size(600,600,P3D); // Size of display is (600, 600) and add "P3D" for rotatingZ effect later
frameRate(60);
smooth();
noStroke();
background(0,0,0); // Background color Black
port = new Serial(this, portname, 9600);
}
void draw() {
}
// called whenever serial data arrives
void serialEvent(Serial p) {
String arduinoString = port.readStringUntil(124); // Read Arduino output until "|" (ASCII number 124)
if(arduinoString != null) {
arduinoString = trim(arduinoString);
int inputs[] = int(split(arduinoString, ',')); // Trim a set of Arduino output and save each to inputs array
var1 = inputs[0]; // inputs[0] to var1
var2 = inputs[1]; // inputs[1] to var2
var3 = inputs[2]; // inputs[2] to var3
println("var1="+var1);
println("var2="+var2);
println("var3="+var3);
x = 10 + var3; // x-position changes according to the value of var3
if ( var1 < 100 && var1 > 1) {
colorR = 255;
colorG = 0;
colorB = 77;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
} // If a user press the FSR, the color of rectangle changes (red)
if (var1 < 200 && var1 > 101) {
colorR = 255;
colorG = 176;
colorB = 77;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
} // If a user press the FSR a bit more strongly, the color of rectangle changes (orange)
if (var1 > 201 && var1 < 300) {
colorR = 255;
colorG = 255;
colorB = 77;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
}  // If a user press the FSR a bit more strongly, the color of rectangle changes (yellow)
if (var1 > 301 && var1 < 400) {
colorR = 63;
colorG = 255;
colorB = 77;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
} // If a user press the FSR a bit more strongly, the color of rectangle changes (green)
if (var1 > 401 && var1 < 450) {
colorR = 77;
colorG = 145;
colorB = 255;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
} // If a user press the FSR a bit more strongly, the color of rectangle changes (blue)
if (var1 > 451 && var1 < 512) {
colorR = 153;
colorG = 22;
colorB = 218;
fill(colorR, colorG, colorB);
noStroke();
rect(x,300,50,50);
} // If a user press the FSR a bit more strongly, the color of rectangle changes (purple)
if (var2 >= 0 && var2 < 361) {
fill(colorR, colorG, colorB);
rotateZ(radians(var2));
rect(x,300,50,50);
var2 = 0;
} // If a user turn the Potentiometer, radians change accordingly.
if (var2 > 362 && var2 < 720) {
fill(colorR, colorG, colorB);
rotateZ(radians(var2-360));
rect(x,300,50,50);
var2 = 0;
} // If a user turn the Potentiometer, radians change accordingly.
if (var2 > 721 && var2 < 1024) {
fill(colorR, colorG, colorB);
rotate(radians(var2-720));
rect(x,300,50,50);
var2 = 0;
} // If a user turn the Potentiometer, radians change accordingly.
}
}
  • Pictures

 

2. Mechanical

  • Idea

: Lens cleaner (Opti-free) is my essential item not only because it keeps my lens clean but also because without this I couldn't wear lenses. However, since this container is not transparent, sometimes I forget to buy it and wouldn't use my lenses the next day. If I could have a small notifier, which is connected to a FSR, is weighing the amount of water in this container, and displaying "Buy Soon!" message, right next to my lens cleaner, it would be really helpful to remind me of buying a new one before I leave and even after.