Gabriella Levine

ongoing and past work

circuit – 2 axis parallax accelerometer

This is on the arduino side:

int pinX = 3;
int pinY = 2;
unsigned long serialTimer = millis();
unsigned long xAcc = 0;
unsigned long yAcc = 0;
boolean flipflop;

void setup()
{
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
Serial.begin(115200);
}

void loop()
{
if (flipflop == true) {
xAcc = pulseIn(pinX, HIGH);
flipflop = false;
} else {
yAcc = pulseIn(pinY, HIGH);
flipflop = true;
}

if ((millis() - serialTimer) > 50 ) {
Serial.print("X ");
Serial.println(xAcc);
// Serial.print(" ");
Serial.print("Y ");
Serial.println(yAcc);
}
}

to visualize it:
in processing:

import processing.serial.*;

Serial port; // Create object from Serial class
int val; // Data received from the serial port
int xAngle;
int yAngle;

void setup()
{
println ( Serial.list());
size(200, 200,P3D);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, Serial.list()[0],115200);
port.bufferUntil(13);
lights();

}

void draw()
{
background(0);
directionalLight(51, 102, 126, 0, 0, -1);
translate(100,100,0);
rotateX(map(xAngle,3800,6300,-1 * HALF_PI,HALF_PI));
rotateY(map(yAngle,3800,6300,-1 * HALF_PI,HALF_PI));
translate(-50,-50,0);
rect(0,0,100,100);

}

void serialEvent(Serial p) {
String msg = port.readStringUntil(13);
if (msg != null) readMsg(msg);

}

void readMsg(String msg) {

//remove non printing chars
int badChars = 0;
for (int i = msg.length() -1; i >= 0; i--) {
char c = msg.charAt(i);
if ( c == 10 || c ==13) {
badChars++;
}
}
if (badChars > 0) msg = msg.substring(0,msg.length()-badChars+1);

String[] words = splitTokens(msg);
if (words[0].equals("X")) {

xAngle = int( words[1]);
}
if (words[0].equals("Y")) {
yAngle = int( words[1]);
}

}

Leave a Reply