Description:
The purpose of the lab was to experiment with two additional analog sensors: photo cells that measure the amount of ambient light and touch sensors that measure the amount of physical pressure.
The lab also introduced the Processing language that is able to generate different forms of visual output.
The first part of the lab combined the capabilities of the sensors to generate variable analog signals and the ability of the Arduino to capture the analog signals. A LED light was made to fade based on the variable analog input coming from either the intensity of the light on the photocell or the intensity of physical pressure on the force sensor. The second part of the lab connected the Arduino to generate ASCII output to the USB serial port of the computer. The Processing program would subsequently capture the ASCII values generated by the incoming serial port and generate random ellipses on the screen.
For the homework, I decided to use one of the Processing Tutorial examples for generating a Sin Wave: http://processing.org/examples/sinewave.html. The output generates an animated Sin curve that waves at a constant frequency. I correlated the serial input coming from the Arduino to vary the amplitude of the sign wave. One of the challenges was understanding what types of values were generated from the Arduino to communicate to the Processing program. After reading the code, I understood that the Processing program was reading individual numeric ASCII characters generated by the Arduino. After reading the range of input, I had divided the total range by 20 to calibrate the max Amplitude change to be around +10. This constituted a max change of 20%. I also experimented with change the frequency of the SIN wave.
I used a pocket package of tissues to distribute the area of measurement for the force sensor. The thickness of the 10 tissues provided more resistance to distribute of pressure across the force sensor. As a result, one would have to apply quite a bit of pressure to trigger the maximum readouts on the force sensor.
Components Used:
1- Breadboard
1- Arduino Uno
1 LEDs (Blue, Green)
1 USB interface cable to Arduino
1 220 Ohm Resistors
1 Photocell
1 Force sensitive resistor
1 10k Ohm resistor
Code Used:
/*
Function uses the example code for drawing sine wave:
http://processing.org/examples/sinewave.html
Signficant changes in the force sensor will change the amplitude of the sine wave.
*/
//Setup variables for serial input
import processing.serial.*;
Serial port ;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int zero = 48; //ASCII numeric zero = 48
int counter = 0;
String buf="";
int xspacing = 16; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float frequency = 1;
float theta = 0.0; // Start angle at 0
float amplitude = 75.0; // Height of wave
float period = 500.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave
void setup() {
size(640, 360);
w = width+16;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
port = new Serial(this, Serial.list()[1], 9600); // Assign physical Serial Port
}
void draw() {
background(0);
calcWave();
renderWave();
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += 0.02;
// For every x value, calculate a y value with sine function
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(frequency*x)*amplitude;
x+=dx;
}
}
void renderWave() {
noStroke();
fill(255);
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
}
}
// Code to test modulation of sin wave amplitude and frequency
void keyPressed(){
if(key == '+'){
//amplitude+=1;
frequency =frequency * 1.05;
}
if(key == '-'){
//amplitude-=1;
frequency =frequency * .9;
}
}
// Code to process Serial input from Arduino
// Reads serial input. If the serial input is significant, changes the amplitude and calls draw functio()
void serialEvent(Serial p) {
int c = port.read();
/* this print statement will allow you to see whether or not Processing can "hear" anything that
Arduino is sending. It should print all the changing values of the FSR or Photocell in your circuit
If nothing is printing, you probably picked the wrong serial port.
*/
// println(c);
if (c != lf && c != cr&& c!=zero) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
if(val>0){// Change the amplitude if there is signficant serial input change
println("val="+val);
amplitude += val/20; // adjust the range of amplitude change
draw();
}else { // base case; serial input just reads "zero" ascii character
amplitude = 75.0; // base amplitude
draw();
}
buf = "";
}
}
- Login to post comments