Lab 4_with FSR

Posted by yoon

yoon's picture

 

  • Components Used: Arduino Board, LED, 10K and 220K resistor, FSR
 
  • Description: 
A bear is playing a game: he can make circles as big as the red circle, (it is the boundary of gray/white circle can reach) and a flower in the middle of circle at the end. The goal is to reach the red circle, and make a perfect shaped flower at the end. The harder the bear presses FSR, the larger the white circle he gets. When the bear starts releasing FSR, the white circle gets smaller and 5 gray circles appear around the white circle, in a flower shape. 
 
 
  • Code:
 
import processing.serial.*;
 
String portname = "COM5"; 
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(300,300);
  frameRate(10);
  smooth();
  background(0);
  noStroke();
  fill (255, 0, 0);
  ellipse(150, 150, 245, 245);
  fill (0);
  ellipse (150, 150, 240, 240);
  
  port = new Serial(this, portname, 9600); 
}
void draw() {
}
 
void flowercircle(int x, int y, int r) {
    for (int a = 0; a < 360; a += 75) {
      float xoff = cos(radians(a)) *6 ;
      float yoff = sin(radians(a)) *6;
      fill(100);
      ellipse(x + xoff, y + yoff, r, r);
    }
    fill(255);
    ellipse(x, y, r, r);
  }
 
 
// 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 = 150;
    int y = 150;
    flowercircle(x,y,val);
    buf = "";
  }
}
0
Your rating: None