How awake am I?

I was sick for this lab, so I decided to see just how awake I was in bed. I decided to graph how much pressure I was applying to my husband pillow (that’s honestly what it’s called). The more pressure, the more likely I was asleep/resting.


Materials

  • FSR
  • Jumper wires
  • Resistor
  • Breadboard
  • Arduino

Arduino Code:

<span class="coMULTI">The circuit:
 Any analog input sensor is attached to analog in pin 0.

 created 2006
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Graph
 */</span>

<span class="kw1">void</span> <span class="kw3">setup</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="co1">// initialize the serial communication:</span>
  <span class="kw1">Serial</span>.<span class="kw1">begin</span><span class="br0">(</span><span class="nu0">9600</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>

<span class="kw1">void</span> <span class="kw3">loop</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="co1">// send the value of analog input 0:</span>
  <span class="kw1">Serial</span>.<span class="kw1">println</span><span class="br0">(</span><span class="kw1">analogRead</span><span class="br0">(</span>A0<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
  <span class="co1">// wait a bit for the analog-to-digital converter</span>
  <span class="co1">// to stabilize after the last reading:</span>
  <span class="kw1">delay</span><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>

Processing Code:

import processing.serial.*;
 String portname = "/dev/cu.usbmodem1421"; // or "COM5"
 Serial port;
 String buf="";
 int cr = 13; // ASCII return == 13
 int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
 float xPos = 0; // horizontal position of the graph
 float yPos = 0; // vertical position of the graph

void setup() {
 size(800,600); // window size
 // List all the available serial ports
 port = new Serial(this, portname, 9600);
 background(#081640);
 }

void serialEvent (Serial myPort) {
 // get the byte:
 int inByte = myPort.read();
 // print it:
 println(inByte);
 yPos = height - inByte;
 }

void draw () {
 // draw the line in a pretty color:
 stroke(#FFD700);
 line(xPos, height, xPos, yPos);
 // at the edge of the screen, go back to the beginning:
 if (xPos &gt;= width) {
 xPos = 0;
 // clear the screen by resetting the background:
 background(#081640);
 } else {
 // increment the horizontal position for the next reading:
 xPos++;
 }
 }
 

Lab 4 – Orb and Fractal

My lab project this week consists of a squeezable, glowing orb and a fractal generator. The results are a bit kooky. I reused a soft silicon casing we created for a past project and placed the pressure sensor between the soft casing and the hard plastic kernel.

img_20160928_140008

For my visualization, I played around with the Mandelbrot sketch example in Processing, making it into a sequential, frame-by-frame visualization rather than a static image. I accelerated the fractal growth with greater pressure on the orb, but as that was hard to see, I also changed the background color slightly to show when pressure was applied. The next step might be to try out algorithmic visualizations that are not fractals, as later evolution is hard to see as the fractals get smaller.

screen-shot-2016-09-28-at-2-02-39-pm

 

Arduino Code:

/*
* Andrew Chong
* Alternating RGB Fade with Potentiometers
* Modified from Clay Shirky &lt;clay.shirky@nyu.edu&gt; 
*/

// Output
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11

float red_direction = 1; 
float blue_direction = 1; 
float green_direction = 1; 
float pressure; 

// brightness 
float blue_brightness = 0; 
float red_brightness = 0;
float green_brightness = 0; 

// Program variables
float redVal   = 255; // Variables to store the values to send to the pins
float greenVal = 255;   
float blueVal  = 0;

int i = 0;     // Loop counter    
int j = 0; // display counter 
int wait = 500; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);         // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
}

// Main program
void loop()
{
  //blue_brightness = analogRead(A0)*3/(float)1023;
  //red_brightness =  analogRead(A5)*3/(float)1023;
  //green_brightness =  analogRead(A1)*3/(float)1023;
  i += 5;      // Increment counter
  j += 1; 

  delay(wait);
  Serial.println(int(analogRead(A0)/100));
  pressure = analogRead(A0);

  greenVal = (pressure/1000)*255;
  //Serial.println("red:");
  //Serial.println("red:");
  //Serial.println(greenVal);
  analogWrite(greenPin, greenVal); 

/*
if (DEBUG) { // If we want to read the output
    DEBUG += 1;     // Increment the DEBUG counter
    if (DEBUG &gt; 20) // Print every 10 loops
    {
      DEBUG = 1;     // Reset the counter
      Serial.print(i);       // Serial commands in 0004 style
      Serial.print("\t");    // Print a tab
      Serial.print("R:");    // Indicate that output is red value
      Serial.print(redVal);  // Print red value
      Serial.print("\t");    // Print a tab
      Serial.print("G:");    // Repeat for green and blue...
      Serial.print(greenVal);
      Serial.print("\t");    
      Serial.print("B:");    
      Serial.println(blueVal); // println, to end with a carriage return
    }
  }
  */ 

  //delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

Processing code:
/**
 * The Mandelbrot Set
 * by Daniel Shiffman.  
 * 
 * Simple rendering of the Mandelbrot set.
 */

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int serialVal = 0;
int currentVal = 10; 
int r = 106;
int g = 0;
int b = 209;

void setup() {
  size(1200,800);
  frameRate(2);  // Animate slowly
   port = new Serial(this, portname, 9600); 
}

void draw() {
 // Establish a range of values on the complex plane
// A different range will allow us to "zoom" in or out on the fractal

// It all starts with the width, try higher or lower values
float w = 4;
float h = (w * height) / width;

// Start at negative half the width and height
float xmin = -w/2;
float ymin = -h/2;

// Make sure we can write to the pixels[] array.
// Only need to do this once since we don't do any other drawing.
loadPixels();

currentVal = currentVal + 1 + serialVal;
// Maximum number of iterations for each point on the complex plane
int maxiterations = currentVal ;

// x goes from xmin to xmax
float xmax = xmin + w;
// y goes from ymin to ymax
float ymax = ymin + h;

// Calculate amount we increment x,y for each pixel
float dx = (xmax - xmin)/(width);
float dy = (ymax - ymin)/(height);

// Start y
float y = ymin;
for (int j = 0; j &lt; height; j++) {
  // Start x
  float x = xmin;
  for (int i = 0; i &lt; width; i++) {

    // Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
    float a = x;
    float b = y;
    int n = 0;
    while (n &lt; currentVal) { float aa = a * a; float bb = b * b; float twoab = 2.0 * a * b; a = aa - bb + x; b = twoab + y; // Infinty in our finite world is simple, let's just consider it 16 if (dist(aa, bb, 0, 0) &gt; 16) {
        break;  // Bail
      }
      n++;
    }

    // We color each pixel based on how long it takes to get to infinity
    // If we never got there, let's pick the color black
    if (n == maxiterations) {
      pixels[i+j*width] = color(0);
    } else {
      // Gosh, we could make fancy colors here if we wanted
      float norm = map(n, 0, maxiterations, 0, 1);
      /*
      if (r&gt;254) {
      r = 0; 
      }
      if (g&gt;254) {
       g = 0;
      }
      if (b&gt;254) {
        b = 0;
      }
      */
      //r = r + 1;
      //b = b + 1;
      //g = g +1; 
      pixels[i+j*width] = color(0,g,b); 
    }
    x += dx;
  }
  y += dy;
}

updatePixels();

}

// called whenever serial data arrives
void serialEvent(Serial p) {
 int c = port.read();
 if (c != lf &amp;&amp; c != cr) {
 buf += char(c);
 }
 if (c == lf) {
 serialVal = int(buf);

 println("val="+serialVal); 
 buf = ""; 
 }
 if (serialVal&gt;5){
 //r = int(random(255));
 if (g==0) {
   g = 20; 
 }
 else if (g==20){
 g = 0;
 }
 print(g);
 //b = int(random(255)); 
 //print(b);
 }
}

FSR Viz

Description

I attached my FSR to a pair of tongs and through the visualization, you can see how much force you’re using to hold/pick up an object. My visualization makes use of color, size and speed to demonstrate changes in force applied. Squared move diagonally across the screen and as the pressure increases, so does their speed and they grow in size too. At maximum force, the background color  becomes a bright green and is meant to grab the user’s attention or alert her.

Components

Arduino

Bread board

1 10 kΩ resistor

Jumper cables

1 FSR

Tongs

Arduino code

int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;

void setup(void) {
Serial.begin(9600); // We’ll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
}

void loop(void) {
fsrReading = analogRead(fsrAnalogPin);
// Serial.print(“Analog reading = “);
// Serial.println(fsrReading);

// we’ll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);

delay(100);
Serial.println(fsrReading); // for processing
}

Processing code

/* PROCESSING SKETCH
* FSR Visualization – Moving squares
* Reema Naqvi
*/

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = “/dev/cu.usbmodem1411”; // or “COM5″
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;
int rgbValr;
int rgbValg;
int rgbValb;
int moveX = 0;
int moveY = 0;
void setup() {
size(1200,700);
frameRate(20);
smooth();
background(0, 10, 20);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen

rgbValr = 255-serialVal/4;
rgbValg = 255-serialVal/9;
rgbValb = 255-serialVal/4;
background(rgbValr, rgbValg, rgbValb);

fill (255,0,190);
rect(moveX, moveY, serialVal/20 , serialVal/20);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/30;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX+300, moveY, serialVal/15 , serialVal/15);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX+600, moveY+100, serialVal/10 , serialVal/10);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX – 200, moveY+300, serialVal/40 , serialVal/40);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX-100, moveY+400, serialVal/25 , serialVal/25);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX+1150, moveY+400, serialVal/8 , serialVal/8);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX, moveY+200, serialVal/15 , serialVal/15);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

rect(moveX+1000, moveY, serialVal/20 , serialVal/20);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/30;
if (moveY >= 300) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}
rect(moveX+600, moveY+400, serialVal/8 , serialVal/8);
moveX = moveX+serialVal/30;
moveY = moveY+serialVal/40;
if (moveY >= height) {
moveY = 0;
moveX = 0;
}
if (moveX >= width) {
moveX = 0;
moveY = 0;
}

}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“val=”+serialVal);
buf = “”;
}
}

ezgif-com-video-to-gif img_7649

Lab 04

Description

I used Arduino with force sensor. I edited the sample process sketch code to turn on the circle into squares with changing color and changing the corners with round angles via force sensor interaction. With different value detected by the force sensor, the color and the size of the square will change as well as the degree of angles of its corners. Lastly, I  uploaded the code and took pictures of the result.

Components

  • 1 Arduino
  • 3 LED
  • 3 Resistor (220Ω)
  • 1 Breadboard
  • One force sensor

Code

/* PROCESSING SKETCH
 * Arduino Ball Paint
 * (Arduino Ball, modified 2008)
 * ---------------------- 
 *
 * Draw a ball on the screen whose size is
 * determined by serial input from Arduino.
 *
 * Created 21 September 2016
 * Noura Howell
 * Edited by Owen Hsiao on Sep. 23 2016
 */

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM3"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

int n = serialVal + int(random(10));

void setup() {
 size(300,300);
 frameRate(10);
 smooth();
 background(40,40,40);
 noStroke();
 port = new Serial(this, portname, 9600); 
}

void draw() {
 // erase the screen
 background(40, 40, 40);

// draw the ball
 f<strong>ill(255, serialVal, 255/n);</strong>
<strong> //ellipse(150,150,serialVal,serialVal);</strong>
<strong> rect(70, 70, serialVal+30, serialVal +30, serialVal/20);</strong>
<strong>}</strong>

<strong>// called whenever serial data arrives</strong>
<strong>void serialEvent(Serial p) {</strong>
<strong> int c = port.read();</strong>
<strong> if (c != lf &amp;&amp; c != cr) {</strong>
<strong> buf += char(c);</strong>
<strong> }</strong>
<strong> if (c == lf) {</strong>
<strong> serialVal = int(buf);</strong>
<strong> println("val="+serialVal); </strong>
<strong> buf = ""; </strong>
<strong> }</strong>
<strong>}

</strong>

Pressure sensor

My program lights up different LEDs on the Arduino based on the pressure applied to the FSR. On low pressure, it’s green; on medium, blue and on higher pressure, red. It can be used in trash cans to check for the amount of trash inside, or installed under the insoles of shoes to check how relaxed the fit is.


const int ledGreen = 7; // Level Min
const int ledBlue = 4; // Level Medium
const int ledRed = 2; // Level Full
int fsrPin = A0; 
int fsrReading; 
void setup(void) {
   // Debugging information via the Serial monitor
   Serial.begin(9600);
   pinMode(ledRed, OUTPUT);
   pinMode(ledBlue, OUTPUT);
   pinMode(ledGreen, OUTPUT);
}
void loop(void) {
   fsrReading = analogRead(fsrPin); 
   if (fsrReading <=512) {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledBlue, LOW);
   }
   if (fsrReading > 512 && fsrReading <= 600) {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledBlue, LOW);
   }
   if (fsrReading > 600 && fsrReading <= 680) {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledBlue, HIGH);
   }
   if (fsrReading > 680) {
      digitalWrite(ledRed, HIGH);
      digitalWrite(ledGreen, LOW);
      digitalWrite(ledBlue, LOW);
   }
   //Serial.print("Analog reading = ");
   Serial.println(fsrReading); // Raw analog reading
   delay(250); // Slow down the output for easier reading
}

img_20160928_075818

Dots and Dashes with FSR

For this lab, I chose to take a modern approach to Morse Code and where the input signals come from. For my project, the dots come from keyboard inputs and the dashes come from FSR presses and together, you get a visual of the dots and spaces for the dashes. The blue LED also flashes in accordance with the FSR presses.

Components:

  • Arduino Uno
  • Breadboard
  • 1 force sensor
  • 1 220Ω resistors
  • 1 10Ω resistor
  •  wires

 

Arduino code #1:

int photocellPin = 0; // select the input pin for the potentiometer
int fsrPin =11;
int bPin = 11; // select the pin for the LED
 
int fsrVal;
int photocellVal; // variable to store the value coming from the sensor
 
int counter;
 
int THRESHOLD = 500;
 
void setup() {
 photocellVal = 0;
 fsrVal = 0;
 counter = 0; 
 Serial.begin(9600);
}
 
void loop() {
 fsrVal = analogRead(fsrPin); // read the value from the sensor, between 0 - 1024
 
 analogWrite(bPin, fsrVal/4);
 
 photocellVal = analogRead(photocellPin);
 
 if(photocellVal&gt;THRESHOLD)
 {
 analogWrite(bPin, photocellVal/4);
 if(counter%300==0)
 {
// Serial.print(photocellVal);
 Serial.print("1");
 }
 }
 else
 {
 analogWrite(bPin, 0);
 if(counter%300==0)
 {
 Serial.print("0");
 }
 }
 
 counter++;
}
 

Processing code:

adapted from :http://courses.ischool.berkeley.edu/i262/s13/content/stlim/lab-submission-4-photocell-and-fsr?destination=node%2F909
import processing.serial.*;
 
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem1411";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
 
int radius = 50;
int gapX = 1;
int gapY = 3;
int windowX = 1000;
int windowY = 720;
int marginX = 40;
int marginY = 40;
int x = marginX;
int y = marginY;
 
boolean gap = false;
boolean init = true;
 
void setup() {
 size(1200, 720);
 frameRate(10);
 smooth();
 background(255, 200, 200);
 noStroke();
 port = new Serial(this, portname, 9600); 
}
void draw() {
}
 
void keyPressed() {
 if(key == ' ') {
 background(40,40,40); // erase screen
 }
 else {
 int x = int(random(0,width));
 int y = int(random(0,height));
 drawball();
 }
}
 
// draw balls
void drawball() {
 ellipse(x,y,radius,radius);
 x+=3;
// x += 2*radius + gapX;
 
 if(x&gt;windowX-marginX) {
 x = marginX;
 y += 2*radius + gapY;
 }
}
// called whenever serial data arrives
void serialEvent(Serial p) {
 int serialVal = 0;
 int c = port.read();
 // called whenever serial data arrives
 if (c != lf &amp;&amp; c != cr) {
 buf += char(c);
 }
 if (c == lf) {
 serialVal = int(buf);
 println("val="+serialVal); 
 buf = ""; 
 }

if(serialVal==0 &amp;&amp; init) {
 
 }
 else if(serialVal == 0) {
 if(!gap) {
 x += 2*radius + gapX;
 }
 
 gap = true;
 }
 else {
 init = false;
 gap = false;
 drawball();
 x+=radius/5;
 }
}
 

Dots & Dashes

Lab 4 FSR, Bouncy Balls, Music, and Color

Description

Video

I made a bouncy balls animation where the FSR can control the gravity of the bouncy balls. I’ve attached a youtube video. Music is accompanied with the bouncy ball experienced. Also I added features that will let users control the color of the balls through mouse and control the color of backgrounds through keyboards. The user can also control the spring of the balls through up and down keys.

I didn’t focus as much on the mechanical part of the lab because I spent too much time playing with the animation and digging into the tutorial. Right now, I’m just controlling the force through my hands. But I definitely had a lot of fun playing with this lab and am looking forward to apply some mechanical concepts in the future.

Processing Code


import processing.serial.*;
import processing.sound.*;
SoundFile file;

// Change this to the portname your Arduino board
String portname =  "/dev/cu.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int SerialVal = 0;
float r = 0;
float g = 0;
float b = 0;

int numBalls = 40;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];

int barWidth = 5;
int lastBar = -1;

void setup() {
  size(800, 800);
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
  }
  noStroke();
  file = new SoundFile(this, "spaceBeat.wav");
  file.play();
  
  port = new Serial(this, portname, 9600); 
}

void draw() {
  background(r, g ,b);
  for (Ball ball : balls) {
    ball.collide();
    ball.move();
    ball.display();  
  }

  if (keyPressed == true && keyCode == UP) {
      spring += 0.01;
    } else if (keyPressed == true && keyCode == DOWN) {
      spring -= 0.01;
    } else if (keyPressed == true && key == 'r') {
      r += 10;
    } else if (keyPressed == true && key == 'R') {
      r -= 10;
    } else if (keyPressed == true && key == 'g') {
      g += 10;
    } else if (keyPressed == true && key == 'G') {
      g -= 10;
    } else if (keyPressed == true && key == 'b') {
      b += 10;
    } else if (keyPressed == true && key == 'B') {
      b -= 10;
    } else if (keyPressed == true && key == 'S' || key == 's') {
      friction = -0.9;
      spring = 0.05;
      r=0;
      g=0;
      b=0;
    }
    int whichBar = mouseX / barWidth;
    if (whichBar != lastBar) {
      int barX = whichBar * barWidth;
      fill(barX, mouseY, 66);
      rect(barX, 0, barWidth, height);
      lastBar = whichBar;
    }
    
}

class Ball {
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;
 
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  } 
  
  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }   
  }
  
  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction; 
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction; 
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
  }
  
  void display() {
    ellipse(x, y, diameter, diameter);
  }
}

// called whenever serial data arrives
void serialEvent(Serial p) {
 int c = port.read();
 if (c != lf && c != cr) {
   buf += char(c);
 }
 if (c == lf) {
   SerialVal = int(buf);
   println("val="+SerialVal);
   gravity = SerialVal/1000;
   println("gravity="+gravity);
   buf = "";
 }
}

Arduino Code


/* Photocell simple testing sketch. 
 
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground 
Connect LED from pin 11 through a resistor to ground 
For more information see http://learn.adafruit.com/photocells */
 
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
int LEDpin = 11;          // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;        // 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
}
 
void loop(void) {
  photocellReading = analogRead(photocellPin);
  
  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0
  photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 0, 1023, 255, 0);
  analogWrite(LEDpin, LEDbrightness);
  
  Serial.println(photocellReading);
 
  delay(100);
}

Lab 4 FSR and graphics

Description:

I thought there were two parts to the assignment: 1. making a graphic that reacts to FSR

2. making a reactive graphic that coincides w a pressure sensor application.

so for 2.

I thought of making a graphic that can tell you if you are pressing on your cellphone and hopefully warn you before you crack it.

Components:

  • Arduino Uno
  • Breadboard
  • 1 force sensor
  • 1 220Ω resistors
  • 1 10Ω resistor
  •  wires

Part 1 graphic is a sort of abstract pattern of lines and circles. I thought it was interesting that the lines rotate when you apply serialVal to the variables.

— processing code —

String portname = “/dev/cu.usbmodem1421”; // or “COM5″
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
size(640,640);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {
// erase the screen
background(100, 40, 40);

// draw the ball
noFill();
stroke(200);
strokeWeight(.5);
line(150,serialVal,serialVal,100);
line(100,serialVal,serialVal,150);
ellipse(serialVal, serialVal, 50 + serialVal, 50 + serialVal);

strokeWeight(1);
line(200,serialVal,serialVal,75);
line(75,serialVal,serialVal,200);
ellipse(serialVal, serialVal, serialVal, serialVal);

strokeWeight(2);
line(250,serialVal,serialVal,55);
line(55,serialVal,serialVal,250);
ellipse(serialVal, serialVal, serialVal -50, serialVal – 50);

strokeWeight(2.5);
line(300,serialVal,serialVal,35);
line(35,serialVal,serialVal,300);
ellipse(serialVal, serialVal, serialVal -150, serialVal – 150);

strokeWeight(3.5);
line(500,serialVal,serialVal,15);
line(15,serialVal,serialVal,500);

}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“potValue=”+serialVal);
buf = “”;
}
}

+ + + + +

Part 2 graphic I can’t really create an actual connection between the cellphone and the graphic so I made a visual mockup on computer monitor where I overlay the Processor sketch window over a photo of a cellphone.

if you press on the center of the phone, the screen turns from black to red (the universal warning color)  and the text

“—hey— watch it.” comes up

—processing code—

String portname = “/dev/cu.usbmodem1421”; // or “COM5″
Serial port;
String buf=””;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
size(325,560);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}

void draw() {

background(serialVal, serialVal-155, serialVal-155);
stroke(255,0,0);
fill(serialVal, serialVal, serialVal);
textSize(15);
text(“—hey—”, 130,260);
text(“watch it.”, 130, 300);

}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
serialVal = int(buf);
println(“potValue=”+serialVal);
buf = “”;
}
}

 

 

 

screen-shot-2016-09-28-at-12-03-59-am screen-shot-2016-09-28-at-12-03-55-am screen-shot-2016-09-28-at-12-03-53-am screen-shot-2016-09-28-at-12-03-40-am untitled1

FSR + PhotoCell

For this lab project, I tried to use both FSR and PhotoCell to feed in multiple streams of changing data to Processing. Along with the blinking change of LED light, the ball on the screen can also change accordingly, in terms of color, size, and location on the screen.

For the mechanical part, I tried hand, foam, and a small bean-bag ball, and it’s amazing even for bean ball it works to transform the force from my squeezing hand to the FSR. Perhaps force could be transmitted evenly across the material I chose?

I also found it challenging to figure out how to feed more than one data to Processing, and that’s why I ended up using 1 FSR for this lab. I want to consult with someone who has experience to push this further.

Materials:

  • 1 Arduino Uno
  • Wires
  • 2 Resistors (220 Ohms, 10k Ohms)
  • 1 LED
  • 1 FSR
  • 1 Small Bean-Bag Ball

Arduino Code

int sensorPin = A0; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
int ledPin = 13; // select the pin for the LED

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(ledPin, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 val = analogRead(sensorPin);
 // turn the ledPin on
 digitalWrite(ledPin, HIGH);
 // stop the program for &lt;sensorValue&gt; milliseconds:
 delay(val);
 // turn the ledPin off:
 digitalWrite(ledPin, LOW);
 // stop the program for for &lt;sensorValue&gt; milliseconds:
 delay(val);
 Serial.println(val);//I tried to use 1 photoCell and 1 FSR, but I couldn't figure out how to send 2 data at the same time
}

Processing Code
* Arduino Ball Paint
 * (Arduino Ball, modified 2008)
 * ---------------------- 
 *
 * Draw a ball on the screen whose size is
 * determined by serial input from Arduino.
 *
 * Created 27 September 2016
 * Edited by Safei Gu
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodem1411"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

int serialVal = 0;

void setup() {
 size(300,300);
 frameRate(10);
 smooth();
 background(40,40,40);
 noStroke();
 port = new Serial(this, portname, 9600); 
}

void draw() {
 // erase the screen
 background(40, 40, 40);

// draw the ball
 fill(serialVal/100, serialVal/100, serialVal/100);
 ellipse(serialVal/30,serialVal/30,serialVal-50,serialVal-50);
}

// called whenever serial data arrives
void serialEvent(Serial port) {
 int c = port.read();
 if (c != lf &amp;&amp; c != cr) {
 buf += char(c);
 }
 if (c == lf) {
 serialVal = int(buf);
 println("val="+serialVal); 
 buf = ""; 
 }
}
 

Images

img_0867

Explosions in the Sky

Description:
I tried to use Particles by Daniel Shiffman to create an interesting visualization using 3 potentiometers for adjusting the RGB values of the foreground particles as well as the background color and used the Force Sensor resistor to adjust the transparency of the particles. I used two artist’s sponge to create a diffusion pad for the force sensor. On squeezing it vertically, the pressure is increased and so is the opacity of the particles. On squeezing it horizontally however, the pressure is relieved and the opacity is reduced.

Components:
1 Arduino UNO
1 FSR
3 Potentiometers
1 10k Ohm resistor
2 Artist sponges for force diffusion
Insulation tape

Arduino Code:


// Explosions in the Sky with an FSR, 3 pots and an Arduino UNO
// Code by Ganesh Iyer, UC Berkeley
// Date: 27th September, 2016

int fsrPin = A0;     // the cell and 10K pulldown are connected to a0
int fsrReading;    // the analog reading from the sensor divider

// The RGB Pots
// NOTE: Put in the right pin here with the A(x) prefix
int potRedPin = A3;
int potRedReading;

int potGreenPin = A2;
int potGreenReading;

int potBluePin = A1;
int potBlueReading;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  // obtain input from FSR to send to Processing

  potRedReading = analogRead(potRedPin);
  potRedReading = potRedReading/4;
  Serial.print(potRedReading);
  Serial.print(",");

  potGreenReading = analogRead(potGreenPin);
  potGreenReading = potGreenReading/4;
  Serial.print(potGreenReading);
  Serial.print(",");
  
  potBlueReading = analogRead(potBluePin);
  potBlueReading = potBlueReading/4;
  Serial.print(potBlueReading);
  Serial.print(",");

  fsrReading = analogRead(fsrPin);
  fsrReading = fsrReading/4;
  Serial.print(fsrReading);
  Serial.print(",");
  Serial.print(".");
  delay(100);
}

Processing Code:



// Explosions in the Sky by Ganesh Iyer
// Time: Ungodly
// Date: 27th September, 2016
// Based on Particles, by Daniel Shiffman.

// The idea is to use FSR to vary the alpha values of explosion
// Use the potentiometers to vary colors

// Processing Setup
import processing.serial.*;
String portname = "/dev/cu.usbmodem1411"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

color colorOfParticles;
float hexCode;

float r = 0;
float g = 0;
float b = 0;
float fsr = 0;
int serialVal = 0;

ParticleSystem ps;
PImage sprite;  

void setup() {
  port = new Serial(this, portname, 9600);
  size(1024, 768, P2D);
  orientation(LANDSCAPE);
  sprite = loadImage("sprite.png");
  ps = new ParticleSystem(10000);

  // Writing to the depth buffer is disabled to avoid rendering
  // artifacts due to the fact that the particles are semi-transparent
  // but not z-sorted.
  hint(DISABLE_DEPTH_MASK);
} 

void draw () {
  serialEvent(port);
  background((255-r), (255-g), (255-b));
  
  ps.update();
  ps.display();
  
  ps.setEmitter(width/2,(height/2)-200);
  
  fill(255);
}

void serialEvent(Serial p){ 
 String arduinoInput = p.readStringUntil(46);
 if(arduinoInput != null ){
   arduinoInput = trim(arduinoInput);
   float inputs[] = float(split(arduinoInput, ','));
   r = inputs[0];
   g = inputs[1];
   b = inputs[2];
   fsr = inputs[3];
 }
}

class Particle {

  PVector velocity; // do not change this
  float lifespan;
  // Verdict: Derive from FSR; this is alpha value. As long as the FSR is pressed, this will keep generating particles
  // If FSR is not pressed at all, the alpha value will be 0 and you can't see the particles anyway.
  // Hence, you do not require a mouseclick variable.

  // Verdict: Derive from the RGB Pots.
  
  PShape part;
  float partSize;
  // Do not change
  
  PVector gravity = new PVector(0,0.1);
  // Do not change

  Particle() {
    partSize = random(10,60);
    part = createShape();
    part.beginShape(QUAD);
    part.noStroke();
    part.texture(sprite);
    part.normal(0, 0, 1);
    part.vertex(-partSize/2, -partSize/2, 0, 0);
    part.vertex(+partSize/2, -partSize/2, sprite.width, 0);
    part.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
    part.vertex(-partSize/2, +partSize/2, 0, sprite.height);
    part.endShape();
    
    rebirth(width/2,height/2);
    lifespan = random(255);
  }

  PShape getShape() {
    return part;
  }
  
  void rebirth(float x, float y) {
    float a = random(TWO_PI);
    float speed = random(0.5,4);
    velocity = new PVector(cos(a), sin(a));
    velocity.mult(speed);
    lifespan = 255;   
    part.resetMatrix();
    part.translate(x, y); 
  }
  
  boolean isDead() {
    if (lifespan < 0) {
     return true;
    } else {
     return false;
    } 
  }
  

  public void update() {
    lifespan = lifespan - 1;
    velocity.add(gravity);
    
    
    // setTint(rgb value, alpha value) replace 255 with particleColor
    
    part.setTint(color(r, g, b, fsr));
    // This is setting color to white all the time. 
    part.translate(velocity.x, velocity.y);
  }
}

class ParticleSystem {
  ArrayList<Particle> particles;

  PShape particleShape;
  
  float red;
  float green;
  float blue;

  ParticleSystem(int n) {
    particles = new ArrayList<Particle>();
    particleShape = createShape(PShape.GROUP);

    for (int i = 0; i < n; i++) {
      Particle p = new Particle();
      particles.add(p);
      particleShape.addChild(p.getShape());
    }
  }

  void update() {
    for (Particle p : particles) {
      p.update();
    }
  }

  void setEmitter(float x, float y) {
    for (Particle p : particles) {
      if (p.isDead()) {
        p.rebirth(x, y);
      }
    }
  }

  void display() {

    shape(particleShape);
  }
}

img_20160928_020022

Video of the visualization