Lab 4- FSR

 

Description:

I worked on the two components, ie the visual and the mechanical ones separately. 

I first visualized the force applied on the FSR and the use of the pot by a simple line graph.

For the mechanical part, I built a "metal detector" which would use the force applied by materials attracted to a magnet which I attached to the FSR. There would be an "ALERT" message on the computer screen, as well as an LED light attached to the circuit would start blinking upon contact.

Materials:

-FSR

- Potentiometers

-magnets

- LED

 

Arduino code part 1

 

 
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
   pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}
 
void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter 
  // to stabilize after the last reading:
   delay(10);
 
}
 
Processing Part 1
 
import processing.serial.*;
Serial port;
int XPos = 10;
 
void setup(){
  size(600,255);
  port = new Serial(this, "COM3", 9600);
  port.bufferUntil('\n');
  background(0); 
}
 
void draw(){
  
}
 
void serialEvent (Serial port) {
 
  String inString = port.readStringUntil('\n');
 
  if (inString != null) {
    inString = trim(inString);
 
    float inByte = float(inString); 
    inByte = map(inByte, 0, 1023, 0, height);
    
    stroke(inByte,0,255);
    noFill();
    line(XPos, height - inByte, XPos, height);
  
    if (XPos >= width) {
      XPos = 0;
      background(0); 
    } 
    else {
      XPos++;
    }
  }
}
 
Arduino Part 2
 
int ledPin = 11;
int val=0;
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
   pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}
 
void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter 
  // to stabilize after the last reading:
   delay(10);
  val = analogRead(analogRead(A0));    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off 
  delay(val);       
 
}
 
Processing Part 2
 
/*
 * Arduino Metal Detector
 * 
 * ---------------------- 
 *
 * Uses an FSR attached to a magnet to detect metallic/magnetic materials.
 * Displays messages to the user when these materials are detected
 *
 * Receives an ASCII number over the serial port, 
 * terminated with a carriage return (ascii 13) then newline (10).
 * 
 * This matches what Arduino's " Serial.println(val)" function
 * puts out.
 *
 *  Modified from the Bouncing Ball Paint Code
 * http://todbot.com/ 
 */
import processing.serial.*;
 
String portname = "COM3";
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(600,600);
  frameRate(10);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 9600); 
}
void draw() {
}
 
 
// 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); 
   
    buf = "";
    background(40,40,40);  // erase screen
    if (val>0)
    {
    textSize(50);
    text("ALERT!",50,250 );
    text("METAL FOUND!!!",50,350 );
    }
    else
    { textSize(30);
     text("DETECTING.....",50,50 );
      
    }
  }
}
 
 
 
 
Magnets.jpg
line graph.jpg
img.jpg
0
Your rating: None