Description:
A simple interactive element, the ouput is a smiley face if the user touches the diffuser, if the user presses hard enough the eyes of the widen with happiness!
If the user lets go, the eyes shrink and the face transforms into a sad face
Materials used:
1 FSR
1 LED
2 Resistors
1 Diffuser (Sheet of thermocol (Polysterene)
1 Arduino Uno
Code Used:
Arduino Code:
/*
* I262 - Assignment 4
* Date: 10/8/2013
* Author: Suhaib Syed
* Input: 1 FSR
* Output: Screen Output of smiley/sad face
* Function: Display happy/sad face depending on touch
*/
int sensorPin = 0; // select the input pin for the sensor
int ledPin = 11; // select the output pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(sensorPin); // read the value from the sensor, 0-1023
analogWrite(ledPin, val/4); // analogWrite (dimming the LED) can be between 0-255
Serial.println(val/4); // writing the value to the PC via serial connection
delay(50); // rest a little...
}
Processing Code:
/*
* I262 - Assignment 4
* Date: 10/8/2013
* Author: Suhaib Syed
* Input: 1 FSR
* Output: Screen Output of smiley/sad face
* Function: Display happy/sad face depending on touch
*/
import processing.serial.*;
String portname = "COM3";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int zero;
int nzero;
void setup() {
size(600,600);
frameRate(60);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
else {
int x = 150;
int y = 150;
drawball(x,y, 50);
}
}
// draw balls
void drawball(int x, int y, int r) {
for (int i = 0; i <= 100; i++){
//Random Color integer generation
int red = int (random (0,255));
int green = int (random (0,255));
int blue = int (random (0,255));
//Filling the colors
fill(r+200, r , blue);
//float variable for local calculation
float x1 = x;
float y1 = y;
//Equation for the parabola
y1 = ((x1-300)*(x1-300))/400;
//Drawing the sad face if sensor is not touched for some time
if (r == 0){
zero = zero + 1;
if (zero == 149 ){
background(40,40,40); // erase screen
}
if (zero >= 50){
y1 = y1 + 400;
nzero = 0;
}
}
//Drawing sad face if the sensor is touched for some time
if (r >= 10){
nzero = nzero + 1;
if (nzero == 149 ){
background(40,40,40); // erase screen
}
if (nzero >= 50 ){
y1 = (-1 * y1) + 400;
zero = 0;
}
}
//Boundary conditions for the smiley/sad face
if (x1 >= width - 100){
x1 = width - 100;
}
if (x1 <= 100) {
x1 = 100;}
if (y1 <= 300) {
y1 = 300;
}
if (y1 >= 500) {
y1 = 500;}
//Drawing the eyes
if (r >= 100) {
//Eyes widen if pressure is more
ellipse (200, 200, r/2, r*0.7);
ellipse (400, 200, r/2, r*0.7);
}
else{
ellipse (200, 200, 30, 50);
ellipse (400, 200, 30, 50);
}
ellipse(x1,y1,40,40);
// }
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
println("val="+val);
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y,val);
buf = "";
}
}
- Login to post comments