Serial Read data not updating with new value.
in
Integration and Hardware
•
1 month ago
IM fairly new to Processing and im sure this is a simple one line fix for your Pro's but im struggling to find a solution.
I have an arduino sketch sending serial data which i can read in the Serial Monitor.
Arduino Sketch:
- int data0; //,data1, data2, data3;
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- data0 = analogRead(0); // read sensor in analog input 0
- // data1 = analogRead(1); // read sensor in analog input 1
- // data2 = analogRead(2); // read sensor in analog input 2
- // data3 = analogRead(3); // read sensor in analog input 3
- Serial.println(data0, DEC); // print sensor 1
- // Serial.print(","); // print ',' // print ','
- // Serial.print(data1, DEC); // print sensor 2
- // Serial.print(","); // print ','
- // Serial.print(data2, DEC); // print sensor 3
- // Serial.print(","); // print ','
- // Serial.println(data3, DEC); // print sensor 4 and newline (println)
- delay(1000); // wait 200ms for next reading
- }
Im my Processing Sketch the data will not update in the Rectangle. It will show the first data value it read, then not update with the latest value in the "TANK oC" Rectangle.
Lines 588-618 is the Serial Read Code.
Here is my Processing Sketch:
- //********************************** AQUATROLLER V2.0 GUI ***************************************
- //****************************** Serial Port Initialisation *************************************
- import processing.serial.*;
- Serial Port;
- //********************************************************************************************
- // Variables Initialisation:
- //********************************************************************************************
- // Mix Start Button Image:
- PImage Mix_Start_btn; // Retreives Button image from data folder
- PImage Mix_Start_btn_pressed; // Retreives Button image from data folder
- int b1X1 = 15; // Top Left X Button Co-ordinate
- int b1Y1 = 480; // Top Left Y Button y Co-ordinate
- int b1X2 = 85; // Bottom Right X Button Co-ordinate
- int b1Y2 = 505; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- //Mix Stop Button Image:
- PImage Mix_Stop_btn; // Retreives Button image from data folder
- PImage Mix_Stop_btn_pressed; // Retreives Button image from data folder
- int b2X1 = 125; // Top Left X Button Co-ordinate
- int b2Y1 = 480; // Top Left Y Button y Co-ordinate
- int b2X2 = 195; // Bottom Right X Button Co-ordinate
- int b2Y2 = 505; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- //AWC Start Button Image:
- PImage AWC_Start_btn; // Retreives Button image from data folder
- PImage AWC_Start_btn_pressed; // Retreives Button image from data folder
- int b3X1 = 15; // Top Left X Button Co-ordinate
- int b3Y1 = 395; // Top Left Y Button y Co-ordinate
- int b3X2 = 85; // Bottom Right X Button Co-ordinate
- int b3Y2 = 420; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- // AWC Stop Button Image:
- PImage AWC_Stop_btn; // Retreives Button image from data folder
- PImage AWC_Stop_btn_pressed; // Retreives Button image from data folder
- int b4X1 = 125; // Top Left X Button Co-ordinate
- int b4Y1 = 395; // Top Left Y Button y Co-ordinate
- int b4X2 = 195; // Bottom Right X Button Co-ordinate
- int b4Y2 = 420; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- // Reset Button Image:
- PImage Reset_btn; // Retreives Button image from data folder
- PImage Reset_btn_pressed; // Retreives Button image from data folder
- int b5X1 = 125; // Top Left X Button Co-ordinate
- int b5Y1 = 313; // Top Left Y Button y Co-ordinate
- int b5X2 = 195; // Bottom Right X Button Co-ordinate
- int b5Y2 = 337; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- // Mute Button Image:
- PImage Mute_btn; // Retreives Button image from data folder
- PImage Mute_btn_pressed; // Retreives Button image from data folder
- int b6X1 = 15; // Top Left X Button Co-ordinate
- int b6Y1 = 313; // Top Left Y Button y Co-ordinate
- int b6X2 = 85; // Bottom Right X Button Co-ordinate
- int b6Y2 = 335; // Bottom Right Y Button Co-ordinate
- //***********************************************************************************
- //Tank Layout Image:
- PImage Tank_Layout; // Retreives Button image from data folder
- int b7X1 = 650; // Top Left X Image Co-ordinate
- int b7Y1 = 35; // Top Left Y Image Co-ordinate
- //variables received from the Arduino ***********************************************
- //you can just add more data variable or change the name of this data...
- //take a look at "void serialEvent (Serial Port)"
- //Be aware that this is very related with the Arduino's program.
- int data0; //data 1 received from Arduino
- int data1; //data 2 received from Arduino
- int data2; //data 3 received from Arduino
- int data3; //data 4 received from Arduino
- int data4; //data 5 received from Arduino
- int data5; //data 6 received from Arduino
- //***********************************************************************************/
- void setup ()
- {
- //************************************GUI Initialisation*************************************************
- //*************************************************************************************************************
- //*********************************** Main Window Config ***********************************************************************
- PFont f1 = loadFont("Cambria-Bold-48.vlw"); //Load Fonts file from Data Folder
- size (1020,600); //Main GUI Window Size
- background(209,207,188); //Main GUI Window Background Color - 209,207,188
- //**************** Left Status Panel ***********************************************************
- stroke(0); // Set Line Colour for panel border
- fill(203,195,195); // Set Panel boarder Fill Colour
- rect(3,48,204,304); // Location and Panel Size
- fill(0); // Set Panel Fill Colour
- stroke(255); // Set Line Colour
- rect(5,50,200,300); // Location and Panel Size
- textFont(f1,20); // Label Font
- fill(0); // Font Colour
- text("SYSTEM STATUS",30,47); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255); // Set Line Colour
- rect(10,55,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("CIRC PUMP",15,69); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,80,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("HEATER",15,94); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,105,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("CHILLER",15,119); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,130,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("AWC DRAIN",15,144); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,155,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("AWC FILL",15,169); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,180,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("SALT MIX",15,194); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,205,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("FUGE",15,219); // Label Text & Location
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(10,230,190,20); // Location and Panel Size
- textFont(f1,12); // Label Font
- fill(255); // Font Colour
- text("ATO",15,244); // Label Text & Location
- fill(255,0,21); // Set Panel Fill Colour
- stroke(255);
- rect(10,280,190,65); // Location and Panel Size
- textFont(f1,20); // Label Font
- fill(0); // Font Colour
- text("ALARM",73,305); // Label Text & Location
- //***************************************** Lower Left Button Panel*********************************************
- fill(0); // Set Panel Fill Colour
- stroke(255);
- rect(5,365,200,200); // Location and Panel Size
- textFont(f1,15); // Label Font
- fill(255); // Font Colour
- text("SALT MIX",73,470); // Label Text & Location
- textFont(f1,15); // Label Font
- fill(255); // Font Colour
- text("AUTO WATER CHANGE",25,385); // Label Text & Location
- //******************************************************************************************************************
- //************************ COM Port Config *************************************************************************
- Port = new Serial(this, "COM4", 9600); //starts the Serial port - COM4
- Port.bufferUntil('\n'); //clean the buffer
- // Draw Mix Start Button:
- Mix_Start_btn = loadImage("Mix_Start_btn.jpg");
- image(Mix_Start_btn,b1X1,b1Y1);
- //**********************
- // Draw Mix Stop Button:
- Mix_Stop_btn = loadImage("Mix_Stop_btn.jpg");
- image(Mix_Stop_btn,b2X1,b2Y1);
- //**********************
- // Draw AWC Start Button:
- AWC_Start_btn = loadImage("AWC_Start_btn.jpg");
- image(AWC_Start_btn,b3X1,b3Y1);
- //**********************
- // Draw AWC Stop Button:
- AWC_Stop_btn = loadImage("AWC_Stop_btn.jpg");
- image(AWC_Stop_btn,b4X1,b4Y1);
- //**********************
- // Draw Reset Button:
- Reset_btn = loadImage("Reset_btn.jpg");
- image(Reset_btn,b5X1,b5Y1);
- //**********************
- // Draw Mute Button:
- Mute_btn = loadImage("Mute_btn.jpg");
- image(Mute_btn,b6X1,b6Y1);
- //**********************
- // Draw Tank Layout Image:
- Tank_Layout = loadImage("Tank_Layout.jpg");
- image(Tank_Layout,b7X1,b7Y1);
- //**********************
- }//*******************************************************************************************
- //********************************************************************************************/
- void draw (){ //starts loop **************************************************************
- //********************************************************************************************/
- PFont f1 = loadFont("DaunPenh-48.vlw"); //fonts
- // headerBackgroundProgram ();
- // grid (); //you can use a grid to make visualization easier
- mousePosition (); //you can use it to show the position of the mouse inside the window
- /*notice that in the Arduino program, data 0 is the value read in the analog input 0.
- -- the other data are fixed values */
- //shows data 0 inside an white rectangle *************************************************
- stroke(0); // Panel Border
- fill(203,195,195);
- rectMode(CORNERS);
- rect(548,48,612,72);
- stroke(0); // Data Panel
- fill(255);
- rectMode(CORNERS);
- rect(550,50,610,70);
- textFont(f1,20); // Data Panel label
- fill(0);
- text("TANK:",505,66);
- textFont(f1,28); // Data
- fill(0);
- text(+ data0,555,66);
- textFont(f1,15); // 'Degrees' Symbol Label
- fill(0);
- text(char(248),615,60);
- textFont(f1,20); // 'C' Label
- fill(0);
- text("C",623,66);
- //************************************************************************************
- //shows data 1 inside an white rectangle *************************************************
- stroke(0);
- fill(203,195,195);
- rectMode(CORNERS);
- rect(548,98,612,122);
- stroke(0);
- fill(255);
- rectMode(CORNERS);
- rect(550,100,610,120);
- textFont(f1,20);
- fill(0);
- text("TANK:", 505,116);
- textFont(f1,28);
- fill(0);
- text(+ data1, 555,116);
- textFont(f1,20);
- fill(0);
- text("S.G.",615,116);
- //************************************************************************************
- //shows data 2 inside an white rectangle *************************************************
- stroke(0);
- fill(203,195,195);
- rectMode(CORNERS);
- rect(548,148,612,172);
- stroke(0);
- fill(255);
- rectMode(CORNERS);
- rect(550,150,610,170);
- textFont(f1,20);
- fill(0);
- text("TANK:",505,166);
- textFont(f1,28);
- fill(0);
- text(+ data2,555,166);
- textFont(f1,20);
- fill(0);
- text("Ph",615,166);
- //************************************************************************************
- //shows data 3 inside an white rectangle *************************************************
- stroke(0);
- fill(255);
- rectMode(CORNERS);
- rect(260,280,360, 310);
- textFont(f1,28);
- fill(0);
- text("Data3 ", 237, 302);
- text(+ data3, 300, 301);
- //************************************************************************************
- //shows data 4 inside an white rectangle *************************************************
- stroke(0);
- fill(255);
- rectMode(CORNERS);
- rect(260,325,360, 355);
- textFont(f1,28);
- fill(0);
- text("Data4 ", 237, 351);
- text(+ data4, 300, 351);
- //************************************************************************************
- //shows data 5 inside an white rectangle *************************************************
- stroke(0);
- fill(255);
- rectMode(CORNERS);
- rect(260,370,360, 400);
- textFont(f1,28);
- fill(0);
- text("Data5 ", 237, 396);
- text(+ data5, 300, 396);
- //************************************************************************************
- //Mix Start Button:
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b1X1 && mouseX< b1X2 && mouseY> b1Y1 && mouseY< b1Y2 && mousePressed){
- Mix_Start_btn_pressed = loadImage("Mix_Start_btn_pressed.jpg");
- image(Mix_Start_btn_pressed,b1X1,b1Y1);} //this little line makes the button change to a "pressed button pic"
- else{
- image(Mix_Start_btn,b1X1,b1Y1);}
- //***********************************************************************************/
- //Mix Stop Button:
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b2X1 && mouseX< b2X2 && mouseY> b2Y1 && mouseY< b2Y2 && mousePressed){
- Mix_Stop_btn_pressed = loadImage("Mix_Stop_btn_pressed.jpg");
- image(Mix_Stop_btn_pressed,b2X1,b2Y1);}
- else{
- image(Mix_Stop_btn,b2X1,b2Y1);}
- //***********************************************************************************/
- //AWC Start Button:
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b3X1 && mouseX< b3X2 && mouseY> b3Y1 && mouseY< b3Y2 && mousePressed){
- AWC_Start_btn_pressed = loadImage("AWC_Start_btn_pressed.jpg");
- image(AWC_Start_btn_pressed,b3X1,b3Y1);}
- else{
- image(AWC_Start_btn,b3X1,b3Y1);}
- //***********************************************************************************/
- //AWC Stop Button:
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b4X1 && mouseX< b4X2 && mouseY> b4Y1 && mouseY< b4Y2 && mousePressed){
- AWC_Stop_btn_pressed = loadImage("AWC_Stop_btn_pressed.jpg");
- image(AWC_Stop_btn_pressed,b4X1,b4Y1);}
- else{
- image(AWC_Stop_btn,b4X1,b4Y1);}
- //***********************************************************************************/
- //Reset Button:
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b5X1 && mouseX< b5X2 && mouseY> b5Y1 && mouseY< b5Y2 && mousePressed){
- Reset_btn_pressed = loadImage("Reset_btn_pressed.jpg");
- image(Reset_btn_pressed,b5X1,b5Y1);}
- else{
- image(Reset_btn,b5X1,b5Y1);}
- //***********************************************************************************/
- //Mute Button
- //this little piece of program, makes the button shows up on the screen and makes it change it's apperence when you click on it ************
- //note that the "if" have the same parameters of the rectangle of the button.
- if( mouseX> b6X1 && mouseX< b6X2 && mouseY> b6Y1 && mouseY< b6Y2 && mousePressed){
- Mute_btn_pressed = loadImage("Mute_btn_pressed.jpg");
- image(Mute_btn_pressed,b6X1,b6Y1);}
- else{
- image(Mute_btn,b6X1,b6Y1);}
- //***********************************************************************************/
- }//*******************************************************************************************/
- //********************************************************************************************/
- void mousePressed(){ //this function has to be named "mousePressed" **************************/
- //it's a Processing function that control the function of the pressed button
- //Mix Start Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 34 to the Arduino ****************************************
- if ( mouseX> b1X1 && mouseX< b1X2 && mouseY> b1Y1 && mouseY< b1Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(12); //Sends the ascii signal
- }//************************************************************
- //Mix Stop Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 35 to the Arduino ****************************************
- if ( mouseX> b2X1 && mouseX< b2X2 && mouseY> b2Y1 && mouseY< b2Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(13); //Sends the ascii signal
- }//************************************************************
- //AWC Start Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 36 to the Arduino ****************************************
- if ( mouseX> b3X1 && mouseX< b3X2 && mouseY> b3Y1 && mouseY< b3Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(10); //Sends the ascii signal
- }//************************************************************
- //AWC Stop Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 37 to the Arduino ****************************************
- if ( mouseX> b4X1 && mouseX< b4X2 && mouseY> b4Y1 && mouseY< b4Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(11); //Sends the ascii signal
- }//************************************************************
- //Reset Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 38 to the Arduino ****************************************
- if ( mouseX> b5X1 && mouseX< b5X2 && mouseY> b5Y1 && mouseY< b5Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(14); //Sends the ascii signal
- }//************************************************************
- //Mute Button:
- //the area in which the mouse needs to click in order to caracterize a button press
- //Sends 39 to the Arduino ****************************************
- if ( mouseX> b6X1 && mouseX< b6X2 && mouseY> b6Y1 && mouseY< b6Y2 ){ //select the area of a valid press -- note that it has to mach with the button size =]
- Port.write(15); //Sends the ascii signal
- }//************************************************************
- }//*******************************************************************************************
- void serialEvent (Serial Port) { //receive the USB data from Arduino ****************************
- //********************************************************************************************/
- String receivedArduino = Port.readStringUntil('\n'); // read the buffer
- receivedArduino = trim(receivedArduino); //erase ALL the possible spaces between the letters
- int data[] = int(split(receivedArduino, ',')); //this function is really nice, it split the string and put a comma between the data
- //it also convert the string in "int"
- //print out the values you got -- just if you want to see =]
- //for (int dataNum=0; dataNum<data.length; dataNum++) {
- //print("data " + dataNum + ": " + data[dataNum] + " ");}
- data0 = data[0]; //data 0 received from Arduino
- data1 = data[1]; //data 1 received from Arduino
- data2 = data[2]; //data 2 received from Arduino
- data3 = data[3]; //data 3 received from Arduino
- data4 = data[4]; //data 4 received from Arduino
- data5 = data[5]; //data 5 received from Arduino
- // add two linefeed after all the sensor values are printed:
- println();
- println();
- Port.write("!"); // send a byte (! - 33) to ask for more data -- It could be any other byte, but this was the chosen one in this program
- }//****************************************************************************************************
- //*****************************************************************************************************/
- // void headerBackgroundProgram (){ //header**********************************************************
- //**************************************************************************************************/
- // PFont f1 = loadFont("DaunPenh-48.vlw"); //font
- // background(150); //Background Color
- // stroke(255);
- // fill(255);
- // rectMode(CORNERS); //a white rectangle
- // rect(0,0,width, 100);
- // textFont(f1,48);
- // fill(0);
- // textAlign(CENTER);
- // text("Arduino USB COM -- William_19", width/2, 60); //the title
- // PImage hulk = loadImage("hulk.jpg"); //a little pic of the hulk
- // image(hulk,20,20);
- // }//****************************************************************************************************
- void grid (){ //grid*******************************************************************************
- //**************************************************************************************************/
- for(int xline=0; xline<width; xline += 10)
- line(xline, 0, xline, width);
- for(int yline=0; yline<height; yline += 10)
- line(0, yline, height, yline);
- }//****************************************************************************************************
- void mousePosition (){ //mousePosition*************************************************************
- //**************************************************************************************************/
- println("X position = " +mouseX);
- println("y position = " +mouseY);
- println("***********************");
- }//****************************************************************************************************
Thanks in Advance,
Richard
1