Created
October 10, 2011 23:32
-
-
Save telamon/1276870 to your computer and use it in GitHub Desktop.
Arduino+Processing Oscilloscope
This file contains 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
#include "oscilloscope.h" | |
#define ANALOG_IN 0 | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
int val = analogRead(ANALOG_IN); | |
OSC(0,val); // Draws pretty white line thanks to Sofian Audry | |
OSC(1, val*1.3); // Overlaps a red line version of the signal amplified by a third. | |
} |
This file contains 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
/* | |
* oscilloscope.h | |
* | |
* Gives a visual rendering of a value (typically analog pin 0) in realtime. | |
* | |
* This is an object-oriented version of the Poorman's oscilloscope as explained here: | |
* http://accrochages.drone.ws/node/90 | |
* | |
* (c) 2008 Sofian Audry | info(@)sofianaudry(.)com | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#ifndef OSCILLOSCOPE_INC | |
#define OSCILLOSCOPE_INC | |
#include "WProgram.h" | |
void writeOscilloscope(int channel, int value) { | |
Serial.print( 0xff, BYTE ); // send init byte | |
Serial.print( channel & 0xff, BYTE); // Send channel ID; | |
Serial.print( (value >> 8) & 0xff, BYTE ); // send first part | |
Serial.print( value & 0xff, BYTE ); // send second part | |
} | |
void writeOscilloscope(int value){ | |
writeOscilloscope(0,value); | |
} | |
#define OSC(c,v) (writeOscilloscope(c,v)) | |
#endif | |
/* An example of Arduino code. */ | |
/* | |
#include "oscilloscope.h" | |
#define ANALOG_IN 0 | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
int val = analogRead(ANALOG_IN); | |
OSC(0,val); | |
} | |
*/ | |
This file contains 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
/* | |
* Oscilloscope | |
* Gives a visual rendering of analog pin 0 in realtime. | |
* | |
* This project is part of Accrochages | |
* See http://accrochages.drone.ws | |
* | |
* (c) 2008 Sofian Audry (info@sofianaudry.com) | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
import processing.serial.*; | |
Serial port; // Create object from Serial class | |
int val[]; // Data received from the serial port | |
int channels = 5; | |
int[][] values; | |
int[] palette = new int[]{#ffffff,#ff0000,#00ff00,#0000ff,#888800,#888888}; | |
void setup() | |
{ | |
size(640, 480); | |
// Open the port that the board is connected to and use the same speed (9600 bps) | |
String[] ttys = Serial.list(); | |
port = new Serial(this, ttys[0], 9600); | |
values = new int[channels][width]; | |
val=new int[channels]; | |
smooth(); | |
} | |
int getY(int val) { | |
return (int)(val / 1023.0f * height) - 1; | |
} | |
void draw() | |
{ | |
while (port.available() >= 4) { | |
if (port.read() == 0xff) { | |
int channel =port.read(); | |
val[channel] = (port.read() << 8) | (port.read()); | |
} | |
} | |
background(0); | |
for (int c=0; c< channels; c++){ | |
int delta = values[c][width-1] - val[c]; | |
for (int i=0; i<width-1; i++) | |
values[c][i] = values[c][i+1]; | |
values[c][width-1] = val[c]; | |
stroke(palette[c]); | |
text("C"+c+": "+val[c], 15,20+30*c,150,70); | |
text("D: "+ delta, 150,20+30*c,150,70); | |
for (int x=1; x<width; x++) { | |
line(width-x, height-1-getY(values[c][x-1]), | |
width-1-x, height-1-getY(values[c][x])); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WTF is going on here... I imported the code and tried to run it.
processing.app.SketchException: Not expecting symbol 'i', which is LATIN SMALL LETTER I.