Skip to content
konsumer edited this page Dec 8, 2014 · 5 revisions

Yoga Purna Tama from Indonesia had a great question:

in your code for processing, it can display 0-5 volt right? the device i want to measure has voltage from minus 15 - 15 volt. but i already make a voltage divider so the voltage that goes to the arduino still 0-5 v.

but , on the oscilloscope, i want to modify few things from your code. i mean, when the arduino send 5 voltage to the processing, i want it to display +15 volt, not 5volt, and if the arduino send 0v , i want it display -15v.

So, the first part is the hard part, setting up the voltage-divider. Arduinos can only safely read 0V-5V.

  • You can find awesome info on adjusting voltage here
  • Here is info on getting it to work right with a negative /positive range
  • Here is a calculator for resistor dividers
  • Test with your multimeter with the full range of voltage, because it could break your arduino if you go outside of that.

Here is the circuit that Yoga Purna Tama made:

divider schematic

Next, all we have to do is adjust the display-range of the code. You can do this in 2 ways. You can adjust the multiplier (by default 5) and adjust the minimum & maximum, and set the real value, or you can adjust the multiplier & adjust the output, on display. I went for the latter, because it seemed slightly simpler:

import controlP5.*;
import processing.serial.*;
import cc.arduino.*;
import arduinoscope.*;
 
Arduino arduino;
ControlP5 cp5;
Oscilloscope[] scopes = new Oscilloscope[6];
float multiplier;
 
void setup(){
  size(800, 800);
  ControlP5 cp5 = new ControlP5(this);
  frame.setTitle("Arduinoscope");
  
  // COM dropdown
  DropdownList com = cp5.addDropdownList("com")
    .setPosition(110, 20)
    .setSize(200,200);
  String[] arduinos = arduino.list();
  for (int i=0; i<arduinos.length; i++) {
    com.addItem(arduinos[i], i);
  }
  
  int[] dim = { width-130, height/scopes.length};
  
  for (int i=0;i<scopes.length;i++){
    int[] posv = new int[2];
    posv[0]=0;
    posv[1]=dim[1]*i;
 
    // random color, that will look nice and be visible
    scopes[i] = new Oscilloscope(this, posv, dim);
    scopes[i].setLine_color(color((int)random(255), (int)random(127)+127, 255));
    
    scopes[i].setMultiplier(30);
    
    cp5.addButton("pause" + i)
      .setLabel("pause")
      .setValue(i)
      .setPosition(dim[0]+10,posv[1] + 85)
      .updateSize();
     
    scopes[i].setPause(false);
  }
  
  // multiplier comes from 1st scope
  multiplier = scopes[0].getMultiplier()/scopes[0].getResolution();
  
}
 
void draw(){
  background(0);
  text("arduinoscope", 20, 20);
  
  int val;
  int[] dim;
  int[] pos;
  
  for (int i=0;i<scopes.length;i++){
    dim = scopes[i].getDim();
    pos = scopes[i].getPos();
    scopes[i].drawBounds();
    stroke(255);
    line(0, pos[1], width, pos[1]);
    if (arduino != null){
      val = arduino.analogRead(i);
      scopes[i].addData(val);
      scopes[i].draw();
      text("analog " + i, dim[0]+10, pos[1] + 30);
      text("val: " + ((val*multiplier) - 15) + "V", dim[0]+10, pos[1] + 45);
      text("min: " + ((scopes[i].getMinval()*multiplier)-15) + "V", dim[0]+10, pos[1] + 60);
      text("max: " + ((scopes[i].getMaxval()*multiplier)-15) + "V", dim[0]+10, pos[1] + 75);
    }
  }
}
 
 
void controlEvent(ControlEvent theEvent) {
  int val = int(theEvent.getValue());
  
  if (theEvent.getName() == "com"){
    arduino = new Arduino(this, Arduino.list()[val], 57600);
  }else{    
    scopes[val].setPause(!scopes[val].isPause());
  }
}

In this code, I do a scopes[i].setMultiplier(30); to set the total range, and -15 for the values that are displayed.

Clone this wiki locally