Multiple values chart issue.

edited March 2017 in Arduino

ok, so I got quite a lot more code now, and in the midst of trying to make this graph display 6 sensors worth of data, I've gotten stuck on the part where it strips the string back down into 6 parts(after the arduino put them together, I have tested, and the arduino side works fine.) I think that part actually may work, but I'm confused as to how to get the stripped string into my existing x/y display. if what I've messed up is obvious, please just point out where the problem is, not how to fix it.

the basic idea is just a 6 channel datalogger- I've not gotten to the saving parts, I'm still on the displaying live data side of things.

1st data feed is in Hz Next 4 are just 0-5v Last one is Digital in

------------------Arduino code------------------

    const uint8_t turboSpdPin = 5; //TurboSpeed Input Digital Pin
    const uint8_t map1Pin = A0; // Map Sensor 1 Input Analog pin
    const uint8_t map2Pin = A1; // Map Sensor 2 Input Analog pin
    const uint32_t oneSecond = 1000000; //Meassured time
    uint32_t timer = 0;
    uint32_t sts = 0;
    const uint32_t c = 1; // wait for 3000 pulses
    uint32_t ets = 0;
    int turboSpdPinState = LOW;
    int turboSpd = 0;
    int prevturboSpdPinState = LOW;
    int map1inputValue = 0;  //set sensor value to 0 
    int map2inputValue = 0;  //set sensor value to 0
    int aux1Value = 0; // set start value to 0
    int aux2Value = 0; // set start value to 0
    const int Aux1 = A2; //Define Aux Input Pin number as Analog 2
    const int Aux2 = A3; //Define Aux Input Pin number as Analog 3
    const int extPwr = 5;
    const int turbineActivity = 6;
    const int map1connected = 7;
    const int map2connected = 8;
    const int pwrOn = 9;

    void setup()
     pinMode(turboSpdPin, INPUT); //Declared Digital Pin 5 as Input
    void loop() {

      map1inputValue = analogRead(map1Pin);   //read map sensor input
      map2inputValue = analogRead(map2Pin);   //     " "
      aux1Value = analogRead(Aux1);
      aux2Value = analogRead(Aux2);
      sts = micros(); // start time stamp
       for (uint32_t i=c; i>0; i--)

     ets = micros(); // end time stamp
     Serial.println((c*1e6/(ets-sts))); // output Hz

-------------------Processing Code---------------------

    void setup () {
      // set the window size:
      size(1024, 300);
    plot = new GPlot(this, 0, 0, 1024, 300);
      // List all the available serial ports
      println(Serial.list ());
      myPort = new Serial(this, Serial.list()[1], 115200); //open COMport(3) on PC
      myPort.bufferUntil('\r'); // don't generate a serialEvent() unless you get a carriage return
      background(0);    // set inital background:
    void draw () {
      // draw the line:
      stroke(lastyPos,205, 0);
      line(lastxPos, height-(lastyPos+20), xPos, height - (inByte+20));
      lastxPos = xPos;
      lastyPos =inByte; // (int)inByte;

      if (xPos >= width) {                                      // at the edge of the screen, go back to the beginning:
       inByte=0;                                                //drop x/y to bottom of window
        line(lastxPos, height-lastyPos+20, xPos, height-20);    // bring x/y back to start of window

        xPos = 20;               //reset
        lastxPos = 20;           //reset
      } else {     
        xPos =xPos + 3;   // increment the horizontal position:

    void serialEvent (Serial myPort) {

      String inString = myPort.readStringUntil('\r');    // read serial buffer:
      if (inString != null) {
           inString = trim(inString);          // trim off any whitespace:
       //next 4 lines worked as one data feed, everything after that was (i think ) added from another sketch to try to make a 6 value feed. I am stuck here.

       // convert to an int and map to the screen height:
       // inByte = float(inString);
       // println(inByte);
       // inByte = map(inByte, 0, 1023, 0, height-20);
       int mysensors[] = int(split(inString, '\t'));
        count = mysensors.length;
        for (int i=0; i<count; i++)  //
          // set sensor[i] value for use in draw()
          sensors[i] = mysensors[i];   //<--- you'll need to change this line to put
                                                 //    the values where you want them
          // print out the values we got:
          print(i + ": " + sensors[i] + "\t");


  • cool, thanks. Processing is now seeing and displaying the correct values in the serial window/debug window, but when I try to plug the values into the 'line' function the IDE says ' No fool, you can't cast int[] into int, no soup for you!' This makes me sad. I want soup.

  • can't cast int[] into int, ...

    An array's element needs to be accessed using operator []:

    Or iterate over it via for ( ; ; ) or for ( : ) loops:

  • I've had to leave it for a bit, health issues are making coding time scarce. thanks for your help, and I'll fire this up again as soon as I possibly can.

  • edited March 2017

    back again! ok, so i have figured out(in my head) how to make this work, but I have a small problem. I have the serial feed being split into 6 separate numbers, and then being drawn in a line from the 6 previous numbers;

    the problem is this

    void serialEvent(final Serial s) {
          vals = int(splitTokens(s.readString()));
          lastyPos1 = (vals[0]);         // the placeholder lastyPos means "previous 'y' 
          lastyPos2 = (vals[1]);         // position, extrapolated from the last serial event"
          lastyPos3 = (vals[2]);         
          lastyPos4 = (vals[3]);
          lastyPos5 = (vals[4]);
          lastyPos6 = (vals[5]);
          redraw = true;

    My issue is i can't see how to define the lastyPos1 to lastyPos6 tags for the first round of drawing. I know that this would be done in 'void setup' but unsure of the syntax needed

    cheers Jason

  • Press the gear icon to edit your post. Select your code and press ctrl + o. Leave a line above and below the code.


  • edited March 2017

    Sorry,Apparently you can't edit code once you've done the CTRL-O....seems to undo it for some reason.

  • any Idea?

  • Answer ✓

    Ok, so if I understand, you want to plot 6 lines related to each sensor? I didn't study your full code but based on your last post...

    I will have lastyPos and curryPos, curr is the short of current. In setup you init all your lastypos and currypos as 0. When serialEvent is executed, it updates curryPos variables. In draw, you draw the line from prevyPos to currYpos followed by lastyPos=curryPos so now lastypos stores the prev position and curryPos stores the latest position read by serial event. See below. Code is not tested btw.

    Also check latest code provided by Trinityin next post, her entry on Feb27th:


    int lasyPos=0;
    int curryPos=0;
    int x=0;
    int[] vals;
    void setup(){
    void draw(){
    void serialEvent(final Serial s) { 
      vals = int(splitTokens(s.readString()));
     if(vals.length != 0)
        curryPos = (vals[0]);
  • edited March 2017

    You understand completely!, I was unsure I was clear enough. what then would happen before a serial event is executed? I think that is where my issue lies, the draw function doesn't appear to like having a null figure for curryPos. (i see that it is not null in your code, but mine doesn't like it being defined as an Int, then using the string deconstructed equivalent)

  • Answer ✓

    Before any serial event, it will have a value of zero. This is why I have lines 1 and 2 set to zero.

    You could also add noLoop()in setup and redraw() in serialEvent. Then your draw is updated only when you get serial events. There are pros and cos about this approach.


  • Excellent!, thanks a bunch

Sign In or Register to comment.