LAB4

albert_tjoeng's picture

Description:

for the programming part, I basically change the speed for a bounce ball using the FSR. If there is no force then the speed is zero, I can increase the speed of the ball by pressing the FSR.

for the mechanical part, I put my FSR under my doormat. This will be useful to detect that there is somebody who come in or leave from the front door.

Materials:

Arduino

FSR

Potentiometer

doormat

Arduino Code:

 

int potPinBlink = 0;
int valBlink = 0;
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);
}
 
void loop()                     // run over and over again
{
  valBlink = analogRead(potPinBlink);
  valBlink = map(valBlink, 1, 1022, 0, 40);
  Serial.println(valBlink);
}

Processing Code:

 

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 size = 60;       // Width of the shape
float xpos, ypos;    // Starting position of shape    
 
int xspeed = 0;  // Speed of the shape
int yspeed = 0;  // Speed of the shape
 
int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom
 
 
void setup() 
{
  size(400, 600);
  noStroke();
  frameRate(30);
  smooth();
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/2;
  port = new Serial(this, portname, 9600); 
}
 
void draw() 
{
  background(102);
  
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  
  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
  if (xpos > width-size || xpos < 0) {
    xdirection *= -1;
  }
  if (ypos > height-size || ypos < 0) {
    ydirection *= -1;
  }
 
  // Draw the shape
  ellipse(xpos+size/2, ypos+size/2, size, size);
}
 
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);
    xspeed = val;
    yspeed = val; 
    buf = "";
  }
}

 

photo.JPG
0
Your rating: None