Using Processing to display a graph of voltage change over time.

edited March 2016 in Arduino

Hello there so I am brand spanking new to processing and my Arduino Uno. The project I am doing is a salinity detector (of sorts). However, for the short term, instead of detecting salinity, I am detecting changes in light with a photo-resistor in the form of voltage differences. I am then attempting to graph the change in voltage over time. My code is coming from a tutorial for processing (have never used until now) and now I want to add some additional complexity to the graphed response, here is my code so far:

Arduino Uno:

int val = 0; // variable to store value read at analog pin 3

void setup(){
  Serial.begin(9600); // set up to print to monitor, baud rate = 9600
}

void loop(){
  val = analogRead(3); // read analog input pin 3
  Serial.println(val); // print value to serial monitor
  delay(2);

}

and Processor:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int xPos = 1;
float inByte = 0;     // Data received from the serial port

void setup() 
{
// set window size:
  size(400, 300);
// set up port
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600); 

// don't generate a serialEvent() unles you get a newline character:
myPort.bufferUntil('\n');

// set inital background
background(0);
}

void draw(){

  // draw the line:
  stroke(127, 34, 255);
  line(xPos, height, xPos, height - inByte);

  // at the edge of the screen, go back to the beginning:
  if(xPos >= width){
    xPos = 0;
    background(0);
  }else{
    // increment the horizontal postion:
    xPos++;
  }
}

void serialEvent (Serial myPort){

  // get ASCII string:
  String inString = myPort.readStringUntil('\n');

  if(inString !=null){

    // trim off any whitespace:
    inString = trim(inString);

    // convert to an int and map the the screen height:
    inByte = float(inString);
    println(inByte);
    inByte = map(inByte, 0, 1023, 0, height);
  }

}

I know that the value's being displayed in the output window are 1024'th's of the actual voltage so a value of 512 = 2.5v. What I would like though is for that value to be displayed on the x axis of the graph in the form of a scale. I want to add x and y axis labels to my graph.

For instance, the y label could be 0-1000 and the x label would be in seconds. I think the y axis shouldn't be to hard to include because its constant not sure how to edit this output graph though.. is it some kind of GUI? Also, would it even be possible to add an x axis label that scales itself as time goes on?

Maybe I should just forget about x axis labeling or maybe I will need to keep track of the information and send it to a file to be graphed later??

Not entirely sure how I should go about the project from here, any advice? I do know that eventually I am going to need at least a y axis label because I want to detect changes in salinity over time from saline rich water to less saline rich water (I will poor in fresh water). My salinity detector will just be two nails spaced about 1 cm apart and will be connected to the circuit in the same configuration as my photo-resistor circuit. For now, if I could just get a y label and a title to the graph that would be good.

Answers

  • Anyone?

  • You're not getting a great response because, well, we don't have whatever sensor it is you're using, so to even start seeing what your Processing sketch looks like, we'd need to strip out all the Serial stuff, and fake getting the data some other way, probably using random. That is a lot of work up front to even begin helping you.

    You certainly can draw your own axis. Just use line(). You can put labels in with text() too. Keep in mind that you will want to redraw the whole graph every frame, so you will need some sort of data structure to record previous values.

  • int xPos = 1;
    float inByte = 150;
    
    void setup() {
      size(400, 300);
      background(0);
    }
    
    void draw() {
      stroke(127, 34, 255);
      line(xPos, height, xPos, height - inByte);
    
      if (xPos >= width) {
        xPos = 0;
        background(0);
      } else {
        xPos++;
      }
    
      fakeSerialEvent ();
    }
    
    void fakeSerialEvent () {
      inByte += random(-4,4);
      inByte = constrain(inByte,0,height);
    }
    
Sign In or Register to comment.