hello, sorry i tried to read data from arduino to processing ide 4.0.1 but am getting wrong last value on the gauge in display window of processing ide i don't know why it happen that, other 5 values that am getting are fine but the last 6 value is wrong. below is the full processing ide code.
// Import Meter library
import meter.*;
// Import serial library
import processing.serial.*;
Serial serial_port = null; // the serial port
// serial port buttons
Button btn_serial_up; // move up through the serial port list
Button btn_serial_dn; // move down through the serial port list
Button btn_serial_connect; // connect to the selected serial port
Button btn_serial_disconnect; // disconnect from the serial port
Button btn_serial_list_refresh; // refresh the serial port list
String serial_list; // list of serial ports
int serial_list_index = 0; // currently selected serial port
int num_serial_ports = 0; // number of serial ports in the list
Meter m, m2, m3, m4, m5, m6, m7, m8;
void setup(){
// First we need to create a empty window
//size(1860, 800); // Size of the window (width, height)
fullScreen();
//background(10, 10, 82); // Background color of window (R,G,B)
// create the buttons
btn_serial_up = new Button("^", 140, 715, 40, 20);
btn_serial_dn = new Button("v", 140, 675, 40, 20);
btn_serial_connect = new Button("Connect", 190, 647, 100, 25);
btn_serial_disconnect = new Button("Disconnect", 190, 690, 100, 25);
btn_serial_list_refresh = new Button("Refresh", 190, 730, 100, 25);
// get the list of serial ports on the computer
serial_list = Serial.list()[serial_list_index];
//println(Serial.list());
//println(Serial.list().length);
// get the number of serial ports in the list
num_serial_ports = Serial.list().length;
}
void mousePressed() {
// up button clicked
if (btn_serial_up.MouseIsOver()) {
if (serial_list_index > 0) {
// move one position up in the list of serial ports
serial_list_index--;
serial_list = Serial.list()[serial_list_index];
}
}
// down button clicked
if (btn_serial_dn.MouseIsOver()) {
if (serial_list_index < (num_serial_ports - 1)) {
// move one position down in the list of serial ports
serial_list_index++;
serial_list = Serial.list()[serial_list_index];
}
}
// Connect button clicked
if (btn_serial_connect.MouseIsOver()) {
if (serial_port == null) {
// connect to the selected serial port
serial_port = new Serial(this, Serial.list()[serial_list_index], 9600);
}
}
// Disconnect button clicked
if (btn_serial_disconnect.MouseIsOver()) {
if (serial_port != null) {
// disconnect from the serial port
serial_port.stop();
serial_port = null;
}
}
// Refresh button clicked
if (btn_serial_list_refresh.MouseIsOver()) {
// get the serial port list and length of the list
serial_list = Serial.list()[serial_list_index];
num_serial_ports = Serial.list().length;
}
}
void draw(){
//background(0, 153, 255); // Background color of window (R,G,B)
background(10, 10, 82);
textSize(30);
fill(255, 255, 255); // Font color , (r,g,b)
text("TIME:", 320, 712); // ("text", x, y)
text("DATE:", 600, 712); // ("text", x, y)
//background(204);
int time[]={hour(),minute(),second(),day(),month(),year()};
// Print time on processing console in the format 00 : 00 : 00
text(nf(time[0],2) + " : " + nf(time[1],2) + " : "+ nf(time[2],2),400,712);
// Print Date as DD/MM/YYYY
text(time[3] + " - " + time[4] + " - "+ time[5],681,712);
// TANK 1
m = new Meter(this, 2, 51); // here 25, 10 are x and y coordinates of meter's upper left corner
m.setTitleFontSize(20);
m.setTitleFontName("Arial bold");
m.setTitle("TANK 1 LEVEL(%)");
// Set a warning if sensor value is too low.
m.setLowSensorWarningActive(true);
m.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m.setHighSensorWarningActive(true);
m.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m.setScaleLabels(scaleLabels);
m.setScaleFontSize(18);
m.setScaleFontName("Times new roman bold");
m.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m.setArcColor(color(199, 113, 178));
m.setArcThickness(15);
m.setMaxScaleValue(100);
m.setMinInputSignal(0);
m.setMaxInputSignal(100);
m.setNeedleThickness(3);
// TANK 2
// lets take some refference from first meter
int mx = m.getMeterX(); // x coordinate of m
int my = m.getMeterY(); // y coordinate of m
int mw = m.getMeterWidth();
m2 = new Meter(this, mx + mw + 20, my);
m2.setTitleFontSize(20);
m2.setTitleFontName("Arial bold");
m2.setTitle("TANK 2 LEVEL(%)");
// Set a warning if sensor value is too low.
m2.setLowSensorWarningActive(true);
m2.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m2.setHighSensorWarningActive(true);
m2.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels2 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m2.setScaleLabels(scaleLabels2);
m2.setScaleFontSize(18);
m2.setScaleFontName("Times new roman bold");
m2.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m2.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m2.setArcColor(color(141, 113, 178));
m2.setArcThickness(15);
m2.setMaxScaleValue(100);
m2.setMinInputSignal(0);
m2.setMaxInputSignal(100);
m2.setNeedleThickness(3);
////WATER TANK 3
// lets take some refference from first meter
int mx1 = m.getMeterX(); // x coordinate of m
int my1 = m.getMeterY(); // y coordinate of m
int mw1 = m.getMeterWidth();
//m3 = new Meter(this, 250, 400);
m3 = new Meter(this, mx + mw + 478, my);
m3.setTitleFontSize(20);
m3.setTitleFontName("Arial bold");
m3.setTitle("TANK 3 LEVEL(%)");
// Set a warning if sensor value is too low.
m3.setLowSensorWarningActive(true);
m3.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m3.setHighSensorWarningActive(true);
m3.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels3 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m3.setScaleLabels(scaleLabels3);
m3.setScaleFontSize(18);
m3.setScaleFontName("Times new roman bold");
m3.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m3.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m3.setArcColor(color(141, 113, 178));
m3.setArcThickness(15);
m3.setMaxScaleValue(100);
m3.setMinInputSignal(0);
m3.setMaxInputSignal(100);
m3.setNeedleThickness(3);
// TANK 4
m4 = new Meter(this, 2, 330); // here 25, 10 are x and y coordinates of meter's upper left corner
m4.setTitleFontSize(20);
m4.setTitleFontName("Arial bold");
m4.setTitle("TANK 4 LEVEL(%)");
// Set a warning if sensor value is too low.
m4.setLowSensorWarningActive(true);
m4.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m4.setHighSensorWarningActive(true);
m4.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels4 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m4.setScaleLabels(scaleLabels4);
m4.setScaleFontSize(18);
m4.setScaleFontName("Times new roman bold");
m4.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m4.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m4.setArcColor(color(141, 113, 178));
m4.setArcThickness(15);
m4.setMaxScaleValue(100);
m4.setMinInputSignal(0);
m4.setMaxInputSignal(100);
m4.setNeedleThickness(3);
// TANK 5
// lets take some refference from first meter
int mx2 = m.getMeterX(); // x coordinate of m
int my2 = m.getMeterY(); // y coordinate of m
int mw2 = m.getMeterWidth();
m5 = new Meter(this, 460, 330); // here 25, 10 are x and y coordinates of meter's upper left corner
m5.setTitleFontSize(20);
m5.setTitleFontName("Arial bold");
m5.setTitle("TANK 5 LEVEL(%)");
// Set a warning if sensor value is too low.
m5.setLowSensorWarningActive(true);
m5.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m5.setHighSensorWarningActive(true);
m5.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels5 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m5.setScaleLabels(scaleLabels5);
m5.setScaleFontSize(18);
m5.setScaleFontName("Times new roman bold");
m5.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m5.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m5.setArcColor(color(141, 113, 178));
m5.setArcThickness(15);
m5.setMaxScaleValue(100);
m5.setMinInputSignal(0);
m5.setMaxInputSignal(100);
m5.setNeedleThickness(3);
// TANK 6
// lets take some refference from first meter
int mx3 = m.getMeterX(); // x coordinate of m
int my3 = m.getMeterY(); // y coordinate of m
int mw3 = m.getMeterWidth();
m6 = new Meter(this, 920, 330); // here 25, 10 are x and y coordinates of meter's upper left corner
m6.setTitleFontSize(20);
m6.setTitleFontName("Arial bold");
m6.setTitle("TANK 6 LEVEL(%)");
// Set a warning if sensor value is too low.
m6.setLowSensorWarningActive(true);
m6.setLowSensorWarningValue((int)10);
// Set a warning if sensor value is too high.
m6.setHighSensorWarningActive(true);
m6.setHighSensorWarningValue((int)90);
// Change meter scale values
String[] scaleLabels6 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
m6.setScaleLabels(scaleLabels6);
m6.setScaleFontSize(18);
m6.setScaleFontName("Times new roman bold");
m6.setScaleFontColor(color(200, 30, 70));
// We can also display the value of meter
m6.setDisplayDigitalMeterValue(true);
// Lets do some more modifications so our meter looks nice
m6.setArcColor(color(141, 113, 178));
m6.setArcThickness(15);
m6.setMaxScaleValue(100);
m6.setMinInputSignal(0);
m6.setMaxInputSignal(100);
m6.setNeedleThickness(3);
textSize(15);
if (serial_port != null && serial_port.available()> 0){
//background(10, 10, 82); // Background color of window (R,G,B)
String val = serial_port.readString(); // read incoming string on serial port
// First we need to separate values
String[] list = split(val, ','); // splits value separated by ','
int tank_1 = int(list[0]); // Tank1
int tank_2 = int(list[1]); // Tank2
int tank_3 = int(list[2]); // Tank3
int tank_4 = int(list[3]); // Tank4
int tank_5 = int(list[4]); // Tank5
int tank_6 = int(list[5]); // Tank6
m.updateMeter(int(tank_1)); // int is used due to updateMeter accepts only int values
m2.updateMeter(int(tank_2));
m3.updateMeter(int(tank_3));
m4.updateMeter(int(tank_4));
m5.updateMeter(int(tank_5));
m6.updateMeter(int(tank_6));
println("Water Tank 1 Level: " + tank_1 + " % " + "Water Tank 2 Level: " + tank_2+ "%"+"Water Tank 3 Level: "+tank_3+ "%"+"Water Tank 4 Level: "+tank_4+ "%"+"Water Tank 5 Level: "+tank_5+ "%" +"Water Tank 6 Level: "+tank_6+ "%");
}
// draw the buttons in the application window
btn_serial_up.Draw();
btn_serial_dn.Draw();
btn_serial_connect.Draw();
btn_serial_disconnect.Draw();
btn_serial_list_refresh.Draw();
// draw the text box containing the selected serial port
DrawTextBox("Select Port", serial_list, 10, 675, 120, 60);
}
// function for drawing a text box with title and contents
void DrawTextBox(String title, String str, int x, int y, int w, int h)
{
fill(255);
rect(x, y, w, h);
fill(0);
textAlign(LEFT);
textSize(14);
text(title, x + 10, y + 10, w - 20, 20);
textSize(12);
text(str, x + 10, y + 40, w - 20, h - 10);
}
// button class used for all buttons
class Button {
String label;
float x; // top left corner x position
float y; // top left corner y position
float w; // width of button
float h; // height of button
// constructor
Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
label = labelB;
x = xpos;
y = ypos;
w = widthB;
h = heightB;
}
// draw the button in the window
void Draw() {
fill(218);
stroke(141);
rect(x, y, w, h, 10);
textAlign(CENTER, CENTER);
fill(0);
text(label, x + (w / 2), y + (h / 2));
}
// returns true if the mouse cursor is over the button
boolean MouseIsOver() {
if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
return true;
}
return false;
}
}