Display values of a variable

Salutations,
I have to visualize the values ​​that come from arduino and that are constantly changing. Unfortunately, every time the value changes, this overlaps the previous one, making it difficult to read.

This is the code:

//Istruzioni per la futura connessione con la seriale
import processing.serial.*;
Serial myPort;
String portName;

//Istruzioni per la futura creazione del font
PFont font;

//Sinizializzazione oggetti dalle classi
Schermata schermo;
Ricezione valueArduino;

void setup(){
  
  //Inizializzazione nuova schermata
  size(900, 600);
  schermo = new Schermata();
  
  //Comunicazione seriale
  portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600); 
  
  
  
}

void draw(){
  
  valueArduino = new Ricezione();

}
class Ricezione{
  
  char HEADER = 'H';
  short LF = 10;
  String [] data;
  String message;
  int i;
  
  String levelwater;
  String soilmoisture;
  String temperature;
  String moisture;
  
  Ricezione(){
    
    if(myPort.available() > 0){
      
      message = myPort.readStringUntil(LF); //Lettura valori seriale
      
      if(message != null){
        
        message = message.trim(); //Rimuove spazi vuoti a inizio e fine
        println(message);
        data = message.split(","); //Dice che la virgola separa i valori
            if(data[0].charAt(0) == HEADER && data.length == 5){
    
      for(i = 1; i < data.length; i++){
        
        levelwater = data[1];
        soilmoisture = data[2];
        temperature = data[3];
        moisture = data[4];
            
      }
    
    }
    
    text("Livello acqua: " + levelwater + " cm", 350, 100);
    text("Umidità del suolo: " + soilmoisture + " %", 350, 150);
    text("Temperatura: " + temperature + " °C", 350, 200);
    text("Umidità: " + moisture + " %", 350, 250);
      
      }
    
    }
  
  }

}

How could I solve?

Try placing a background() call as the first line in draw().

1 Like

Oh, perfect. Now works. Thanks

1 Like

Now I have another problem…

How can I create a text cell, in which the end user will write values, which when a delivery button is pressed, will be saved in variables?

You will need to read about controls, either g4p, controlP5, or some other library. The following demo uses g4p text fields. After data is entered in each field you need to hit ‘Enter’ so that the data will be stored. Then when you get ready to save to disk, hit ‘s’ (could use a push button if desired). The output file is kept in the sketch folder, but this could be changed by adding the path in front of the file name if you want to store it someplace else .

import g4p_controls.*;

GTextField txtFld1, txtFld2, txtFld3;
String editStr1, editStr2, editStr3;
String[] myData;

public void setup() {
  size(500, 300);
  G4P.setInputFont("Arial", G4P.PLAIN, 18);
  txtFld1 = new GTextField(this, 10, 10, 200, 24);
  txtFld1.setText("test1");
  txtFld2 = new GTextField(this, 10, 40, 200, 24);
  txtFld2.setText("test2");
  txtFld3 = new GTextField(this, 10, 70, 200, 24);
  txtFld3.setText("test3");
  myData = new String[3];
}

public void draw() {
  background(64, 124, 188);

  if (keyPressed && key == 's') {
    // save array to file in sketch folder
    saveStrings("myData.txt", myData);
  }
}

public void handleTextEvents(GEditableTextControl textcontrol, GEvent event) {
  if (txtFld1 == textcontrol && event == GEvent.ENTERED) {
    editStr1 = txtFld1.getText();
    println(editStr1);
    myData[0] = editStr1;
  }
  if (txtFld2 == textcontrol && event == GEvent.ENTERED) {
    editStr2 = txtFld2.getText();
    println(editStr2);
    myData[1] = editStr2;
  }
  if (txtFld3 == textcontrol && event == GEvent.ENTERED) {
    editStr3 = txtFld3.getText();
    println(editStr3);
    myData[2] = editStr3;
  }
}