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
 fill(255, serialVal, 255/n);
 //ellipse(150,150,serialVal,serialVal);
 rect(70, 70, serialVal+30, serialVal +30, serialVal/20);
}

// 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 = ""; 
 }
}


Leave a Reply