Sunday, February 24, 2008

Sensors and Time



Sensors & Time - Graphing, visualizing and characterizing sensor behavior.

Two potentiometer and one photo sell sensors.



Arduino Code

int sensorPin1 = 4; // select the input pin for sensor
int sensorPin2 = 5; // select the input pin for other sensor
int sensorPin3 = 0; // select the input pin for other sensor
int ledPin = 13; // select the pin for the LED
int val1 = 0; // variable to store the value coming from the sensor
int val2 = 0; // variable to store the value coming from the sensor
int val3 = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(sensorPin1, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(sensorPin2, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(sensorPin3, OUTPUT); // declare the ledPin as an OUTPUT

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

digitalWrite(ledPin, HIGH); // sets the LED on

val1 = analogRead(sensorPin1); // read the value from the sensor
val2 = analogRead(sensorPin2); // read the value from the sensor
val3 = analogRead(sensorPin3); // read the value from the sensor

Serial.print("A"); //header variable, so we know which sensor value is which
Serial.print(val1, DEC); //send as a ascii encoded number - we'll turn it back into a number at the other end
Serial.print(10, BYTE); //terminating character

Serial.print("B"); //header variable, so we know which sensor value is which
Serial.print(val2, DEC); //send as a ascii encoded number - we'll turn it back into a number at the other end
Serial.print(10, BYTE); //terminating character

Serial.print("C"); //header variable, so we know which sensor value is which
Serial.print(val3, DEC); //send as a ascii encoded number - we'll turn it back into a number at the other end
Serial.print(10, BYTE); //terminating character

delay(10);
}



============================================


Processing Code

import processing.serial.*;

Serial myPort;
PFont myFont;
String buff = "";
int val = 0;
int NEWLINE = 10;
int i = 1;
int valueA, valueB, valueC;

int[] valuesA = new int[10];
int[] valuesB = new int[10];
int[] valuesC = new int[10];

float valNormA, valNormB, valNormC;
int valnormA, valnormB, valnormC;

String bufA="", bufB="", bufC="";
int buf;

int wrote = 0;
int legendary = 0;
int offset = 0;
int offsettext = 25;
int lf = 10;


void setup () {
size(800, 600);

myFont = loadFont("ArialMT-12.vlw");
textFont(myFont,14);
textAlign(LEFT);
smooth();
strokeCap(ROUND);

background(0);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);

line(0, 220, width, 220);
line(0, 440, width, 440);

}

void draw()
{
fill(0,5);
noStroke();
rect(0,0,width,height);

while (myPort.available() > 0) {
serialEvent(myPort.read());
}

valNormA = valueA/1023.0;
valNormB = valueB/800.0;
valNormC = valueC/1023.0;

valnormA = (int)valueA;
valnormB = (int)valueB;
valnormC = (int)valueC;

strokeWeight(5);
stroke(255,100);
fill(0,0,255);
ellipse(i, valNormA*220+100, 20, 20);

pushMatrix();
translate(0, 220);
stroke(255,100);
fill(0,255,0);
ellipse(i,valNormB*1000+100, 20,20);
popMatrix();

pushMatrix();
translate(0,440);
stroke(255,100);
fill(255,0,0);
ellipse(i,160/2 - valNormC*160, 20, 20);
popMatrix();

if (i > width) {
i = 0;
pushMatrix();
strokeWeight(2);
fill(0,1);
rect(0,0, width, height);

popMatrix();
}
else {
i++;
}

if (wrote > 30)
{

fill(255);
text(valnormA, i, ( valNormA*220)+80);
pushMatrix();
translate(0, 220);
fill(255);
text(valnormB, i, ( valNormB*1000)+80);
popMatrix();
pushMatrix();
translate(0, 440);
fill(255);
text(valnormC, i, (160/2 - valNormC*160)+60);
popMatrix();
wrote = 0;
}
wrote++;

}

void serialEvent(int serial){
if(serial!=10) {
if (serial=='A') buf = 1;
if (serial=='B') buf = 2;
if (serial=='C') buf = 3;

if (buf==1) {if (serial!='A') bufA += char(serial);}
else if (buf==2) { if (serial!='B') bufB+= char(serial);}
else if (buf==3) { if (serial!='C') bufC+= char(serial);}
} else {
if (buf==1) {valueA = int(bufA); bufA="";}
else if (buf==2) {valueB = int(bufB); bufB="";}
else if (buf==3) {valueC = int(bufC); bufC="";}
}
}

No comments: