save and load Array list

2»

Answers

  • and the sketch is now too long to post in one go

    this is so lame............................

  • I made a new version that can display the file name for each column

  • Thanks Chrisir ..... you know a more than wikipedia ..... you're teaching me a lot ......

  • you are not making progress

    only I make progress

    can you read arduino data and fill in table 1 please?

  • Technically our (my) code is not good, because we have very similar sections that could be unified and made much better.

    E.g. we have 8 buttons now, 4 on the right side (Load etc.) and 4 buttons above the 4 columns. But we don't have a nice structure to check them for mouse click.

    Look at this code, there are a lot of repetitions going on:

    // functions to register if mouse is over buttons / funzione da registrare se il mouse è sui pulsanti
    
    boolean overSave() {            // modificato
      return mouseX > width-50 && 
        mouseY > height-740  && 
        mouseY < height-740+25 ;
    }
    
    boolean overLoad() {                 //modificato
      return mouseX > width-50 && 
        mouseY > height-710  && 
        mouseY < height-710+25 ;
    }
    
    boolean overNew() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-680  && 
        mouseY < height-680+25 ;
    }
    
    boolean overHelp() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-650  && 
        mouseY < height-650+25 ;
    }
    

    Its almost the same 4 times:

    boolean over_<A Button>_() {                         //modificato
      return mouseX > _<x position>_ && 
        mouseY > _<y position>_  && 
        mouseY < _<y position>_+_<button height (25)>_ ;
    }
    

    Instead it would be cool to have a list of buttons and say something like

    for (Button btn : buttons) // for all our buttons do something
         if (mouseIsOver btn) 
               do button btn command; 
    }
    

    That's pseudo code obviously but it's much shorter.

    At the moment you just have numbers like height-650. Now you could put them in variables like xpositon1. Then you could have one list for the x position of all buttons, one list of the y positions of all buttons, the heights and so on.

    Then there is the concept of object oriented programming (OOP) which allows us to define what ONE button is (seen in an abstract way), a x position, y position, widthButton and heightButton ajnd its text (caption, "Load" etc.) and it should be able to register the mouse on it of course. Such a package is called a class.

    Then we can make a list (array) of that Button class. Very cool.

    To dive into it read:

    https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

    https://www.processing.org/tutorials/objects/

    Chrisir ;-)

  • // https : // forum.processing.org/two/discussion/26719/save-and-load-array-list#latest
    
    import processing.serial.*; 
    import controlP5.*;   
    import grafica.*;
    
    Serial myPort;      // The serial port: 
    ControlP5 controlP5;   // controlP5 object
    Knob myKnob1, myKnob2, myKnob3, myKnob4;  // knobs 
    
    // buttons above columns
    int[] arrayOfXValuesFrom = { 13, 220, 390, 570 };  
    int[] arrayOfXValuesTo = { 193, 340, 550, 710 };
    
    // text for file names for each column 
    String[] fileNames = { "not a file", 
      "not a file", 
      "not a file", 
      "not a file" };  
    
    // 4 columns 
    ArrayList<PVector> points1 = new ArrayList(); // 1st 
    ArrayList<PVector> points2 = new ArrayList(); // 2nd
    ArrayList<PVector> points3 = new ArrayList(); // 3rd 
    ArrayList<PVector> points4 = new ArrayList(); // 4th
    
    // active column 
    int activeArrayListHasNumber = 0;  
    
    // some flags for texts 
    boolean showHelpText1 = false; 
    boolean showText = true; 
    
    String inString;  // Input string from serial port: 
    int lf = 10;      // ASCII linefeed 
    float deg, val;
    
    final String pathFolder="";//CARTELLA DI PERCORSO
    final String fileExtension = ".txt";//estensione file
    
    // unique numbers for constants: 
    final int normal = 0;
    final int save   = 1;
    final int load   = 2;
    final int help   = 3;
    /// current state (must be one of them) 
    /// stato attuale (deve essere uno di loro)
    int state=normal;
    
    // Paths 
    String savePath=""; 
    String loadPath=""; 
    
    // --------------------------------------------------------------------------
    
    void setup() {
      size(1350, 750);
    
      // not necessary
      for (int i = 0; i < 3; i++) { 
        points1.add(new PVector(i, 20 + 10*noise(i*0.1)));
      }//for
      for (int i = 0; i < 13; i++) { 
        points2.add(new PVector(i, 30 + 10*noise(i*0.1)));
      }//for
      for (int i = 0; i < 5; i++) { 
        points3.add(new PVector(i, 40 + 10*noise(i*0.1)));
      }//for
      for (int i = 0; i < 7; i++) { 
        points4.add(new PVector(i, 50 + 10*noise(i*0.1)));
      }//for
    
      //ControlP5
      PFont  f= createFont ("Georgia", 25);
      {
        controlP5 = new ControlP5(this);   //ControlP5
    
        myKnob1 = controlP5.addKnob(" Massa ARIA")
          //.setRange(0,1024)
          .setRange(0, 5)
          .setValue(0)
          .setPosition(width-370, height-185)//posizione 605
          .setTickMarkLength(10)
          .setColorForeground(color(#FF0004))//colore cursore
          .setColorBackground(color(255))//colore sfondo
          .setColorValueLabel(color(0))//colore numeri//label etichetta
          .setColorActive(color(#6AFA05))
          .setDragDirection(Knob.VERTICAL)
          //  . setColorCaptionLabel(0) //colore scritta(capition)
          .lock()
          .setSize(150, 150)//dimensioni
          // .setScrollSensitivity(120)
          . setDecimalPrecision(2) //numero dopo la virgola
          ;
        myKnob1 .getCaptionLabel()
          . setFont(f);
        myKnob1.getValueLabel()
          .setFont(f)
          ;
        ;
    
        myKnob2 = controlP5.addKnob("P. Cil")
          .setRange(0, 500)
          .setValue(0)
          .setPosition(width-370, height-380)
          .setColorForeground(color(#FF0004))//colore cursore
          .setColorBackground(color(255))//colore sfondo
          .setColorValueLabel(color(0))//colore numeri//label etichetta
          .setColorActive(color(#6AFA05))
          .setDragDirection(Knob.VERTICAL)
          // . setColorCaptionLabel(0) //colore scritta(capition)
          .lock()
          .setSize(150, 150)//dimensioni
          // .setScrollSensitivity(120)
          .  setDecimalPrecision(2) //numero dopo la virgola
          .setTickMarkLength(10) 
          ;
        myKnob2 .getCaptionLabel()
          . setFont(f);
        myKnob2.getValueLabel()
          .setFont(f)
          ;
    
        myKnob3 = controlP5.addKnob("P. Atm ")
          //.setRange(0,1023)
          .setRange(0, 500)
          .setValue(1)
          .setPosition(width-180, height-380)
          .setColorForeground(color(#FF0004))//colore cursore
          .setColorBackground(color(255))//colore sfondo
          .setColorValueLabel(color(0))//colore numeri//label etichetta
          .setColorActive(color(#6AFA05))
          .setDragDirection(Knob.VERTICAL)
          //   . setColorCaptionLabel(0) //colore scritta(capition)
          .lock()
          .setSize(150, 150)//dimensioni
          .setDecimalPrecision(2) //numero dopo la virgola
          .setTickMarkLength(10)
          ;
        myKnob3 .getCaptionLabel()
          . setFont(f);
        myKnob3.getValueLabel()
          .setFont(f)
          ;
    
        myKnob4 = controlP5.addKnob("temp")
          //.setRange(0,1023)
          .setRange(0, 500)
          .setValue(1)
          .setPosition(width-180, height-185)
          .setColorForeground(color(#FF0004))//colore cursore
          .setColorBackground(color(255))//colore sfondo
          .setColorValueLabel(color(0))//colore numeri//label etichetta
          .setColorActive(color(#6AFA05))
          .setDragDirection(Knob.VERTICAL)
          //   . setColorCaptionLabel(0) //colore scritta(capition)
          .lock()
          .setSize(150, 150)//dimensioni
          .setDecimalPrecision(2) //numero dopo la virgola
          .setTickMarkLength(10)
          ;
        myKnob4 .getCaptionLabel()
          . setFont(f);
        myKnob4.getValueLabel()
          .setFont(f)
          ;
      }
    
      //  myPort = new Serial(this, "com3", 9600);  // ?????
      //  myPort.bufferUntil(lf);
    }
    
    void draw() {
    
      switch (state) {
    
      case normal:
        drawForStateNormal() ;
        break; 
    
      case save:
        // wait for Save Dialog 
        waitForSaveDialog();
        break; 
    
      case load:
        // wait for Load Dialog 
        waitForLoadDialog();
        break;
    
      case help:
        // help
        drawForStateHelp() ;
        break; 
    
      default:
        //Error 
        println("Fail 192; unknown state: "
          +state);
        exit();
        break; 
        //
      } //switch
      //
    }//func
    
    // ------------------------------------------------
    
    void drawForStateNormal() {
    
      background(245);
      {
        strokeWeight(2);   
        stroke(#FF0B03);
        fill(0);
        rect(width-380, height-490, 360, 90, 20);// retangolo diff. press.
        fill(255);
        textSize(28); 
        text("Diff.Press.", width-270, height-405);     //diff.press
        textSize(40);
        text(( myKnob2.getValue()- myKnob3.getValue()), width-270, height-445);     //diff.press
      }
      {
        fill(0);
        strokeWeight(2);   
        stroke(#FF0B03);
    
        rect(width-380, height-390, 360, 190, 20);// retangolo press
        rect(width-380, height-195, 360, 190, 20);// rettangolo massa Temp
      }
      textSize(14);
      showData1();
      showData2();
      showData3();
      showData4();
    
      // title 
      fill(255, 2, 2);
      textAlign(CENTER);
      text("My little\nGraph program", 
        width-223, 19, 130, 422);
      //reset 
      textAlign(LEFT);
    
      //// help
      if (showHelpText1) {
        float widthOfBox = 210; 
        fill(240, 544, 2); // yellow 
        stroke(0);         // black 
        rect(420, 27, widthOfBox, 322);
        fill(0);           // black 
        text("Use mouse above columns (or 0..3) to set the active table "
          +"which is marked by the horizontal line on top.\n\n"
          +"New, Load and Save are referring to the active table (and Backspace).\n"
          +"m to hide / show text data.\n\nHit x to hide / show this text.", 
          425, 30, widthOfBox-10, 522);
      }
    
      // ----------------------
      // buttons
      showButtons();
    
      // gray lines above columns 
      for (int i=0; i < arrayOfXValuesFrom.length; i++) {
        if (activeArrayListHasNumber!=i) {
          stroke(111); //
          strokeWeight(2.8); 
          line (arrayOfXValuesFrom [i], 10, 
            arrayOfXValuesTo[i], 10);
          strokeWeight(0.8); 
          line (arrayOfXValuesFrom [i]+10, 14, 
            arrayOfXValuesTo[i]-10, 14);
          line (arrayOfXValuesFrom [i]+10, 10-4, 
            arrayOfXValuesTo[i]-10, 10-4);
        }//if 
        fill(0);
        text (fileNames[i], 
          arrayOfXValuesFrom [i], height-18);
      }//for 
    
      // reset
      strokeWeight(1.0); 
      //
    } //draw
    
    void drawForStateHelp() {
    
      // Help
    
      background(245);
      myKnob1.setVisible(false);
      myKnob2.setVisible(false);
      myKnob3.setVisible(false);
      myKnob4.setVisible(false); 
    
      fill(255, 2, 2);
      text("My little Graph program\n\nUse buttons on the right and below;\n"    //testo e posizione testo
        +"use Backspace to delete last data point of each list.\n\n" 
        +"Use 1 and 2 to set the active table which is marked by the horizontal line on top.\n"
        +"New, Load and Save are referring to the active table (and also Backspace).\n\n"
        +"You can click now above each column to activate it.\n"
        +"You can press m to hide/show data text and x to show / hide quick help."
        +"\n\nHit any key to return to main program.", 
        113, 122, width-200, 422);
    }
    
    // --------------------------------------------------
    // functions to show buttons funzione per mostrare i bottoni
    
    void showButtons() {
      //
      strokeWeight(2);   
      stroke(0);
      textSize(15);
    
      fill(0);
      if ( overSave() ) {
        fill(#FF0303);
      }
    
      rect(width-50, height-740, 40, 20, 5);// modificato posizione bottone "salve"
      fill(255); 
      text("Save", 
        width-50+2, height-740+9+6);
    
      // ---
    
      fill(0);
      if ( overLoad() ) {
        fill(#49C3FA);
      }
    
      rect(width-50, height-710, 40, 20, 5);// modificato posizione" load"
      fill(255); 
      text("Load", 
        width-50+2, height-710+9+6);
    
      // ---
    
      fill(0);
      if ( overNew() ) {
        fill(#1FFF03);
      }
    
      rect(width-50, height-680, 40, 20, 5);// modificato posizione "new"
      fill(255); 
      text("New", 
        width-50+4, height-680+9+6);
    
      // ---
    
      fill(0);
      if ( overHelp() ) {
        fill(#1AAF03);
      }
      rect(width-50, height-650, 40, 20, 5);// modificato posizione "help"
      fill(255); 
      text("Help", 
        width-50+4, height-650+9+6);
    }
    
    //----------------------------------------------------------------------------
    // functions to register if mouse is over buttons funzione da registrare se il mouse è sui pulsanti
    
    boolean overSave() {            // modificato
      return mouseX > width-50 && 
        mouseY > height-740  && 
        mouseY < height-740+25 ;
    }
    
    boolean overLoad() {                 //modificato
      return mouseX > width-50 && 
        mouseY > height-710  && 
        mouseY < height-710+25 ;
    }
    
    boolean overNew() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-680  && 
        mouseY < height-680+25 ;
    }
    
    boolean overHelp() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-650  && 
        mouseY < height-650+25 ;
    }
    
    // ---------------------------------------------------------------------------
    // Inputs 
    
    void keyPressed() {
    
      if (state==help) {
        goBackToMainState();
        return;
      }
    
      if (state!=normal)
        return;
    
      //  
      if (  keyCode == BACKSPACE ) {
        //
        backspaceOnActiveList();
        //
      } 
      // ----
      else if ( keyCode == DELETE ) {
        //
        // ignore 
        //
      } else if  ( keyCode == ESC ) {
        //
        // If yellow help box is ON  
        if (showHelpText1) {
          showHelpText1 = false; // yellow help box off
        } else {
          // else toggle
          showText = !showText; // data text / tables
        }//else 
        // kill Esc so we stay in the program 
        key=0;
        //
      } 
    
      // ------
    
      else {
        if ( key != CODED ) {
          //
          switch(key) {
    
          case '0':
            activeArrayListHasNumber = 0; 
            break;
    
          case '1':
            activeArrayListHasNumber = 1; 
            break;
    
          case '2':
            activeArrayListHasNumber = 2; 
            break;
    
          case '3':
            activeArrayListHasNumber = 3; 
            break;
    
          case 'x':
            showHelpText1 = !showHelpText1; // yellow help box 
            break;
    
          case 'm':
            showText = !showText; // data text / tables 
            break;
            //
          }//switch
          //
        }//if
      }//else
    }// func 
    
    void mousePressed() {
    
      if (state==help) {
        goBackToMainState();
        return;
      }
    
      if (state!=normal)
        return;
    
      // for the buttons 
      if ( overSave() ) {
        initSave();
      }
      //---
      else if ( overLoad() ) {
        initLoad();
      } else if ( overHelp() ) {
        state=help;
      }
      //---
      else if ( overNew() ) {
        //
        if ( activeArrayListHasNumber==0 ) {
          points1.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==1 ) {
          points2.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==2 ) {
          points3.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==3 ) {
          points4.clear();            //cancellare punti con new
        }// else if
    
        // set name 
        fileNames[activeArrayListHasNumber] = "not a File"; 
        //
      }
      //---
    
      boolean done = false; 
    
      if (mouseY<25) {
        for (int i=0; i < arrayOfXValuesFrom.length; i++) {
          if (mouseX>arrayOfXValuesFrom[i] && 
            mouseX<arrayOfXValuesTo[i]) { 
            //
            activeArrayListHasNumber=i;
            done = true; 
            break;
          }//if
        }//for
      }//if
    
      if (!done) {
        // points.add(new PVector(points.size(), mouseY/10));
        if ( activeArrayListHasNumber == 0 ) {
          // points1.add(new PVector(points1.size(), myKnob1.getValue()));
          points1.add(new PVector(points1.size(), mouseY));
        } else if ( activeArrayListHasNumber == 1 ) {
          //points2.add(new PVector(points2.size(), myKnob1.getValue()));
          points2.add(new PVector(points2.size(), mouseY));
        } else if ( activeArrayListHasNumber == 2 ) {
          //points2.add(new PVector(points2.size(), myKnob1.getValue()));
          points3.add(new PVector(points3.size(), mouseY));
        } else if ( activeArrayListHasNumber == 3 ) {
          //points2.add(new PVector(points2.size(), myKnob1.getValue()));
          points4.add(new PVector(points4.size(), mouseY));
        }
      }
    }//func
    
    // -------------------------------------------------
    // Save and load 
    
    void initSave() {
      // init save process 
      // reset
      savePath=""; 
      // make date time stamp (the expression nf(n,2) means leading zero: 2 becomes 02)
      String dateTimeStamp = year() 
        + nf(month(), 2) 
        + nf(day(), 2) 
        + "-"
        + nf(hour(), 2)
        + nf(minute(), 2)
        + nf(second(), 2); 
      // prepare fileDescription which occurs in the dialogue
      // prepara la descrizione del file che avviene nel dialogo
      File fileDescription = new File( sketchPath()
        + "//"
        + pathFolder 
        + "//"
        + dateTimeStamp
        + fileExtension); 
      // open the dialog  
      selectOutput("Select a file to write to", "fileSelectedSave", fileDescription); 
      // set state to wait
      state=save;
    }
    
    void initLoad() {
      // init load process 
      // reset
      loadPath=""; 
      // prepare fileDescription which occurs in the dialogue
      File fileDescription = new File( sketchPath()+"//"+pathFolder+"//"+"*" + fileExtension ); 
      // open the dialog
      selectInput("Select a file to load", "fileSelectedLoad", fileDescription); 
      // set state to wait
      state=load;
    }
    
    void fileSelectedSave(File selection) {
      // the 'callback' function
      if (selection == null) {
        // println("Window was closed or the user hit cancel.");
    
    
        // println ("La finestra è stata chiusa o l'utente ha premuto annulla.");
        // torna indietro
    
        state=normal; // go back
      } else {
        // println("User selected " + selection.getAbsolutePath());
        savePath=selection.getAbsolutePath();
      }
    }
    
    void fileSelectedLoad(File selection) {
      // the 'callback' function
      if (selection == null) {
        // println("Window was closed or the user hit cancel.");
    
        // go back 
        state=normal;
      } else {
        // println("User selected " + selection.getAbsolutePath());
        loadPath=selection.getAbsolutePath();
      }
    }
    
    void waitForSaveDialog() { 
      if (!savePath.equals("")) {
        // waiting is over
        saveIt(); 
        // go back 
        state=normal;
      }
    }
    
  • void waitForLoadDialog() { 
      if (!loadPath.equals("")) {
        // waiting is over
        loadIt(); 
        // go back 
        state=normal;
      }
    }
    
    void saveIt() {
      // save
    
      // make array (to save it)
      String[] strs; 
      strs = new String[0]; 
    
      if (activeArrayListHasNumber==0) {
        strs = new String[points1.size()]; 
        int i=0; 
        for (PVector pv : points1) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }// 
      else if (activeArrayListHasNumber==1) {
        strs = new String[points2.size()]; 
        int i=0; 
        for (PVector pv : points2) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
      else if (activeArrayListHasNumber==2) {
        strs = new String[points3.size()]; 
        int i=0; 
        for (PVector pv : points3) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
      else if (activeArrayListHasNumber==3) {
        strs = new String[points4.size()]; 
        int i=0; 
        for (PVector pv : points4) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
    
      // check if file extension (fileExtension, e.g. .txt) is there 
      int len = savePath.length(); 
      if (len<4 || !savePath.substring( len-4 ).equals(fileExtension)) {
        // file Extension is not present, we have to add it
        savePath += fileExtension; // add the file Extension
      } 
    
      // save 
      println("Saved: " + savePath); 
      saveStrings( savePath, strs );
    
      // get file name for list 
      fileNames[activeArrayListHasNumber] =  nameFromPath(savePath);
    } //func
    
    void loadIt() {
      // load
      if (activeArrayListHasNumber==0)
      {
        points1.clear();
      } else if (activeArrayListHasNumber==1)
      {
        points2.clear();
      } else if (activeArrayListHasNumber==2)
      {
        points3.clear();
      } else if (activeArrayListHasNumber==3)
      {
        points4.clear();
      }
    
      fileNames[activeArrayListHasNumber] =  nameFromPath(loadPath); 
    
      String[] strs = loadStrings( loadPath ); 
      for (String s : strs) {
        String[] thisLine=split(s, ","); 
    
        if (activeArrayListHasNumber==0) 
        {
          points1.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==1)
        {
          points2.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==2)
        {
          points3.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==3)
        {
          points4.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        }//else
        //
      }//for
    }//func 
    
    // -------------------------------------------------
    
    void showData1() {
      //
      // show data 1
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
    
      if (activeArrayListHasNumber==0) {
        stroke(255, 2, 2); // RED
        line (10, 10, 198, 10);
      }
    
      for (PVector pv : points1) {
    
        if (showText) {
          fill(255, 2, 2); // RED 
          // show data
          text(pv.x, 30, y); 
          text(pv.y, 110, y);
        }
    
        fill(0, 2, 2); 
        stroke(255, 2, 2); // RED 
        float yvalue = map( pv.y, 0, height, 300, 355); 
        if (prev.x!=-1) {
          line(10 + pv.x*3, yvalue, 
            prev.x, prev.y);
        }
        noStroke(); 
        // ellipse (10 + pv.x*3, yvalue, 4, 4);
        prev = new PVector(10 + pv.x*3, yvalue); 
    
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(255, 2, 2); // RED 
        line( 100, 30, 100, height);
    
        // double middle lines!!!   
        stroke(0); // BLACK 
        int x1=200; 
        line( x1-2, 30, x1-2, height); 
        line( x1+2, 30, x1+2, height);
      }
      //
    }
    
    void showData2() {
      //
      // show data 2 
    
      if (activeArrayListHasNumber==1) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(2, 255, 2); // GREEN
        line (200, 10, 
          350, 10);
      }
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points2) {
    
        if (showText) {
          fill(0, 255, 2); // GREEN 
          // show data
          text(pv.x, 210, y); 
          text(pv.y, 210+80, y);
        }
    
        fill(0, 2, 2); 
        stroke(0, 255, 2); // GREEN 
        float yvalue = map( pv.y, 0, height, 300, 355); 
        if (prev.x!=-1) {
          line(10 + pv.x*3, yvalue, 
            prev.x, prev.y);
        }
        noStroke(); 
        //   ellipse (10 + pv.x*3, yvalue, 4, 4);
        prev = new PVector(10 + pv.x*3, yvalue); 
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(0, 255, 2); // GREEN 
        line( 288, 30, 288, height);
    
        // double middle lines!!!   
        stroke(0); // BLACK 
        int x1=210+80+80; 
        line( x1-2, 30, x1-2, height); 
        line( x1+2, 30, x1+2, height);
      }
      //
    }
    
    void showData3() {
      //
      // show data 3
    
      color col3 = color(0, 2, 255); // BLUE 
    
      if (activeArrayListHasNumber==2) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(col3); // 
        line (400, 10, 
          550, 10);
      }
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points3) {
    
        fill(col3); // Blue 
    
        if (showText) {
          // show data
          text(pv.x, 210+180, y); 
          text(pv.y, 210+180+80, y);
        }
    
        fill(0, 2, 2); 
        stroke(col3); // blue 
        float yvalue = map( pv.y, 0, height, 300, 355); 
        if (prev.x!=-1) {
          line(10 + pv.x*3, yvalue, 
            prev.x, prev.y);
        }
        noStroke(); 
        //   ellipse (10 + pv.x*3, yvalue, 4, 4);
        prev = new PVector(10 + pv.x*3, yvalue); 
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(col3); // blue 
        line( 467, 30, 467, height);
    
        // double middle lines!!!   
        stroke(0); // BLACK 
        int x1=210+180+80+80; 
        line( x1-2, 30, x1-2, height); 
        line( x1+2, 30, x1+2, height);
      }
      //
    }
    
    void showData4() {
      //
      // show data 4 
    
      color col4 = color(155, 255, 112); 
    
      if (activeArrayListHasNumber==3) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(col4); // 
        line (570, 10, 
          710, 10);
      }
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points4) {
    
        fill(col4); //  
    
        if (showText) {
          // show data
          text(pv.x, 210+180+180, y); 
          text(pv.y, 210+180+180+80, y);
        }
    
        fill(0, 2, 2); 
        stroke(col4); //  
        float yvalue = map( pv.y, 0, height, 300, 355); 
        if (prev.x!=-1) {
          line(10 + pv.x*3, yvalue, 
            prev.x, prev.y);
        }
        noStroke(); 
        //   ellipse (10 + pv.x*3, yvalue, 4, 4);
        prev = new PVector(10 + pv.x*3, yvalue); 
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(col4); //  
        line( 210+180+180+80-4, 30, 210+180+180+80-4, height);
      }
      //
    }
    
    // ---------------------------------------------------------------
    
    void serialEvent(Serial p) { 
      inString = p.readString(); 
      char primo=inString.charAt(0); // primo carattere
      String cifra=inString.substring(1); // da secondo carattere in poi
      float val=parseFloat(cifra); // valore da 0-255 o 0-1023, non sò cosa spedisce arduino
    
    
      print("val="); 
      println(val); 
    
      //   inString = trim(inString);
    
      //    // converte in int e mappa all'altezza dello schermo: 
    
    
      switch(primo) { 
        case ('A') : 
        myKnob1.setValue(val); 
        break; 
        case ('B') : 
        myKnob2.setValue(val); 
        break; 
        case ('C') : 
        myKnob3.setValue(val); 
        break; 
        case ('D') : 
        myKnob3.setValue(val); 
        break;
      }
    }
    
    void goBackToMainState() {
    
      // from help screen back to main screen
    
      // buttons ON 
      myKnob1.setVisible(true); 
      myKnob2.setVisible(true); 
      myKnob3.setVisible(true); 
      myKnob4.setVisible(true); 
    
      //go back to main screen 
      state=normal;
    
      // kill Esc so we stay in the program 
      key=0;
    }
    
    void  backspaceOnActiveList() {
    
      // for the key BACKSPACE 
    
      switch (activeArrayListHasNumber) {
    
      case 0:
        if (points1.size()>0)
          points1.remove(points1.size()-1);
        break; 
    
      case 1:
        if (points2.size()>0)
          points2.remove(points2.size()-1);
        break;
    
      case 2:
        if (points3.size()>0)
          points3.remove(points3.size()-1);
        break;
    
      case 3:
        if (points4.size()>0)
          points4.remove(points4.size()-1);
        break;
      }//switch
      //
    }//func 
    
    // ------------------------------------------------
    // tools
    
    String nameFromPath(String fileName1) {
    
      File file = new File(fileName1);
      String result = file.getName();
      return result;
    } //func 
    //
    
  • In this new version, file names are written under the 4 columns

    My advice, read careful the entire code, think about what I do there, grab a pen of paper and make some notes about the components of the sketch

  • edited March 2018

    What I said about the buttons being redundant and the necessity to unify them in a list / array is also true for the 4 columns of data : they also share a lot of common code which contradicts the DRY principle of programming: don’t repeat yourself.

  • here is an example of buttons in an Array (list)

    Button[] buttons;
    String status=""; 
    
    void setup() {
    
      size(440, 400);
    
      int unit = 40;
      int count = 440/unit;
      buttons = new Button[count];
    
      for (int x = 0; x < buttons.length; x++) {
        Button newButton = new Button(x*unit+6, 50, 
          25, 55, 
          color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), 
          x);
        buttons[x] = newButton;
      }
    }
    
    void draw() {
    
      background(0, 0, 0); // black 
    
      text (status, 44, height-44); 
    
      for (Button but : buttons) {
        but.update();
        but.display();
    
        fill(0, 255, 0);  // green
        textAlign(CENTER); 
        text(but.index, but.x+but.sizeWidth/2, 40);
        textAlign(LEFT);
      }//for
    }//func 
    
    //_____________________________//
    
    void mousePressed() {
      for (Button but : buttons) {
        but.press();
        if (but.pressed) {
          println(but.index);
          doCommand(but.index);
          break; // Other buttons won't be pressed, just stop
        }
      }
    }
    
    void mouseReleased() {
      for (Button but : buttons) {
        but.release();
      }
    }
    
    void doCommand(int cmd) {
      switch(cmd) {
      case 0:
        status="Interesting Button 0";
        break; 
    
      case 1:
        status="Button 1";
        break; 
    
      case 2:
        status="Now Button 2";
        break; 
    
      default:
        status="Something else...";
        break;
      }//switch
    }//func 
    
    //_____________________________//
    
    class Button {
    
      int x, y; // The x- and y-coordinates
      int sizeWidth, sizeHeight; // Dimension (width and height)
    
      color baseGray; // Default gray value
      color overGray; // Value when mouse is over the button
      color pressGray; // Value when mouse is over and pressed
    
      boolean over = false; // True when the mouse is over
      boolean pressed = false; // True when the mouse is over and pressed
    
      int index;
    
      // constructor 
      Button(int xp_, int yp_, 
        int sizeWidth_, int sizeHeight_, 
        color b_, 
        color o_, 
        color p_, 
        int index_) {
    
        x = xp_;
        y = yp_;
    
        sizeWidth = sizeWidth_;
        sizeHeight = sizeHeight_; 
    
        baseGray = b_;
        overGray = o_;
        pressGray = p_;
    
        index=index_;
      } // constructor 
    
      // Updates the over field every frame
      void update() {
        if ((mouseX >= x) && (mouseX <= x + sizeWidth) &&
          (mouseY >= y) && (mouseY <= y + sizeHeight)) {
          over = true;
        } else {
          over = false;
        }
      }
    
      boolean press() {
        if (over) {
          pressed = true;
          return true;
        } else {
          return false;
        }
      }
    
      void release() {
        pressed = false; // Set to false when the mouse is released
      }
    
      void display() {
    
        if (pressed) {
          fill(pressGray);
        } else if (over) {
          fill(overGray);
        } else {
          fill(baseGray);
        }
    
        stroke(255);
        rect(x, y, sizeWidth, sizeHeight);
      }
    }//class
    //
    
  • Thanks Chrisir, the merit of where I arrived and all yours, I still have to study and apply myself so much ..... This is where I came to create the sketch ......

    // https : // forum.processing.org/two/discussion/26719/save-and-load-array-list#latest
    
    import processing.serial.*; 
    
    Serial myPort;      // The serial port: 
    
    
    // buttons above columns  pulsanti sopra  colonne
    int[] arrayOfXValuesFrom = { 900, 1010,1120, 1230 };  
    int[] arrayOfXValuesTFile = { 275, 305, 335, 365}; 
    int[] arrayOfXValuesTo = { 990, 1100, 1210, 1320 };
    
    // text for file names for each column   // testo per i nomi dei file per ogni colonna
    String[] fileNames = { "not a file", 
      "not a file", 
      "not a file", 
      "not a file" };  
    
    // 4 columns 
    ArrayList<PVector> points1 = new ArrayList(); // 1st 
    ArrayList<PVector> points2 = new ArrayList(); // 2nd
    ArrayList<PVector> points3 = new ArrayList(); // 3rd 
    ArrayList<PVector> points4 = new ArrayList(); // 4th
    
    // active column 
    int activeArrayListHasNumber = 0;  
    
    // some flags for texts 
    boolean showHelpText1 = false; 
    boolean showText = true; 
    
    String inString;  // Input string from serial port: 
    int lf = 10;      // ASCII linefeed 
    float deg, val,MasA,PCil,PAtm;
    
    final String pathFolder="";//CARTELLA DI PERCORSO
    final String fileExtension = ".txt";//estensione file
    
    // unique numbers for constants: 
    final int normal = 0;
    final int save   = 1;
    final int load   = 2;
    final int help   = 3;
    /// current state (must be one of them) 
    /// stato attuale (deve essere uno di loro)
    int state=normal;
    
    // Paths 
    String savePath=""; 
    String loadPath=""; 
    
    
    // --------------------------------------------------------------------------
    
    void setup() {
      size(1350, 750);
    
      //// not necessary
      //for (int i = 0; i < 3; i++) { 
      //  points1.add(new PVector(i, 20 + 10*noise(i*0.1)));
      //}//for
      //for (int i = 0; i < 13; i++) { 
      //  points2.add(new PVector(i, 30 + 10*noise(i*0.1)));
      //}//for
      //for (int i = 0; i < 5; i++) { 
      //  points3.add(new PVector(i, 40 + 10*noise(i*0.1)));
      //}//for
      //for (int i = 0; i < 7; i++) { 
      //  points4.add(new PVector(i, 50 + 10*noise(i*0.1)));
      //}//for
    
      PFont  f= createFont ("Georgia", 25);
    
       // myPort = new Serial(this, "com3", 9600);  
      // myPort.bufferUntil(lf);
    }
    
    void draw() {
    
      switch (state) {
    
      case normal:
        drawForStateNormal() ;
        break; 
    
      case save:
        // wait for Save Dialog 
        waitForSaveDialog();
        break; 
    
      case load:
        // wait for Load Dialog 
        waitForLoadDialog();
        break;
    
      case help:
        // help
        drawForStateHelp() ;
        break; 
    
      default:
        //Error 
        println("Fail 192; unknown state: "
          +state);
        exit();
        break; 
        //
      } //switch
      //
    }//func
    
    // ------------------------------------------------
    
    void drawForStateNormal() {
    
      background(245);
      {
          strokeWeight(2);//spessore linea rettangolo grafico
          stroke(50);;//colore contorno rettangolo grafico
        fill(245);
        rect(width-1320, height-590, 820, 565); //posizione e grandezza rettangolo grafico
        rect(width-490, height-495, 480, 120); //posizione e grandezza rettangolo nome file
        rect(width-490, height-740, 430, 235); //posizione e grandezza rettangolo valori
        //linee grafico verticali.
        strokeWeight(2);
        stroke (200);
        fill(255);
        line(width-1270,height-60,width-1270,height-580);
        line(width-1215,height-60,width-1215,height-580);
        line(width-1160,height-60,width-1160,height-580);
        line(width-1105,height-60,width-1105,height-580);
        line(width-1050,height-60,width-1050,height-580);
        line(width-995,height-60,width-995,height-580);
        line(width-940,height-60,width-940,height-580);
        line(width-885,height-60,width-885,height-580);
        line(width-830,height-60,width-830,height-580);
        line(width-775,height-60,width-775,height-580);
        line(width-720,height-60,width-720,height-580);
        line(width-665,height-60,width-665,height-580);
        line(width-610,height-60,width-610,height-580);
        line(width-555,height-60,width-555,height-580);
    
        //linee grafico verticali
        //line(width-1200,height-30,width-1200,height-580);
        //line(width-1200,height-30,width-1200,height-580);
        //line(width-1200,height-30,width-1200,height-580);
        //numeri grafico orizzontali
        stroke(0);
         strokeWeight(1);
        fill(0);
        line(width-1270,height-50,width-1270,height-60);
        line(width-1215,height-50,width-1215,height-60);
        line(width-1160,height-50,width-1160,height-60);
        line(width-1105,height-50,width-1105,height-60);
        line(width-1050,height-50,width-1050,height-60);
        line(width-995,height-50,width-995,height-60);
        line(width-940,height-50,width-940,height-60);
        line(width-885,height-50,width-885,height-60);
        line(width-830,height-50,width-830,height-60);
        line(width-775,height-50,width-775,height-60);
        line(width-720,height-50,width-720,height-60);
        line(width-665,height-50,width-665,height-60);
        line(width-610,height-50,width-610,height-60);
        line(width-555,height-50,width-555,height-60);
        line(width-1280,height-50,width-540,height-50);
       // line(width-1275,height-50,width-1275,height-580);// linea verticale bianca
        textSize(15);
        text("0,0",width-1280,height-30);  // numeri grafico
        text("1,0",width-1225,height-30);
        text("2,0",width-1170,height-30);
        text("3,0",width-1115,height-30);
        text("4,0",width-1060,height-30);
        text("5,0",width-1005,height-30);
        text("6,0",width-950,height-30);
        text("7,0",width-895,height-30);
        text("8,0",width-840,height-30);
        text("9,0",width-785,height-30);
        text("10,0",width-740,height-30);
        text("11,0",width-685,height-30);
        text("12,0",width-630,height-30);
        text("13,0",width-575,height-30);
    
        textSize(38);
        text(MasA,width-495,height-695);
    
        text(nf(PCil,0,2),width-200,height-695);
        text(nf(PAtm,0,2),width-200,height-617);
        //text(Temp,1000,200);
        text(((nf(PAtm- PCil,0,2))), width-200,height-539);
        textSize(16);
        text("(In/Wg)",width-200,height-670);
        text("(In/Wg)",width-200,height-592);
        text("(In/Wg)",width-200,height-514);
        text("(Kg/s)",width-485,height-670);
        strokeWeight(1);
        fill(0);   
        textSize(15); 
        text(" (mm)", width-1180, height-5);
        // text verticale
          fill(0);
        float x = width-1330;
        float y = height-130;
        textAlign(CENTER,BOTTOM);
        pushMatrix();
        translate(x,y);
        rotate(-HALF_PI);
        text("(Kg/s)",0,0);
        popMatrix(); 
    
        // tabella
         strokeWeight(2);//spessore linea rettangolo grafico
          stroke(50);;//colore contorno rettangolo grafico
        fill(245);
        rect(width-490, height-365, 480, 340);//posizione e grandezza rettangolo tabella
       // stroke(200);
        fill(0);
          text("0",width-470,height-290); //numeri tab.
        text("1",width-470,height-270);
        text("2",width-470,height-250);
        text("3",width-470,height-230);
        text("4",width-470,height-210);
        text("5",width-470,height-190);
        text("6",width-470,height-170);
        text("7",width-470,height-150);
        text("8",width-470,height-130);
        text("9",width-470,height-110);
        text("10",width-475,height-90);
        text("11",width-475,height-70);
        text("12",width-475,height-50);
        text("13",width-475,height-30);
    
    
        stroke(0);
         strokeWeight(1);
        fill(0);
        line(width-480,height-290,width-20,height-290);
        line(width-480,height-270,width-20,height-270);
        line(width-480,height-250,width-20,height-250);
        line(width-480,height-230,width-20,height-230);
        line(width-480,height-210,width-20,height-210);
        line(width-480,height-190,width-20,height-190);
        line(width-480,height-170,width-20,height-170);
        line(width-480,height-150,width-20,height-150);
        line(width-480,height-130,width-20,height-130);
        line(width-480,height-110,width-20,height-110);
        line(width-480,height-90,width-20,height-90);
        line(width-480,height-70,width-20,height-70);
        line(width-480,height-50,width-20,height-50);
        line(width-480,height-30,width-20,height-30);
        //line val
         line(width-490,height-662,width-60,height-662);
         line(width-347,height-505,width-347,height-740);
         line(width-490,height-584,width-60,height-584);
         line(width-204,height-505,width-204,height-740);
    
    
    
    
    
      textSize(14);
      showData1();
      showData2();
      showData3();
      showData4();
    
      //fill(255, 2, 2);
     textSize(77);
      text("thank you Chrisir", width-910,90);//TITOLO
      textSize(40);
    
      textAlign(LEFT);
    
      //// help
      if (showHelpText1) {
        float widthOfBox = 210; 
        fill(240, 544, 2); // yellow 
        stroke(0);         // black 
        rect(420, 27, widthOfBox, 322);
        fill(0);           // black 
        text("Use mouse above columns (or 0..3) to set the active table "
          +"which is marked by the horizontal line on top.\n\n"
          +"New, Load and Save are referring to the active table (and Backspace).\n"
          +"m to hide / show text data.\n\nHit x to hide / show this text.", 
          425, 30, widthOfBox-10, 522);
      }
    
      // ----------------------
      // buttons
      showButtons();
    
      // gray lines above columns 
      for (int i=0; i < arrayOfXValuesFrom.length; i++) {
        if (activeArrayListHasNumber!=i) {
          stroke(111); //
          strokeWeight(2.8); //spessore linea richiamo
          line (arrayOfXValuesFrom [i], 405, //linea richiamo
            arrayOfXValuesTo[i], 405);
          strokeWeight(0.8); //spessore linea richiamo
          line (arrayOfXValuesFrom [i]+10, 415, //
            arrayOfXValuesTo[i]-10, 415);
          line (arrayOfXValuesFrom [i]+10, 400-4, 
            arrayOfXValuesTo[i]-10, 400-4);
        }//if 
        fill(0);
        text (fileNames[i],
        875, arrayOfXValuesTFile [i]);             // posizione fileNames =                                           //
      }//for 
    
      // reset
      strokeWeight(1.0); 
    }  //
    } //draw
    
    void drawForStateHelp() {
    
      // Help
    
      background(245);
      //MasA.setVisible(false);
      //PCil.setVisible(false);
      //PAtm.setVisible(false);
      ////myKnob4.setVisible(false); 
    
  •   fill(255, 2, 2);
      text("My little Graph program\n\nUse buttons on the right and below;\n"    //testo e posizione testo
        +"use Backspace to delete last data point of each list.\n\n"
        +"Use 1 and 2 to set the active table which is marked by the horizontal line on top.\n"
        +"New, Load and Save are referring to the active table (and also Backspace).\n\n"
        +"You can click now above each column to activate it.\n"
        +"You can press m to hide/show data text and x to show / hide quick help."
        +"\n\nHit any key to return to main program.", 
        113, 122, width-200, 422);
    }
    
    // --------------------------------------------------
    // functions to show buttons funzione per mostrare i bottoni
    
    void showButtons() {
      //
      strokeWeight(2);   
      stroke(0);
      textSize(15);
    
      fill(0);
      if ( overSave() ) {
        fill(#FF0303);
      }
    
      rect(width-50, height-740, 40, 20, 5);// modificato posizione bottone "salve"
      fill(255); 
      text("Save", 
        width-50+2, height-740+9+6);
    
      // ---
    
      fill(0);
      if ( overLoad() ) {
        fill(#49C3FA);
      }
    
      rect(width-50, height-710, 40, 20, 5);// modificato posizione" load"
      fill(255); 
      text("Load", 
        width-50+2, height-710+9+6);
    
      // ---
    
      fill(0);
      if ( overNew() ) {
        fill(#1FFF03);
      }
    
      rect(width-50, height-680, 40, 20, 5);// modificato posizione "new"
      fill(255); 
      text("New", 
        width-50+4, height-680+9+6);
    
      // ---
    
      fill(0);
      if ( overHelp() ) {
        fill(#1AAF03);
      }
      rect(width-50, height-650, 40, 20, 5);// modificato posizione "help"
      fill(255); 
      text("Help", 
        width-50+4, height-650+9+6);
    }
    
    //----------------------------------------------------------------------------
    // functions to register if mouse is over buttons funzione da registrare se il mouse è sui pulsanti
    
    boolean overSave() {            // modificato
      return mouseX > width-50 && 
        mouseY > height-740  && 
        mouseY < height-740+25 ;
    }
    
    boolean overLoad() {                 //modificato
      return mouseX > width-50 && 
        mouseY > height-710  && 
        mouseY < height-710+25 ;
    }
    
    boolean overNew() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-680  && 
        mouseY < height-680+25 ;
    }
    
    boolean overHelp() {                         //modificato
      return mouseX > width -50 && 
        mouseY > height-650  && 
        mouseY < height-650+25 ;
    }
    
    // ---------------------------------------------------------------------------
    // Inputs 
    
    void keyPressed() {
    
      if (state==help) {
        goBackToMainState();
        return;
      }
    
      if (state!=normal)
        return;
    
      //  
      if (  keyCode == BACKSPACE ) {
        //
        backspaceOnActiveList();
        //
      } 
      // ----
      else if ( keyCode == DELETE ) {
        //
        // ignore 
        //
      } else if  ( keyCode == ESC ) {
        //
        // If yellow help box is ON  
        if (showHelpText1) {
          showHelpText1 = false; // yellow help box off
        } else {
          // else toggle
          showText = !showText; // data text / tables
        }//else 
        // kill Esc so we stay in the program 
        key=0;
        //
      } 
    
      // ------
    
      else {
        if ( key != CODED ) {
          //
          switch(key) {
    
          case '0':
            activeArrayListHasNumber = 0; 
            break;
    
          case '1':
            activeArrayListHasNumber = 1; 
            break;
    
          case '2':
            activeArrayListHasNumber = 2; 
            break;
    
          case '3':
            activeArrayListHasNumber = 3; 
            break;
    
          case 'x':
            showHelpText1 = !showHelpText1; // yellow help box 
            break;
    
          case 'm':
            showText = !showText; // data text / tables 
            break;
            //
          }//switch
          //
        }//if
      }//else
    }// func 
    
    void mousePressed() {
    
      if (state==help) {
        goBackToMainState();
        return;
      }
    
      if (state!=normal)
        return;
    
      // for the buttons 
      if ( overSave() ) {
        initSave();
      }
      //---
      else if ( overLoad() ) {
        initLoad();
      } else if ( overHelp() ) {
        state=help;
      }
      //---
      else if ( overNew() ) {
        //
        if ( activeArrayListHasNumber==0 ) {
          points1.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==1 ) {
          points2.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==2 ) {
          points3.clear();            //cancellare punti con new
        } else if ( activeArrayListHasNumber==3 ) {
          points4.clear();            //cancellare punti con new
        }// else if
    
        // set name 
        fileNames[activeArrayListHasNumber] = "not a File"; 
        //
      }
      //---
    
      boolean done = false; 
    
      if (mouseY>390 && mouseY<425){
        for (int i=0; i < arrayOfXValuesFrom.length; i++) {
          if (mouseX>arrayOfXValuesFrom[i] && 
            mouseX<arrayOfXValuesTo[i]) { 
            //
            activeArrayListHasNumber=i;
            done = true; 
            break;
          }//if
        }//for
      }//if
    
      if (!done) {
        // points.add(new PVector(points.size(), mouseY/10));
        if ( activeArrayListHasNumber == 0 ) {
           points1.add(new PVector(points1.size(), MasA));
         // points1.add(new PVector(points1.size(), mouseY));
        } else if ( activeArrayListHasNumber == 1 ) {
          points2.add(new PVector(points2.size(), MasA));
          //points2.add(new PVector(points2.size(), mouseY));
        } else if ( activeArrayListHasNumber == 2 ) {
          points3.add(new PVector(points3.size(), MasA));
          //points3.add(new PVector(points3.size(), mouseY));
        } else if ( activeArrayListHasNumber == 3 ) {
          //points2.add(new PVector(points2.size(), MasA.getValue()));
          //points4.add(new PVector(points4.size(), mouseY));
          points4.add(new PVector(points4.size(), MasA));
        }
      }
    }//func
    
    // -------------------------------------------------
    // Save and load 
    
    void initSave() {
      // init save process 
      // reset
      savePath=""; 
      // make date time stamp (the expression nf(n,2) means leading zero: 2 becomes 02)
      String dateTimeStamp = year() 
        + nf(month(), 2) 
        + nf(day(), 2) 
        + "-"
        + nf(hour(), 2)
        + nf(minute(), 2)
        + nf(second(), 2); 
      // prepare fileDescription which occurs in the dialogue
      // prepara la descrizione del file che avviene nel dialogo
      File fileDescription = new File( sketchPath()
        + "//"
        + pathFolder 
        + "//"
        + dateTimeStamp
        + fileExtension); 
      // open the dialog  
      selectOutput("Select a file to write to", "fileSelectedSave", fileDescription); 
      // set state to wait
      state=save;
    }
    
    void initLoad() {
      // init load process 
      // reset
      loadPath=""; 
      // prepare fileDescription which occurs in the dialogue
      File fileDescription = new File( sketchPath()+"//"+pathFolder+"//"+"*" + fileExtension ); 
      // open the dialog
      selectInput("Select a file to load", "fileSelectedLoad", fileDescription); 
      // set state to wait
      state=load;
    }
    
    void fileSelectedSave(File selection) {
      // the 'callback' function
      if (selection == null) {
        // println("Window was closed or the user hit cancel.");
    
    
        // println ("La finestra è stata chiusa o l'utente ha premuto annulla.");
        // torna indietro
    
        state=normal; // go back
      } else {
        // println("User selected " + selection.getAbsolutePath());
        savePath=selection.getAbsolutePath();
      }
    }
    
    void fileSelectedLoad(File selection) {
      // the 'callback' function
      if (selection == null) {
        // println("Window was closed or the user hit cancel.");
    
        // go back 
        state=normal;
      } else {
        // println("User selected " + selection.getAbsolutePath());
        loadPath=selection.getAbsolutePath();
      }
    }
    
    void waitForSaveDialog() { 
      if (!savePath.equals("")) {
        // waiting is over
        saveIt(); 
        // go back 
        state=normal;
      }
    }
    void waitForLoadDialog() { 
      if (!loadPath.equals("")) {
        // waiting is over
        loadIt(); 
        // go back 
        state=normal;
      }
    }
    
    void saveIt() {
      // save
    
      // make array (to save it)
      String[] strs; 
      strs = new String[0]; 
    
      if (activeArrayListHasNumber==0) {
        strs = new String[points1.size()]; 
        int i=0; 
        for (PVector pv : points1) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }// 
      else if (activeArrayListHasNumber==1) {
        strs = new String[points2.size()]; 
        int i=0; 
        for (PVector pv : points2) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
      else if (activeArrayListHasNumber==2) {
        strs = new String[points3.size()]; 
        int i=0; 
        for (PVector pv : points3) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
      else if (activeArrayListHasNumber==3) {
        strs = new String[points4.size()]; 
        int i=0; 
        for (PVector pv : points4) {
          strs[i]=str(pv.x)+","+str(pv.y); 
          i++;
        }//for
      }//else
    
      // check if file extension (fileExtension, e.g. .txt) is there 
      int len = savePath.length(); 
      if (len<4 || !savePath.substring( len-4 ).equals(fileExtension)) {
        // file Extension is not present, we have to add it
        savePath += fileExtension; // add the file Extension
      } 
    
      // save 
      println("Saved: " + savePath); 
      saveStrings( savePath, strs );
    
      // get file name for list 
      fileNames[activeArrayListHasNumber] =  nameFromPath(savePath);
    } //func
    
    void loadIt() {
      // load
      if (activeArrayListHasNumber==0)
      {
        points1.clear();
      } else if (activeArrayListHasNumber==1)
      {
        points2.clear();
      } else if (activeArrayListHasNumber==2)
      {
        points3.clear();
      } else if (activeArrayListHasNumber==3)
      {
        points4.clear();
      }
    
      fileNames[activeArrayListHasNumber] =  nameFromPath(loadPath); 
    
      String[] strs = loadStrings( loadPath ); 
      for (String s : strs) {
        String[] thisLine=split(s, ","); 
    
        if (activeArrayListHasNumber==0) 
        {
          points1.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==1)
        {
          points2.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==2)
        {
          points3.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        } else if (activeArrayListHasNumber==3)
        {
          points4.add(new PVector(float(thisLine[0]), float(thisLine[1])));
        }//else
        //
      }//for
    }//func 
    
    // -------------------------------------------------
    
    void showData1() {
      //
      // show data 1
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
    
      if (activeArrayListHasNumber==0) {
        stroke(255, 2, 2); // RED
        line (900, 405, 990, 405);  //linea sopra tabella
        line (870, 260, 1330, 260); 
        line (870, 280, 1330, 280);// linee text file
        ellipse (865, 270, 6, 6);
      }
    
      for (PVector pv : points1) {
    
        if (showText) {
          fill(255, 2, 2); // RED 
          // show data
          text(pv.x, 970-50, y+410); 
          text(pv.y, 970, y+410);
        }
    
        fill(0, 2, 2); 
        stroke(255, 2, 2); // RED 
        float yvalue = map( pv.y, 0,1, 700, 180); //mappatura linea grafica RED
        if (prev.x!=-1) {
          line(80 + pv.x*55, yvalue, 
            prev.x, prev.y);// linea grafico red
        }
        stroke(150); 
         ellipse (80 + pv.x*55, yvalue, 4, 4);
        prev = new PVector(80 + pv.x*55, yvalue); 
    
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(255, 2, 2); // RED 
        line(1275-330,440, 1275-330,720);
    
      }
      //
    }
    
    void showData2() {
      //
      // show data 2 
    
      if (activeArrayListHasNumber==1) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(2, 255, 2); // GREEN
        line (1010, 405, 1100, 405);
        line (870, 290, 1330, 290);
        line (870, 310, 1330, 310);// linee text file
        ellipse (865, 300, 6, 6);
    
      }
    
  •   PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points2) {
    
        if (showText) {
          fill(0, 255, 2); // GREEN 
          // show data
         // text(pv.x, 210, y); 
           text(pv.x, 1080-50, y+410);
           text(pv.y, 1080, y+410);
        }
    
        fill(0, 2, 2); 
        stroke(0, 255, 2); // GREEN 
        float yvalue = map( pv.y, 0,1, 700, 180); // dimensione grafico GREEN
        if (prev.x!=-1) {
          line(80 + pv.x*55, yvalue, // linea grafico verde
            prev.x, prev.y);
    
        }
        noStroke(); 
        ellipse (80 + pv.x*55, yvalue, 4, 4);
        prev = new PVector(80 + pv.x*55, yvalue);
    
        y+=20; //next line
      }//for
    
      if (showText) {
      //  // middle line 
        stroke(0, 255, 2); // GREEN 
        line( 1275-220, 440, 1275-220, 720);// linea divisoria
    
      }
      ////
    }
    
    void showData3() {
      //
      // show data 3
    
      color col3 = color(0, 2, 255); // BLUE 
    
      if (activeArrayListHasNumber==2) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(col3); // 
        line (1120, 405, 
          1210, 405);
         line (870, 320, 1330, 320);
        line (870, 340, 1330, 340);
         ellipse (865, 330, 6, 6);
      }
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points3) {
    
        fill(col3); // Blue 
    
        if (showText) {
          // show data
          text(pv.x, 1190-50, y+410); 
          text(pv.y, 1190, y+410);// 
        }
    
        fill(0, 2, 2); 
        stroke(col3); // blue 
        //float yvalue = map( pv.y, 0, height, 300, 355); 
        //if (prev.x!=-1) {
        //  line(10 + pv.x*3, yvalue, 
        //    prev.x, prev.y);
             float yvalue = map( pv.y, 0,1, 700, 180); // dimensione grafico GREEN
        if (prev.x!=-1) {
          line(80 + pv.x*55, yvalue, // linea grafico verde
            prev.x, prev.y);
        }
        noStroke(); 
    
      ellipse (80 + pv.x*55, yvalue, 4, 4);
        prev = new PVector(80 + pv.x*55, yvalue);
        y+=20; //next line
      }//for
    
      if (showText) {
     //   // middle line 
       stroke(col3); // blue 
        line( 1275-110,440,1275-110,720);
    
      }
      //
    }
    
    void showData4() {
      //
      // show data 4 
    
      color col4 = color(#FF00E6); 
    
      if (activeArrayListHasNumber==3) {
        // activeArrayListHasNumber is FALSE, right side is active
        stroke(col4); // 
        line (1230,405,1320,405);
        line (870, 350, 1330, 350);
        line (870, 370, 1330, 370);
         ellipse (865, 360, 6, 6);
      }
    
      PVector prev = new PVector(-1, -1); 
      int y=50; 
      for (PVector pv : points4) {
    
        fill(col4); //  
    
        if (showText) {
          // show data
          text(pv.x, 1300-50, y+410); 
          text(pv.y, 1300, y+410);
        }
    
        fill(0, 2, 2); 
        stroke(col4); //  
        //float yvalue = map( pv.y, 0, height, 300, 355); 
        //if (prev.x!=-1) {
        //  line(10 + pv.x*3, yvalue, 
        //    prev.x, prev.y);
         float yvalue = map( pv.y, 0,1, 700, 180); // dimensione grafico viola
        if (prev.x!=-1) {
          line(80 + pv.x*55, yvalue, // linea grafico viola
            prev.x, prev.y);
        }
        noStroke(); 
        ////   ellipse (10 + pv.x*3, yvalue, 4, 4);
        //prev = new PVector(10 + pv.x*3, yvalue);
         ellipse (80 + pv.x*55, yvalue, 4, 4);
        prev = new PVector(80 + pv.x*55, yvalue);
    
        y+=20; //next line
      }//for
    
      if (showText) {
        // middle line 
        stroke(col4); //  
        line( 1275, 440, 1275, 720);
      }
       { stroke(0); // BLACK 
    
        line( 1220, 400, 1220, 720); 
        line( 1220-110, 400, 1220-110, 720); 
        line( 1220-220, 400, 1220-220, 720); 
    
       }
    }
    
    // ---------------------------------------------------------------
     void serialEvent(Serial p) { 
      while (myPort.available() > 0) {
      inString = p.readString(); 
      char primo=inString.charAt(0);  // primo carattere
      String cifra=inString.substring(1);  // da secondo carattere in poi
      float val=parseFloat(cifra);          // valore da 0-255 o 0-1023, non sò cosa spedisce arduino
      print("primo=");  println(primo); //debug
    
      print("cifra=");  println(cifra); //debug
      print("val=");    println(val);
     // print(nf(val,0,2));
      switch(primo) { 
        case ('A'): MasA=(val);
         break;
    
        case ('B'): PCil=(val);
         break;  
    
        case ('C'): PAtm=(val);
          break;
    
       // case ('D'): Temp=(val);
         // break;
      } 
    }
    }
    
    void goBackToMainState() {
    
      // from help screen back to main screen
    
      // buttons ON 
     //MasA.setVisible(true); 
     // PCil.setVisible(true); 
     // PAtm.setVisible(true); 
     // myKnob4.setVisible(true); 
    
      //go back to main screen 
      state=normal;
    
      // kill Esc so we stay in the program 
      key=0;
    }
    
    void  backspaceOnActiveList() {
    
      // for the key BACKSPACE 
    
      switch (activeArrayListHasNumber) {
    
      case 0:
        if (points1.size()>0)
          points1.remove(points1.size()-1);
        break; 
    
      case 1:
        if (points2.size()>0)
          points2.remove(points2.size()-1);
        break;
    
      case 2:
        if (points3.size()>0)
          points3.remove(points3.size()-1);
        break;
    
      case 3:
        if (points4.size()>0)
          points4.remove(points4.size()-1);
        break;
      }//switch
      //
    }//func 
    
    // ------------------------------------------------
    // tools
    
    String nameFromPath(String fileName1) {
    
      File file = new File(fileName1);
      String result = file.getName();
      return result;
    } //func 
    //
    
  • Can you read data from arduino now...?

  • Read them

  • Answer ✓

    ;-)

  • however, from me it is said the appetite is eating .....

  • What do you mean by this?

Sign In or Register to comment.