Dropdownlist from csv file

Hello, I am asking for an example of how to complete the dropdownlist from the controlP5 library from a csv file. The csv file looks like
Id, slider1, slider2, slider 3
Settings_1, 10, 12, 20
Settings_2, 12, 20, 30

I would like the Id field to be dropdownl.addItem “”
and use the values for the rest of the program.
I cannot find such an example anywhere. I wanted to use the loadTable and saveTable functions
Maybe someone would have a code snippet as an example.
Thank you.

can you show your attempt?

You should at least try it

I have a standard procedure taken from the command list + drop down list settings. This is my first GUI program to change the values in an arduino program. Usually I declared it to myself in the program and chose it with the “case” but I wanted to go a step further.

Table table

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);
 
 
 table = loadTable("Slider.csv", "header");

  println(table.getRowCount() + " total rows in table");

  for (TableRow row : table.rows()) {

    String id = row.getString("id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");
    int slider3 = row.getInt("slider3");

  ddl1 = cp5.addDropdownList("Settings")
          .setPosition(50, 200)
          .setSize(140, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
 
 

somehow the Sketch is not complete but cut off?

please post again

here ONE dropdownlist is filled with items from the csv file

import controlP5.*;

Table table;
ControlP5 cp5; 

DropdownList ddl1 ; 

void setup() {

  size(700, 400 );
  clear();

  cp5 = new ControlP5(this);


  table = loadTable("Slider.csv", "header");
  println(table.getRowCount() + " total rows in table");

  ddl1 = cp5.addDropdownList("Settings")
    .setPosition(50, 200)
    .setSize(140, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    //    .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  int i=0; 
  for (TableRow row : table.rows()) {

    String id = row.getString("Id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");
    int slider3 = row.getInt("slider3");

    ddl1.addItem(id +" "+i+ " -> ", slider1);
    i++;
    //
  }//for
}//func

void draw() {
  background(128);
}

This description is not so clear. Settings_1 and Settings_2 are now the captions for the items in the list. But what about slider1, slider2, slider 3?

The csv is wrong: better without spaces after , and before the 3 (in “slider 3”)

Id,slider1,slider2,slider3
Settings_1, 10, 12, 20
Settings_2, 12, 20, 30
…

Hey, and welcome to the forum!

Great to have you here!

Thank you, I will deal with your example. I’m sorry you had to correct such stupid things, but I am a mechanic who has to do some simple programming from time to time (very simple) and ask such silly questions in such a good forum.

Thank you. the csv file is to be expanded and the Slider3 value is to be added. My code:

import processing.serial.*;
import controlP5.*;
 
ControlP5 cp5;
DropdownList ddl1;
//Slider Range1MaxControl,Range1MinControl,Range2MinControl,Range2MaxControl;
 
Serial myPort;
 
String portName, id;
int serialListIndex;
int k, slider1, slider2;
int Range1Min,Range1Max,Range2Min,Range2Max;
Table table;

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);

ddl1 = cp5.addDropdownList("Settings")
          .setPosition(50, 200)
          .setSize(140, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
 
 table = loadTable("Slider.csv", "header");
  for (TableRow row : table.rows()) {

    String id = row.getString("id");
    int slider1 = row.getInt("Slider_1");
    int slider2 = row.getInt("Slider_2");
  }
  //  Here it is messed up because assigning values from table to array should be in the same for loop.
    for (int i=0; i=table.getRowCount() ; i++) {          
            id[i] =row.getString("id");
            slider1[i]=row.getInt("Slider_1");
            slider2[i]=row.getInt("Slider_2");
            ddl1.addItem(id, i); //add the items in the list
    }

     
                      
}
 
void draw() {
  background(128);
 

        if(theEvent.isController() && ddl1.isMouseOver())
    {
      switch(int(theEvent.getController().getValue()))
      {
    for (int i=0; i=table.getRowCount() ; i++) {          
           
        case [i]:
          Range1Min=(slider1[i]);
          Range1Max=(slider2[i]);
          break;
    }
      }
    
}
}

remember to click ctrl-t in processing (not in the forum) to get nice auto-format

I guess it worked: Maybe someone green like me will need it someday …

/*slider.csv
 Id,slider1,slider2
 Settings_1,10,12
 Settings_2,40,50
 Settings_3,10,60
 Settings_4,240,150
 Settings_5,120,12
 Settings_6,402,50
 */

import controlP5.*;

Table table;
ControlP5 cp5; 

DropdownList ddl1 ; 
int[] slide1;  
int[] slide2; 
void setup() {

  size(700, 400 );
  clear();

  cp5 = new ControlP5(this);


  table = loadTable("Slider.csv", "header");

  println(table.getRowCount() + " total rows in table");

  slide1= new int[table.getRowCount()];
  slide2= new int[table.getRowCount()];
  ddl1 = cp5.addDropdownList("Settings")
    .setPosition(50, 200)
    .setSize(140, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    //    .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  int i=0; 

  for (TableRow row : table.rows()) {

    String id = row.getString("Id");
    int slider1 = row.getInt("slider1");
    int slider2 = row.getInt("slider2");


    ddl1.addItem(id +"  "+i+ " -> ", slider1);
    slide1[i]=slider1;
    slide2[i]=slider2;

    i++;
    //
  }//for
}//func

void draw() {
  background(128);
  println(slide1[0]);
  println(slide2[4]);
}

1 Like

Helli I would have one more request. I am trying to use this code to read and select a COM port. But with more COM, the list doesn’t scroll. Jumps and I can’t select COM, from the bottom, invisible part of the list. Please help/

import processing.serial.*;
import controlP5.*;
 
ControlP5 cp5;
DropdownList d1;
 
Serial myPort;
 
String portName;
int serialListIndex;
 
void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);
 
  PFont pfont = createFont("Arial",10,true); //Create a font
  ControlFont font = new ControlFont(pfont,20); //font, font-size
 
  d1 = cp5.addDropdownList("myList-d1")
          .setPosition(100, 100)
          .setSize(100, 200)
          .setHeight(210)
          .setItemHeight(40)
          .setBarHeight(50)
          .setFont(font)
          .setColorBackground(color(60))
          .setColorActive(color(255, 128))
          ;
 
      d1.getCaptionLabel().set("PORT"); //set PORT before anything is selected
 
      portName = Serial.list()[0]; //0 as default
      myPort = new Serial(this, portName, 9600);
}
 
void draw() {
  background(128);
 
  if(d1.isMouseOver()) {
   d1.clear(); //Delete all the items
   for (int i=0;i<Serial.list().length;i++) {
     d1.addItem(Serial.list()[i], i); //add the items in the list
   }
  }
  if ( myPort.available() > 0) {  //read incoming data from serial port
    println(myPort.readStringUntil('\n')); //read until new input
   } 
}
 
void controlEvent(ControlEvent theEvent) { //when something in the list is selected
    myPort.clear(); //delete the port
    myPort.stop(); //stop the port
    if (theEvent.isController() && d1.isMouseOver()) {
    portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
    myPort = new Serial(this, portName, 9600); //Create a new connection
    println("Serial index set to: " + theEvent.getController().getValue());
    delay(2000); 
    }
}

Apparently I don’t have any ports.

So I just use a String list instead, OK?

Do you see a scrolling here? I do.


import processing.serial.*;
import controlP5.*;

ControlP5 cp5;
DropdownList d1;

Serial myPort;

String portName;
int serialListIndex;

void setup() {
  clear();
  size(700, 400 );
  cp5 = new ControlP5(this);

  PFont pfont = createFont("Arial", 10, true); //Create a font
  ControlFont font = new ControlFont(pfont, 20); //font, font-size

  d1 = cp5.addDropdownList("myList-d1")
    .setPosition(100, 100)
    .setSize(100, 200)
    .setHeight(210)
    .setItemHeight(40)
    .setBarHeight(50)
    .setFont(font)
    .setColorBackground(color(60))
    .setColorActive(color(255, 128))
    ;

  d1.getCaptionLabel().set("PORT"); //set PORT before anything is selected

  //println(Serial.list().length); 
  //  portName = Serial.list()[0]; //0 as default
  //String[] portNameList = Serial.list(); //0 as default
  String[]  portNameList = { 
    "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz", "dsf", "ed", "df", "urz"
  }; 
  print("here ");
  //  println( portName2);

  for (String s1 : portNameList) {
    d1.addItem(s1, "!");
  } 

  //myPort = new Serial(this, portName, 9600);
}

void draw() {
  background(128);
}

void controlEvent(ControlEvent theEvent) { //when something in the list is selected
  myPort.clear(); //delete the port
  myPort.stop(); //stop the port
  if (theEvent.isController() && d1.isMouseOver()) {
    portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
    myPort = new Serial(this, portName, 9600); //Create a new connection
    println("Serial index set to: " + theEvent.getController().getValue());
    delay(2000);
  }
}
//

Sorry to just write back, but I was busy. Thank you for your help. I threw the COM ports section into Setup and it works. I have one more prosaic problem.

    String myString = myPort.readStringUntil(97); //read until "a"
     println(myString);

By COM, I send a text in the form: number + digit, e.g. 150a, I want to read it and convert it into an integer variable. As a String I read 150a, but I can’t convert it to an int.

You can use substring command to cut the last letter off

Then use int() command


String old = "150a";

String new1 = old.substring(0, old.length()-1); 

println (new1); 

int newInt = int(new1); 

println (newInt);

Thanks, works perfect !!!

1 Like