Description
Arduino Color Size Pressure Detector
This Lab uses Processing to draw a ball on the screen, size depending on a random selection of an input device.
A Potentiometer (Pot) and a Force Sensitive Resistor (FSR) provides serial values. As the serial port begins to recognize values, the first input device READ will be considered the SIZE changer, the second will be the COLOR ("FILL") changer. Processing receives their ASCII number (for voltage) over the serial port, terminating with a carriage return (ascii 13) then newline (10).
A program written in an Arudino sketch is uploaded, providing values as such:
Serial.println(v0);
Serial.println(v1);
* The min value for each is set to be > 20, so as not to conflict with Newline (which is ASCII 10) or Carriage Return (which is ASCII 13)
* The max was set to be 255 because higher values gave faulty values.
For the mechanical portion, I used my contraption to measure the hanging weights of my clothes (a heavy sweater and some light weighing pants). This can be used as a sort of gauge to measure the stresses on a pole => When there is not much weight, I colored the ball green and kept it big, but as pressure grows, I shrink the ball and/or make the ball red.
Components Used
1- Arduino Board
2- 220 Ω Resistors (for LEDs as visual indication of input)
1- 10,000 Ω Resistors (for Force Sensitive Resistor, as resistance to ground)
1- Breadboard
1 – Potentiometer
1 – Force Sensitive Resistor (FSR)
Connecting wires
Code
--- ARDUINO CODE ---
/*
* Resistive Sensor Input
* Takes the input from a resistive sensor, e.g., FSR or photocell
* Dims the LED accordingly, and sends the value (0-255) to the serial port
*/
int readA0 = 0; // select the input pin for the sensor
int readA1 = 1;
int output10 = 10; //output on 10 to show that it works...unused
int output11 = 11; //output on 11 to show that it works...unused
int val0 = 0; // variable to store the value coming from the sensor
int val1 = 0;
int v0 = 0; //value with minimum of 20 to be sent to Processing
int v1 = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
val0 = analogRead(readA0); // read the value from the sensor, 0-1023
val1 = analogRead(readA1);
v0 = val0 / 4;
v1 = val1 / 4;
if (v0 < 20) v0 = 20; //prevents sending low numbers like 9 (/t) 10 (/n) or 11 (/r), messing up status
else if (v1 > 1000) val1 = 1000; //prevents going too high with numbers, getting neg. numbers
if (v1 < 20) v1 = 20;
else if (v1 > 1000) val1 = 1000; //prevents going too high with numbers, getting neg. numbers
analogWrite(output10, v0); // used LED for correctly working indicator light
analogWrite(output11, v1);
Serial.println(v0);
Serial.println(v1);
delay(500); // rest a little...
}
--- PROCESSING CODE ---
/*
* Arduino Color Size Pressure Detector
* (Created 2013)
* ----------------------
* Draw a ball on the screen, size depending on a random selection of an input device.
* As the serial port begins to recognize values, the first input device READ will be
* considered the SIZE changer, the second will be the COLOR ("FILL") changer.
* A Potentiometer (Pot) and a Force Sensitive Resistor (FSR) provides serial values.
*
* Processing receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
* A program written in an Arudino sketch should be uploaded, providing values such:
* Serial.println(v0);
* Serial.println(v1); <END>
* The min value for each is set to be > 20, so as not to conflict with Newline/CarReturn
* The max was set to be 255; higher values gave faulty values
*
* Created 08 October 2013
* copyleft 2013 Clint Anderson <clintanderson@berkeley.edu>
*
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A4001nLJ"; // or "COM5"
Serial port;
String A0buf="";
String A1buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
boolean A0active = false;
void setup() {
size(300,300);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
portname = Serial.list()[0];
port = new Serial(this, portname, 9600);
}
// Need this empty function to show the ball
void draw() {
}
// Called whenever serial data arrives
void serialEvent(Serial p) {
//The first port read (A0 vs. A1) is random between a the pot an FSR
int c = port.read();
//If a voltage-in is read that is not equal to /newline or /carriageReturn
//Add to the buffer to be read when there IS a newline or carriage Return.
//For 215 sensed, for the first loop c reads "2", the second "1", the third "5"
//For the fourth, c will read 13 => next else if statement
if ((c != lf) && (c != cr)) {
if (A0active){
A0buf += char(c);
println("Reading -- C: " + c + "\tchar(c): " + char(c) + "\tA0Buf: " + A0buf);
}
else{
A1buf += char(c);
println("Reading -- C: " + c + "\tchar(c): " + char(c) + "\tA1Buf: " + A1buf);
}
}
//During new lines, 1 source will control the fill, the other the size
else if (c == lf) {
if(!A0active){
int A0 = int (A0buf);
fill(A0, 255-A0, 127);
}
else{
print ("\tA1 before: " + A1buf);
int A1 = int (A1buf);
print("\tA1 now an int: " + A1);
println("\tThe subtractions: " + (255 - A1));
println("If (lf?): " + c + "\tA1 Buffer is: " + A1 );
background(40, 40, 40);
ellipse(width/2, height/2, 255 - A1, 255 - A1);
}
A0buf = "";
A1buf = "";
}
//During carriage returns in between, switch to reading the other input source
else if(c == cr){
print("Else (cr?): " + c);
//boolean helps distinguish which buffer belongs to which input method
if(A0active){
A0active = false;
}
else
A0active = true;
println("\t A0 Active?:" + A0active);
}
}
- Login to post comments