Processing Forum
void loop()
{
if(stringComplete)
{
Serial.println(inputString);
typeDonnee = inputString.substring(0, 2);
numPin = inputString.substring(2, 4);
value = inputString.substring(4, 5);
if(typeDonnee == "10")
{
if(value == "1")
digitalWrite(numPin.toInt(), HIGH);
else if(value == "0")
digitalWrite(numPin.toInt(), LOW);
}
inputString = "";
stringComplete = false;
}
//Method use to receive data from RX port
void serialEvent()
{
while(Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
if(inChar == '*')
{
stringComplete= true;
}
}
}
- import controlP5.*;
import processing.serial.*;
final int P_SIZE_WIDTH = 255;
final int P_SIZE_HEIGHT = 30;
final int buttonHeight = 40;
final int buttonWidth = 70;
DropdownList ports; //Define the variable ports as a Dropdownlist.
Serial port; //Define the variable port as a Serial object.
ControlP5 cp5;
int Ss; //The dropdown list will return a float value, which we will connvert into an int. we will use this int for that).
String[] comList ; //A string to hold the ports in.
boolean serialSet; //A value to test if we have setup the Serial port.
boolean Comselected = false; //A value to test if you have chosen a port in the list.
String portSelec;
String s;
int centerX = 0, centerY = 0, offsetX = 0, offsetY = 0;
void setup() {
size(1200, 800);
cp5 = new ControlP5(this);
//Setup the dropdownlist by using a function. This is more pratical if you have several list that needs the same settings.
ControlFont font = new ControlFont(createFont("Arial",20),20);
//Make a dropdown list calle ports. Lets explain the values: ("name", left margin, top margin, width, height (84 here since the boxes have a height of 20, and theres 1 px between each item so 4 items (or scroll bar).
ports = cp5.addDropdownList("list-1",10,40,220,84);
cp5.setControlFont(font);
customize(ports);
pompesInit();
vannesInit();
centerX = 0;
centerY = 0;
cursor(HAND);
smooth();
}
void draw() {
//So when we have chosen a Serial port but we havent yet setup the Serial connection. Do this loop
s = "";
text(s,50, 50, 480, 80);
if(Comselected && !serialSet)
{
//Call the startSerial function, sending it the char array (string[]) comList
//startSerial(comList);
s = "Port sélectionné.";
}
else {
s = ("Vous n'avez pas sélectionné de port COM.");
}
fill(0);
textSize(25);
text(s,50, 50, 480, 80);
if (mousePressed == true) {
centerX = mouseX-offsetX;
centerY = mouseY-offsetY;
}
translate(centerX,centerY);
}
void mousePressed(){
offsetX = mouseX-centerX;
offsetY = mouseY-centerY;
}
void pompesInit()
{
for(int i = 1; i < 6; i++)
{
cp5.addSlider("p"+i);
Slider s1 = (Slider)cp5.controller("p"+i);
s1.setSize(P_SIZE_WIDTH,P_SIZE_HEIGHT);
s1.setColorLabel(0);
}
cp5.getController("p1").setPosition(500, 50);
cp5.getController("p2").setPosition(120, 250);
cp5.getController("p3").setPosition(720, 400);
cp5.getController("p4").setPosition(720, 590);
cp5.getController("p5").setPosition(240, 680);
}
void vannesInit()
{
stroke(0);
for(int i = 1; i < 7; i++)
{
cp5.addToggle("v" + i)
.setSize(buttonWidth,buttonHeight)
.setValue(false)
.setLabel("OFF")
.setMode(ControlP5.SWITCH)
.setColorLabel(0);
;
cp5.getController("v" + i).getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER);
}
cp5.getController("v1").setPosition(50, 20);
cp5.getController("v2").setPosition(50, 120);
cp5.getController("v3").setPosition(155, 75);
cp5.getController("v4").setPosition(380, 275);
cp5.getController("v5").setPosition(540, 275);
cp5.getController("v6").setPosition(700, 275);
}
public void etatButton(boolean buttonState, int numVanne)
{
if(buttonState)
{
cp5.getController("v"+numVanne).setLabel("ON");
}
else if(!buttonState)
{
cp5.getController("v"+numVanne).setLabel("OFF");
}
}
public void v1(boolean buttonState) {
etatButton(buttonState, 1);
}
public void v2(boolean buttonState) {
etatButton(buttonState, 2);
}
public void v3(boolean buttonState) {
etatButton(buttonState, 3);
}
public void v4(boolean buttonState) {
etatButton(buttonState, 4);
}
public void v5(boolean buttonState) {
etatButton(buttonState, 5);
}
public void v6(boolean buttonState) {
etatButton(buttonState, 6);
}
void p1(float p1Value) {
// Grab slider value (0-100) and send to Arduino
int p1FinalValue = round(p1Value);
port.write(p1FinalValue);
println(p1FinalValue);
}
//The dropdown list returns the data in a way, that i dont fully understand, again mokey see monkey do. However once inside the two loops, the value (a float) can be achive via the used line ;).
void controlEvent(ControlEvent theEvent) {
if (theEvent.isGroup())
{
//Store the value of which box was selected, we will use this to acces a string (char array).
float S = theEvent.group().value();
//Since the list returns a float, we need to convert it to an int. For that we us the int() function.
Ss = int(S);
//With this code, its a one time setup, so we state that the selection of port has been done. You could modify the code to stop the serial connection and establish a new one.
Comselected = true;
}
}
//here we setup the dropdown list.
void customize(DropdownList ddl) {
//Set the height of each item when the list is opened.
ddl.setItemHeight(20);
//Set the height of the bar itself.
ddl.setBarHeight(30);
//Set the lable of the bar when nothing is selected.
ddl.captionLabel().set("Select COM port");
//Set the top margin of the lable.
ddl.captionLabel().style().marginTop = 3;
//Set the left margin of the lable.
ddl.captionLabel().style().marginLeft = 3;
//Set the top margin of the value selected.
ddl.valueLabel().style().marginTop = 3;
//Store the Serial ports in the string comList (char array).
comList = port.list();
//We need to know how many ports there are, to know how many items to add to the list, so we will convert it to a String object (part of a class).
String comlist = join(comList, ",");
//We also need how many characters there is in a single port name, we´ll store the chars here for counting later.
String COMlist = comList[0];
//Here we count the length of each port name.
int size2 = COMlist.length();
//Now we can count how many ports there are, well that is count how many chars there are, so we will divide by the amount of chars per port name.
int size1 = comlist.length() / size2;
//Now well add the ports to the list, we use a for loop for that. How many items is determined by the value of size1.
for(int i=0; i< size1; i++)
{
//This is the line doing the actual adding of items, we use the current loop we are in to determin what place in the char array to access and what item number to add it as.
ddl.addItem(comList[i],i);
}
//Set the color of the item your mouse is hovering over.
ddl.setColorActive(color(255,128));
ddl.setPosition(960,50);
}
void startSerial(String[] theport)
{
//When this function is called, we setup the Serial connection with the accuried values. The int Ss acesses the determins where to accsess the char array.
port = new Serial(this, theport[Ss], 9600);
//Since this is a one time setup, we state that we now have set up the connection.
serialSet = true;
portSelec = theport[Ss];
}