2D Array with Shift

edited October 2017 in Arduino

I have several sensors on my arduino that I would like to log

Data comes from the Arduino in the form of a string and is formatted in the following manner (there are 6 fields)

"10.2", "123.33", "0.00", "1023.00", "99.01", "256.9"/r

in processing I am reading the string then splitting and converting it in to a float and that seems to be working fine.

What I need to do is store the information in an array so I can output the information however I also need to be able to shift the last readings in and out as I would like to keep track of the last 60 values for each sensor.

for example

{0}, {0, 1, 2, 3, -> whatever the incoming values are etc-> 58, 59, 60};

When a new value comes in the new values is added and shifted to the left.

{0}, {1, 2, 3, 4, -> whatever the incoming values are etc-> 59, 60, 61};

Ideally i would like to do this without using loops as some of the other functions are kind of time critical.

I hope that made sense and thanks in advance for any assistance or ideas on this.

Answers

  • Answer ✓
    class DataSet {
      float[] data = new float[6];
      DataSet(float a, float b, float a, float b, float a, float b ){
         data[0] = a;
         data[1] = b;
         data[2] = c;
         data[3] = d;
         data[4] = e;
         data[5] = f;
      }
    }
    
    ArrayList<DataSet> lastSixty = new ArrayList();
    
    // Add new data to end:
    lastSixty.add( new DataSet( 1, 2, 3, 4, 5, 6 ) );
    
    // Remove oldest data:
    lastSixty.remove(0);
    
    // Maintain sixty data:
    while( lastSixty.size() > 60 ){ lastSixty.remove(0); }
    
    // Access the 5th most recent record's third data:
    println( lastSixty.get(lastSixty.size() - 5 ).data[2] );
    
  • Thanks @TfGuy44 will try that out. looks a lot less complex than the way I was trying to do it.

  • Not sure if i'm holding my tongue on the right angle for this one..

    Seems that no matter what I add in to lastSixty the length is allays 1.

    `class DataSet { float[] data = new float[6]; DataSet(float a, float b, float c, float d, float e, float f ){ data[0] = a; data[1] = b; data[2] = c; data[3] = d; data[4] = e; data[5] = f; } } ArrayList lastSixty;

    void setup() { size(640, 360); lastSixty = new ArrayList();

    // Add new data to end: lastSixty.add(new DataSet(1, 2, 3, 4, 5, 6)); }

    void draw() {

    //ArrayList lastSixty = new ArrayList(); // Remove oldest data: //lastSixty.remove(0);

    // Maintain sixty data: //while( lastSixty.size() > 60 ){ lastSixty.remove(0); }

    println(lastSixty.size()); // println(lastSixty.get(2));

    // Access the 5th most recent record's third data: //println( lastSixty.get(lastSixty.size() - 5 ).data[2] ); }`

  • Doh... Scratch that... I just realised I was looking at the array the wrong way around... Typical..!! guess that's what you get from being on the bottom side of the planet :)

  • Australia?

  • @Chrisir Close, but a little Further down :P New Zealand :)

  • edited October 2017 Answer ✓

    You make your life easier when you tell the ArrayList its class.

    Greetings to New Zealand

    Chrisir ;-)

    class DataSet { 
    
      float[] data = new float[6];
    
      //constructor 
      DataSet(float a, float b, float c, float d, float e, float f ) { 
        data[0] = a; 
        data[1] = b; 
        data[2] = c; 
        data[3] = d; 
        data[4] = e; 
        data[5] = f;
      }//constructor 
    
      String toString() {
        String result="";
    
        for (float f1 : data ) 
          result+=str(f1)+"; ";
    
        // clean ending: 
        if (result.length()>2) {
          result=result.substring(0, result.length()-2); 
          result+=".";
        }
        return result;
      }//method 
      //
    }//class 
    
    //=======================================================
    
    ArrayList<DataSet> lastSixty;
    
    void setup() { 
      size(640, 360); 
    
      fill(0);
      text("See direct window", 19, 19);
    
      //ArrayList 
      lastSixty = new ArrayList();
    
      // Add new data to end:
      lastSixty.add(new DataSet(1, 2, 3, 4, 5, 6));
      lastSixty.add(new DataSet(21, 22, 23, 24, 25, 26));
      lastSixty.add(new DataSet(41, 42, 43, 44, 45, 46));
      lastSixty.add(new DataSet(61, 62, 63, 64, 65, 66));
      lastSixty.add(new DataSet(71, 72, 73, 74, 75, 76));
    }
    
    void draw() {
    
      // Remove oldest data: 
      // lastSixty.remove(0);
    
      // Maintain sixty data: //
      while ( lastSixty.size() > 60 ) { 
        lastSixty.remove(0);
      }
    
      println("size: "+lastSixty.size());
      println("--------------------------------------");
      println("2nd data set");
      printArray(lastSixty.get(2).data);
      println("Same as: " +lastSixty.get(2).toString()); 
      println("--------------------------------------");
    
      // Access the 2nd most recent record's third data: //
      DataSet d1 = lastSixty.get( lastSixty.size() - 2 ) ;  
      println("Last but 2 data set, item 2: " + d1.data[2] );
    
      noLoop();
    }
    //
    
  • edited October 2017 Answer ✓

    this is with noLoop()... you want to comment that out later

  • Thanks @Chrisir, Yes using a dataset really did make it so much easier :) where are you from?

  • Answer ✓

    using a dataset really did make it so much easier

    That's this line:

    ArrayList<DataSet> lastSixty;
    

    (just to make sure we are on the same page ;-) )

    I am from Germany

  • :) We are in the same memory space. Thanks

  • @Chrisir is there a way to get the size of data[] to see how many sensors are in there? kind of like a println(data.size())?

  • data.length

    More in the reference: https://processing.org/reference/Array.html

    Kf

Sign In or Register to comment.