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>

Leave a Reply