Last active
December 28, 2015 18:38
-
-
Save bilalsavas/7544048 to your computer and use it in GitHub Desktop.
Arduino memsic gyro sensor project w/ processing dataviz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Memsic2125 | |
Read the Memsic 2125 two-axis accelerometer. Converts the | |
pulses output by the 2125 into milli-g's (1/1000 of earth's | |
gravity) and prints them over the serial connection to the | |
computer. | |
The circuit: | |
* X output of accelerometer to digital pin 2 | |
* Y output of accelerometer to digital pin 3 | |
* +V of accelerometer to +5V | |
* GND of accelerometer to ground | |
http://www.arduino.cc/en/Tutorial/Memsic2125 | |
created 6 Nov 2008 | |
by David A. Mellis | |
modified 30 Aug 2011 | |
by Tom Igoe | |
This example code is in the public domain. | |
*/ | |
// these constants won't change: | |
const int xPin = 9; // X output of the accelerometer | |
const int yPin = 10; // Y output of the accelerometer | |
void setup() { | |
// initialize serial communications: | |
Serial.begin(115200); | |
// initialize the pins connected to the accelerometer | |
// as inputs: | |
pinMode(xPin, INPUT); | |
pinMode(yPin, INPUT); | |
} | |
void loop() { | |
// variables to read the pulse widths: | |
int pulseX, pulseY; | |
// variables to contain the resulting accelerations | |
int accelerationX, accelerationY; | |
// read pulse from x- and y-axes: | |
pulseX = pulseIn(xPin,HIGH); | |
pulseY = pulseIn(yPin,HIGH); | |
// convert the pulse width into acceleration | |
// accelerationX and accelerationY are in milli-g's: | |
// earth's gravity is 1000 milli-g's, or 1g. | |
accelerationX = ((pulseX / 10) - 500) * 8; | |
accelerationY = ((pulseY / 10) - 500) * 8; | |
// print the acceleration | |
Serial.print(accelerationX); | |
// print a tab character: | |
Serial.print(","); | |
Serial.print(accelerationY); | |
Serial.print("."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
import ddf.minim.*; | |
import ddf.minim.ugens.*; | |
Serial myPort; | |
// create all of the variables that will need to be accessed in | |
// more than one methods (setup(), draw(), stop()). | |
Minim minim; | |
AudioOutput out; | |
// the Oscil we use for modulating frequency. | |
Oscil fm; | |
void setup() | |
{ | |
size(1600, 900); | |
myPort = new Serial(this, Serial.list()[2], 115200); | |
fill(255); | |
minim = new Minim( this ); | |
out = minim.getLineOut(); | |
Oscil wave = new Oscil( 200, 0.8, Waves.TRIANGLE ); | |
fm = new Oscil( 10, 2, Waves.SINE ); | |
fm.offset.setLastValue( 200 ); | |
fm.patch( wave.frequency ); | |
wave.patch( out ); | |
} | |
int x=0, y=0; | |
String data = ""; | |
void draw() | |
{ | |
fill(0, 0, 0, 10); | |
noStroke(); | |
rect(0, 0, width, height); | |
pushMatrix(); | |
translate(width/2, height/2); | |
String inBuffer = myPort.readString(); | |
if (inBuffer != null) { | |
if (inBuffer.indexOf(".") != -1) | |
{ | |
data += inBuffer.substring(0, inBuffer.indexOf(".")); | |
printData(); | |
} | |
else | |
{ | |
data += inBuffer; | |
} | |
} | |
popMatrix(); | |
} | |
int oldX,oldY; | |
color c; | |
void printData() | |
{ | |
c = color(random(255),random(255),random(255)); | |
x=int(split(data, ',')[0]); | |
y=int(split(data, ',')[1]); | |
fill(255); | |
stroke(c); | |
line(oldX,oldY,x,y); | |
oldX=x; | |
oldY=y; | |
ellipse(x, y, 50, 50); | |
float modulateAmount = map( y, 0, height, 220, 1 ); | |
float modulateFrequency = map( x, 0, width, 0.1, 100 ); | |
fm.frequency.setLastValue( modulateFrequency ); | |
fm.amplitude.setLastValue( modulateAmount ); | |
data=""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is the serial pot on 115200?