Lab 4: Happy Face

dheinzerling's picture

 

Description

  • Use the Arduino to control a Processing sketch using a photocell, an FSR, and a potentiometer. A happy face is drawn in a Processing window--if the potentiometer is in the middle range, the happy face is green and happy, if it goes to the low range, the face fades to blue and becomes unhappy, if goes to a high range, the face fades to red and becomes unhappy. If the photocell is covered, the eyes close. If the FSR is pressed lightly, the mouth opens and if it is pressed hard, a frowny face comes up.
  •  
  • For the physical dispersion of force, I tried using a cushion on a seat. This would be useful for a project in which you wanted to use the FSR as an occupancy sensor. It could also be applied to a program that would record how long a person has been sitting in a chair (health applications).

Components Used

  • Resistors
  • Potentiometer
  • FSR
  • Photocell

Arduino Code

The Arduino board was loaded with the Standard FIRMATA that is included with the Arduino software. This allows the board to be referenced with Arduino commands directly from Processing.

 

Processing Code

 

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
 
String portname = "COM18"; // or "COM5"
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
PFont font;
 
void setup() {
  size(600,400);
  frameRate(10);
  arduino = new Arduino(this, portname, 57600); 
  font = loadFont("Arial-Black-14.vlw");
  }
int posx = 300;
int posy = 200;
int wid =300;
int ht = 300;
int redVal = 0;
int grnVal = 0;
int bluVal = 0;
 
void draw() {
  background(196,196,196);
  smiley(posx,posy,wid,ht);
  
  }
  
void smiley(int x,int y,int w,int h) {
   int fsr = arduino.analogRead(0);
   int potVal = arduino.analogRead(1);
   int photo = arduino.analogRead(2);
     if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
      {                  
        potVal = potVal / 4; // Normalize to 0-255
        redVal = 256 - potVal;  // Red from full to off
        grnVal = potVal;        // Green from off to full
        bluVal = 1;             // Blue off
      }
      else // Middle third of potentiometer's range (341-681)
      {
        potVal = potVal / 4; // Normalize to 0-255
        redVal = 1;            // Red off
        grnVal = 256 - potVal; // Green from full to off
        bluVal = potVal;       // Blue from off to full
      }
   println(potVal);
   println(fsr);
   println(photo);
   fill(redVal,grnVal,bluVal);
   ellipse(x,y,w,h);
   if ( (fsr < 100) && ((potVal < 110) && (potVal > 80)) ) 
    {
   arc(x,y,w/2,h/2,.25*PI,PI*.75);
   }
   else if ( ((fsr < 500) && (fsr > 100)) || (((potVal < 81) && (potVal > 30)) || ((potVal > 111) && (potVal < 200))) ) {
     fill(0,0,0);
     ellipse(x,y+40,w/3,h/3);
   }
   else {
     arc(x,y+100,w/2,h/2,PI*1.25,PI*1.75);
   }
   if (photo < 550){
   fill(0,0,0);
   ellipse(x-w*.17,y-h*.15,w/10,h/10);
   ellipse(x+w*.17,y-h*.15,w/10,h/10);
   }
   else {
   fill(255,255,255);
   ellipse(x-w*.17,y-h*.15,w/10,h/10);
   ellipse(x+w*.17,y-h*.15,w/10,h/10);
   }
   if ( ((potVal < 110) && (potVal > 80))){
       textFont(font);
   fill(0);
   text("Temperature is comfortable",x-75,y+175);
   }
   else if (((potVal < 81) && (potVal > 30)) || ((potVal > 111) && (potVal < 200))) {
       textFont(font);
   fill(0);
   text("Temperature is uncomfortable",x-75,y+175);
   }
   else {
       textFont(font);
   fill(0);
   text("Temperature is horrific!",x-75,y+175);
   }
   if ((fsr < 500) && (fsr > 100)) {
       textFont(font);
   fill(0);
   text("Did you just hit me?",x-75,y-175);
   }
   else if (fsr > 500) {
       textFont(font);
   fill(0);
   text("Ouch!!",x-75,y-175);
   }
   else {
       textFont(font);
   fill(0);
   text("Howdy partner!",x-75,y-175);
   }
   
   }
     

Video

0
Your rating: None