Skip to content

Instantly share code, notes, and snippets.

@telamon
Created October 10, 2011 23:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telamon/1276870 to your computer and use it in GitHub Desktop.
Save telamon/1276870 to your computer and use it in GitHub Desktop.
Arduino+Processing Oscilloscope
/*
* 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/>.
*/
/* 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);
}
*/
#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
/*
* 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();
println("BORKLET: " + ttys.length);
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]));
}
}
}
@KillerXtreme-
Copy link

Is it possible to modify this to read a PWM signal from digital pin 5?

@theoparis
Copy link

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.

@Ellafa
Copy link

Ellafa commented Sep 15, 2021

Guys It's code for what
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment