Created
October 26, 2014 11:59
-
-
Save yoggy/cad2bb67594218284de9 to your computer and use it in GitHub Desktop.
ちょっとしたオシロスコープがわりに?
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
//firmata_analog_linechart.pde | |
import cc.arduino.*; | |
import processing.serial.*; | |
import java.util.*; | |
class LineChart { | |
Vector v; | |
int size; | |
LineChart(int size) { | |
v = new Vector(); | |
this.size = size; | |
} | |
void clear() { | |
v.clear(); | |
} | |
void add(int val) { | |
v.add(val); | |
if (this.size <= v.size()) { | |
v.remove(0); | |
} | |
} | |
void draw() { | |
if (v.size() < 2) return; | |
for (int i = 0; i < v.size() - 1; ++i) { | |
int v0 = (Integer)v.get(i); | |
int v1 = (Integer)v.get(i+1); | |
line(i, v0, i+1, v1); | |
} | |
} | |
} | |
Arduino arduino; | |
LineChart line_chart; | |
void setup() { | |
size(400, 400); | |
println(Arduino.list()); | |
arduino = new Arduino(this, Arduino.list()[0], 57600); | |
line_chart = new LineChart(width); | |
} | |
void draw() { | |
background(0); | |
int val = arduino.analogRead(0); | |
line_chart.add((int)((1.0-val/1023.0)*height)); | |
noFill(); | |
stroke(0, 255, 0); | |
line_chart.draw(); | |
stroke(255, 255, 0); | |
for (int i = 0; i <= 5; ++i) { | |
int h = (int)((1.0 - i / 5.0) * height); | |
line(0, h, width, h); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment