Created
November 28, 2013 23:06
-
-
Save rlogiacco/7699213 to your computer and use it in GitHub Desktop.
Processing scale aware plotter for Arduino usable as coarse oscilloscope.
Two Arduino code examples provided.
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
void setup(){ | |
Serial.begin(9600); | |
} | |
void loop(){ | |
Serial.print(analogRead(A0)); | |
Serial.print(","); | |
Serial.print(analogRead(A1)); | |
Serial.print(","); | |
Serial.print(analogRead(A2)); | |
Serial.println(); | |
} |
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
long lastMajor, lastMinor; | |
void setup(){ | |
Serial.begin(9600); | |
lastTime = lastTimeMinor = millis(); | |
} | |
void loop(){ | |
long time = millis(); | |
if (time - lastMajor >= 1000) { | |
lastMajor += 1000; | |
lastMinor += 250; | |
Serial.println("==="); | |
} else if (time - lastMinor >= 250) { | |
lastMinor += 250; | |
Serial.println("---"); | |
} | |
Serial.print(analogRead(A0)); | |
Serial.print(","); | |
Serial.print(analogRead(A1)); | |
Serial.print(","); | |
Serial.print(analogRead(A2)); | |
Serial.print(","); | |
Serial.print(analogRead(A3)); | |
Serial.print(","); | |
Serial.print(analogRead(A4)); | |
Serial.print(","); | |
Serial.print(analogRead(A5)); | |
Serial.println(); | |
} |
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
// Time Aware Plotter | |
// License: Creative Commons BY-SA | |
// This program takes ASCII-encoded strings from the serial port at 9600 baud and graphs them. | |
// It expects up to 6 comma separated values in the range 0 to 1023 followed by a newline. | |
// Plotting must be started with a mouse click and can be paused with a mouse click as well. | |
// Please note: while plotting is stopped serial data is discarded! | |
import processing.serial.*; | |
Serial myPort; | |
final int BORDER = 3; | |
final int BAND = 10; | |
final int TRANSPARENCY = 160; | |
color[] COLORS = { | |
#0000AA, | |
#00AA00, | |
#AA0000, | |
#AAAA00, | |
#AA00AA, | |
#00AAAA | |
}; | |
boolean pause = true, standby = true; | |
int x = 1 + BORDER; | |
void setup () { | |
// set the window size: | |
size(1000, 400); | |
// list all the available serial ports | |
println(Serial.list()); | |
// open the first serial port | |
myPort = new Serial(this, Serial.list()[0], 9600); | |
// don't generate a serialEvent() unless you get a newline character: | |
myPort.bufferUntil('\n'); | |
// set inital background: | |
background(255); | |
noSmooth(); | |
fill(#000000); | |
textSize(10); | |
text("Click to start", BORDER + 10, BORDER + 10); | |
} | |
void draw () { /* DO NOTHING */ } | |
void serialEvent (Serial myPort) { | |
// get the ASCII string: | |
String inputLine = myPort.readStringUntil('\n').trim(); | |
if (standby) { | |
return; | |
} | |
if (inputLine.equals("===")) { | |
// major marker | |
strokeWeight(2); | |
stroke(#333333); | |
line(x, BORDER, x, height - BORDER); | |
} else if (inputLine.equals("---")) { | |
// minor marker | |
strokeWeight(1); | |
stroke(#CCCCCC); | |
line(x, BORDER, x, height - BORDER); | |
} else if (inputLine != null) { | |
// trace | |
String[] values = split(inputLine, ","); | |
for (int i = 0; i < values.length; i++) { | |
// convert to an int and map to the screen height: | |
int reading = int(values[i]); | |
float y = map(reading, 0, 1023, BORDER, height - BORDER); | |
smooth(); | |
strokeWeight(2); | |
stroke(COLORS[i], TRANSPARENCY); | |
point(x, y); | |
noSmooth(); | |
} | |
if (x >= width - BORDER) { | |
// at the edge of the screen, go back to the beginning: | |
x = BORDER; | |
if (pause) { | |
standby = !standby; | |
fill(#000000); | |
textSize(10); | |
text("Click to resume", BORDER + 10, BORDER + 10); | |
} else { | |
stroke(255); | |
strokeWeight(BAND); | |
line(x, BORDER, x, height - BORDER); | |
} | |
} else { | |
// increment the horizontal position: | |
x++; | |
stroke(255); | |
strokeWeight(BAND); | |
line(x + BAND / 2, BORDER, x + BAND / 2, height - BORDER); | |
} | |
} | |
} | |
void mousePressed() { | |
if (standby) { | |
standby = false; | |
} | |
pause = !pause; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work!