How to save last selections on Processing

Hi everyone, i am an amatur coder.
I want to use Processing as user interface to controll my arduino with LED color.
I want to save config i choose on Processing. My mean the User interface will show last select when i open UI again.
Please help me. So sorry, i dont know what’s keyword to search this case so i create the topic.

1 Like

Hi,

Welcome to the community! :slight_smile:

If you want to save information from one run to another, you need to store it on your computer in a file format. You can either create your own and then read it after and change your UI accordingly or use a predefined file format.

Are you using a specific GUI library like G4P or ControlP5?

You might be interested in saving in JSON or XML :

2 Likes

Here is an example

  • It simulates a GUI of buttons stored in list0
  • You can alter each of the 3 buttons with keys 1,2,3
  • When you leave, current settings is stored
  • when you load, either there is a file then its content is restored OR there is no file (first time) then nothing is loaded

Chrisir






// classButton 
ArrayList<ClassButton> list0=new ArrayList();
String[] arrString; 

void setup() {
  size(800, 800);
  background(0);

  list0.add(new ClassButton()); 
  list0.add(new ClassButton()); 
  list0.add(new ClassButton());  

  // load
  arrString=loadStrings("data.txt");
  // successful?
  if (arrString!=null) {
    // Yes, load 
    int i=0; 
    for (ClassButton pv : list0) {
      pv.value = int( arrString[i] );
      i++;
    }
  }//if
}

void draw() {
  background(0);

  // show entire list
  noStroke(); 
  for (ClassButton pv : list0) {
    pv.display();
  }//for
}//draw()

// -----------------------------------------------------------------------
// Inputs

void keyPressed() {
  switch (key) {
  case '1':
    list0.get(0).inc(); 
    break; 
  case '2':
    list0.get(1).inc();
    break; 
  case '3':
    list0.get(2).inc();
    break;

  case ESC:
    exitMy() ;
    break;
  }//case
}

void exitMy() {
  //println ("leave");
  String[] arrS = new String[3];
  arrS[0] =    new String( list0.get(0).value+"");
  arrS[1] =    new String( list0.get(1).value+""); 
  arrS[2] =    new String( list0.get(2).value+""); 
  // printArray (arrS);
  saveStrings("data.txt", arrS);
}

//================================================================================

class  ClassButton {

  int value; 
  float x = list0.size() * 45 + 34; 

  void inc() {
    value++;
    if (value>2)
      value=0;
  }//method

  void display() {
    text(value, 
      x, 45);
  }//method
}//class
//
3 Likes

Thank you so much. i will try it

1 Like