Second applet, closing it and transparent buttons in Processing 3

Hi everyone. My first post, please do not judge strictly.



There was a need for work to visually read the condition of the equipment and, if necessary, restart the cryocompressor remotely. At hand was Arduino UNO, a light sensor was purchased for it, so far 2 relays and 2 solenoids at 12 V (not the point). In the future, this business will be overgrown with sensors of temperature, humidity and pressure, but so far it is not ready-made.



The reason for writing the post is that there was very little information on the Internet on the capabilities of Processing 3, especially in the Russian-language part of it. I hope that some teapots like me will come in handy someday so as not to stupid like me over the past week.



The sketch for Arduino is as simple as three rubles: when certain values โ€‹โ€‹are received from the com port, the dunk sets the value on the pins and writes the answer to the com port, measures the illumination and also reports on it. In the future, it will be expanded.



Sketch Arduino:



#include <Wire.h> #include <BH1750.h> int val; BH1750 lightMeter; void setup() { Serial.begin(9600); lightMeter.begin(); Serial.println("Running..."); pinMode (7, OUTPUT); pinMode(6, OUTPUT); digitalWrite(7, HIGH); digitalWrite(6, HIGH); } void loop() { uint16_t lux = lightMeter.readLightLevel(); if (Serial.available()) { val = Serial.read(); if (val == '0') { digitalWrite(7, HIGH); digitalWrite(6, HIGH); Serial.println("  "); } if (val == '1') { digitalWrite(7, HIGH); digitalWrite(6, LOW); Serial.println(" 1 "); } if (val == '2') { digitalWrite(7, LOW); digitalWrite(6, HIGH); Serial.println("  2"); } if (val == '3') { digitalWrite(7, LOW); digitalWrite(6, LOW); Serial.println("  "); } if (val == '4') { if (lux >= 800) { Serial.print("Light: "); Serial.print(lux); Serial.println(" lx   "); } else { Serial.print("Light: "); Serial.print(lux); Serial.println(" lx  !!!!!!!"); } } } }
      
      





I think this is a no brainer, but due to the fact that other people will use the functionality of my Pribluda in the future, I wanted to write a simple but understandable GUI. The choice fell on Processing, as I was previously familiar with the Arduino IDE, which, in turn, was built on the basis of Processing.



The applet uses the ControlP5 library to render controls.



Personally, I had problems with implementing and searching for information on the following things: 1) launching the second window from the application, 2) closing the 2nd window, but so that the main window continues to work, 3) for the sake of fun - setting pictures as a background and buttons, translucent buttons.



So, the Processing sketch:



Code listing
 import processing.serial.*; import controlP5.*; import java.util.Date; Serial port; Textfield recived; Textfield recived1; Textfield recived2; Textfield recived3; Textfield recived4; Textfield recived5; String val; String data; String DateTab; String Dat; int data2=0; String time; Button vyvod; PrintWriter output; Table table2; int i=0; ControlP5 cp5; ControlP5 ax; PFont font; PFont font2; String[] args = {"YourSketchNameHere"}; SecondApplet sa; PImage fon; PImage fon2; PImage titlebaricon; //     void setup() { titlebaricon = loadImage("data/myicon.png"); surface.setIcon(titlebaricon); surface.setTitle(" "); //     ))) printArray(Serial.list()); port = new Serial(this, "/dev/ttyUSB0", 9600); // com  ( Ubuntu,  Windows  //com port) cp5 = new ControlP5(this); font = createFont("Arial", 20); //  fon = loadImage("data/phil.bmp"); //     fon2 = loadImage("data/fon2.bmp"); /*        void settings()*/ PImage img; img = loadImage("data/image1.bmp"); // -     "data"  image(img, 0, 0); tint(0, 255, 0, 126); //  - 50%      image(img, 50, 0); PImage img2; img2 = loadImage("data/image2.bmp"); image(img2, 0, 0); image(img2, 50, 0); //       // tint()       . PImage img3; //       . img3 = loadImage("data/image3.bmp"); image(img3, 0, 0); image(img3, 50, 0); PImage img4; img4 = loadImage("data/image4.bmp"); image(img4, 0, 0); image(img4, 50, 0); PImage img5; img5 = loadImage("data/imagezapros.bmp"); image(img5, 0, 0); image(img5, 50, 0); PImage img6; img6 = loadImage("data/temper.bmp"); image(img6, 0, 0); image(img6, 50, 0); cp5.addButton("Relay_1") .setSize (100, 50) .setPosition(50, 50) .setFont(font) .setImage(img) .setLabel(" 1"); cp5.addButton("Relay_2") .setSize (100, 50) .setPosition(250, 50) .setFont(font) .setImage(img2) .setLabel(" 2"); cp5.addButton("Relay_ON") .setSize (300, 50) .setPosition(50, 120) .setFont(font) .setLabel(" ") .setImage(img3); cp5.addButton("Relay_OFF") .setSize (300, 50) .setPosition(50, 190) .setFont(font) .setLabel(" ") .setImage(img4); cp5.addButton("Zapros_sostoyaniya") .setSize (300, 50) .setPosition(50, 260) .setFont(font) .setLabel(" ") .setImage(img5); recived=cp5.addTextfield(" ") .setSize(380,100) .setPosition(10, 340) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); cp5.addButton("temps") .setSize (380, 50) .setPosition(10, 500) .setColorBackground(color(70, 100, 0)) .setColorForeground(color(0, 0, 0)) .setFont(font) .setLabel("") .setImage(img6); } void settings(){ size(400, 560); } void draw() { background(fon); fill(0, 0, 0); textFont(font); text(" ", 135, 30); int s = second(); int m = minute(); int h = hour(); int d = day(); int mo = month(); int y = year(); DateTab = str(d)+ "." + str(mo) + "." + str(y) + (" ") + str(h) + (":") + str(m) + (":") + str(s); if ( port.available() > 0){ val = port.readString(); if(data2==0){ //        -   , recived.setText(val); // - com ,  -   . } /* data2 - ,    Second Applet     textfield   . */ } sa = new SecondApplet(); } void Relay_1(){ port.write("1"); } void Relay_2(){ port.write("2"); } void Relay_ON(){ port.write("3"); } void Relay_OFF(){ port.write("0"); } void Zapros_sostoyaniya(){ data2=0; port.write("4"); } void save123(){ //       data2=1; port.write("4"); delay(600); port.available(); Dat=DateTab + " " + val; table2 = loadTable("data/Journaltemp.ods"); //   table2.setString(0, 0, "t"); //     0.0 table2.removeColumn(0); /*  0,          */ TableRow newRow = table2.addRow(); //   newRow.setString(0, Dat); /*  0        ( )*/ saveTable(table2, "data/Journaltemp.ods"); //    data2=0; } void temps(){ PApplet.runSketch(args, sa); //  Second Applet } public class SecondApplet extends PApplet { public void setup(){ surface.setTitle(" "); surface.setIcon(titlebaricon); font2 = createFont("Arial", 20); /*   , ,    ..*/ ax = new ControlP5(this); ax.addButton("test1") .setSize(159, 70) .setPosition(1, 300) .setFont(font2) .setLabel(""); ax.addButton("test2") .setSize(158, 70) .setPosition(161, 300) .setFont(font2) .setLabel(""); ax.addButton("test3") .setSize(159, 70) .setPosition(320, 300) .setFont(font2) .setLabel(""); ax.addButton("test4") .setSize(159, 70) .setPosition(480, 300) .setFont(font2) .setLabel(""); ax.addButton("test5") .setSize(159, 70) .setPosition(640, 300) .setFont(font2) .setLabel(""); ax.addButton("obj") .setSize (790, 50) .setPosition(5, 540) .setColorBackground(color(70, 100, 0)) .setColorForeground(color(0, 0, 0)) .setFont(font) .setLabel(""); recived1 = ax.addTextfield(" ") .setSize(157, 70) .setPosition(2, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived2=ax.addTextfield(" ") .setSize(156, 70) .setPosition(162, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived3=ax.addTextfield(" ") .setSize(157, 70) .setPosition(321, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived4=ax.addTextfield(" ") .setSize(157, 70) .setPosition(481, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); recived5=ax.addTextfield(" ") .setSize(157, 70) .setPosition(641, 370) .setColorValueLabel(0) .setFont(font) .setColorBackground(color(255, 255, 255)); } public void settings() { size(800, 600); //    } public void draw() { background(fon2); //  )) textFont(font2); } public void test1(){ recived1.setText(" 11111"); } public void test2(){ recived2.setText(" 22222"); } public void test3(){ recived3.setText(" 33333"); //    Second Applet } public void test4(){ recived4.setText(" 44444"); } public void test5(){ recived5.setText(" 55555"); } public void obj(){ save123(); /*              */ } public void exitActual(){} //      [X]    . } //      ,       [X].
      
      







The program I received:



Time

Two



I ask you not to kick much - my first project on Processing, I hope someone can figure it out with such simple things as it turned out. And if someone tells me how to optimize my g ... code - I will be glad.



All Articles