Use time as x-axis in plotting values from arduino

edited February 2015 in Arduino

I have written a code in arduino which reads two variable values.Now I want to plot one of them vs time in processing. The value from arduino should be on y axis and time should be on the x axis of the plot.The unit of time that i want is 50ms. Can anyone please help me out with setting up time as the x -axis. Here is code for both arduino and pprocessing.

Arduino Code 
            void setup()
            {
             Serial.begin(9600);
            }
            void loop()
            {
            int xPot = analogRead(4);
            int yPot = analogRead(5);
            Serial.print(xPot);
            Serial.print("\t");
            Serial.println(yPot);
             delay(50);
            }

Processing Code
          import processing.serial.*;
                Serial myPort;


                void setup() {
       size(512, 512);
       println(Serial.list());  //list of available serial ports
        String portName = Serial.list()[0]; //replace 0 with whatever port you want to use.
         myPort = new Serial(this, portName, 9600);
         background(0);


       }


                void draw() {
                  while (myPort.available () > 0) {
             String inString = myPort.readStringUntil('\n');
             if (inString != null) {
            inString = trim(inString);
             String[] xyRaw = splitTokens(inString, "\t");
             if (xyRaw.length == 2) {
             int x = int(xyRaw[1]);

                  println(x);
                  stroke(255, 255, 0);
                   line(frameCount-1, x, frameCount, x);
                  if (frameCount >=width) {
                    frameCount=0;
                   background(0);
                  }

                }

             }
                  }
                }
Tagged:

Answers

  • Break this problem down into smaller steps. Which half of it is giving you trouble: getting your values from arduino into an array or ArrayList? Or using that array or ArrayList to plot points in a graph in Processing?

    Create an MCVE that focuses on only the half that you're stuck on. If you're stuck on getting the values form arduino, then don't bother with any graphing code. If you're stuck on graphing the values, then hardcode them in an array or ArrayList and only post the graphing code.

  • I am not having any troubles with getting the values from arduino. I just want to use time as the x axis on the plot

  • Cool. Then I suggest creating an MCVE that uses a hard-coded array of values to create a plot on the X axis. We'll go from there.

Sign In or Register to comment.