We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am trying to debugg this code but there is some problems that I am not familiar with.
I am using processing to control and evaluate heating system: two temperature sensors as reading and two actuators, a fan to distribute the heat and a heater that turn off and on.
In general works, but when I added some parts to save a .csv data file the variables that I manage in the code do not refresh at the same rate they do when I don't attempt to save the .csv file.
Here is an example of the first code:
Serial port; // Create object from Serial class
int x,y,val,val1,mouseclick,tid,fan, valold;
int count1,count2,count,counting, energytab;
color myred = color(255,0,0);
color mygreen = color(0,255,0);
color myblue = color(0,0,255);
color mydarkgreen = color(0,128,0);
color mydarkblue = color(0,144,128);
void setup() {
size(900, 620); // size of the user interface window
println(Serial.list()); // prints the active COM-port list
//String arduinoPort = Serial.list()[0]; //COM3
//String arduinoPort = Serial.list()[1]; //COM5
String arduinoPort = Serial.list()[2]; //COM6
port = new Serial(this, arduinoPort, 115200);
//fill(0);
//size(900, 610); // size of the user interface window
rect(80, 40, 790, 540); // Draw the graph window
// Ritar upp boxar
rect(120, 15, 20, 20); // Draw the T1+ box
rect(150, 15, 20, 20); // Draw the T1- box
rect(240, 15, 20, 20); // Draw the T1min+ box
rect(270, 15, 20, 20); // Draw the T1min- box
rect(360, 15, 20, 20); // Draw the T2+ box
rect(390, 15, 20, 20); // Draw the T2- box
rect(490, 15, 35, 20); // Draw the PID box
rect(535, 15, 50, 20); // Draw the AV/PÅ box
rect(595, 15, 20, 20); // Draw the AV/PÅ+ box
rect(625, 15, 20, 20); // Draw the AV/PÅ- box
smooth();
PFont font;
font = createFont("Arial",8,true); // Arial, 16 point, anti-aliasing on
fill(0);
textFont(font);
// Draw the grading lines on left side with every 5 line longer
for(y=580 ; y>40 ; y=y-10) {
if (y==580||y==530||y==480||y==430||y==380||y==330||y==280||y==230||y==180||y==130||y==80) { x=10; }
else { x=0; }
line(75-x, y, 80, y); // Draw the line
}
for(y=0 ; y<800 ; y=y+10) {
if (y==0||y==50||y==100||y==150||y==200||y==250||y==300||y==350||y==400||y==450||y==500||y==550||y==600||y==650||y==700||y==750||y==800) { x=4; }
else { x=0; }
line(80+y, 581, 80+y, 584+x); // Draw the line
}
fill(0);
textSize(14);
for(y=580 ; y>40 ; y=y-50) {
text((580-y)/10, 45, y+5);// Draw the 0 at the bottom under the line
}
text("Temp (°C)", 5, 45);// Draw the Start text
text("Tid 1 sek/pixel", 250, 605);// Draw the Tid text
text("5 pixlar/sträck", 450, 605);// Draw the Tid text
text("50 pixlar/längre sträck", 650, 605);// Draw the Tid text
text("T1:", 95, 30);// Draw the T1 text
text("+", 126, 30);// Draw the T1 + text
text("-", 159, 30);// Draw the T1 - text
text("T1min:", 195, 30);// Draw the T1min text
text("+", 246, 30);// Draw the T1min + text
text("-", 279, 30);// Draw the T1min - text
text("T2:", 335, 30);// Draw the T2 text
text("+", 366, 30);// Draw the T2 + text
text("-", 399, 30);// Draw the T2 - text
text("Fläkt:", 450, 30);// Draw the Fläkt text
text("PID", 496, 30);// Draw the PID text
text("AV/PÅ", 540, 30);// Draw the AV/PÅ text
text("+", 601, 30);// Draw the T2 + text
text("-", 634, 30);// Draw the T2 - text
energytab = 0;
}// end setup()
void draw() {
// Kollar om musknappen är tryckt
if (mousePressed){
mousecheck();
} // end if (mousePressed)
// Kontrollerar om det finns data på serieporten
if (port.available() > 0) { // If data is available,
val = port.read(); // Läser seriedata och sparar det i variabeln val
switch(val){
// Tar emot T1-börvärder - grön linje
case 255:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(mygreen);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Tar emot T2-börvärder - blå linje
case 254:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(myblue);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Tar emot T1 temperatur - röd linje
case 253:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(myred);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Tar emot T2 temperatur - blå linje
case 252:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(mydarkblue);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Tar emot T1minvärder - grön linje
case 251:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(mygreen);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Tar emot elementstatus - röd/grön för av/på linje
case 250:
val1 = port.read(); // read it and store it in val
//// println(val1," ",valold);
//// println(val,"=",val1);
if (val1==0) {
if (valold != val1 && counting == 1){
count2 = tid;
counting = 0;
}
stroke(mygreen);
}
else{
if (valold != val1){
count1 = tid;
counting = 1;
}
stroke(myred);
}
line (81+tid, 579,81+tid, 574);
noStroke();
if (count1 > 0 && count2 > 0 && counting == 0){
count = count2 - count1;
////// text("Tid för energi:", 81+tid, 80);// Draw the Tid text
////// text(count, 140+tid, 80);// Draw the Tid text
/// text("Tid för energi:", 670, 30);// Draw the Tid text
/// text(count, 775, 30);// Draw the Tid text
counting = 2;
}
valold = val1;
break;
// Tar emot fläkthastigheten - blå linje
case 249:
if(fan==0){
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(myblue);
line (81+tid, 41,81+tid, 41+val1*2);
noStroke();
}
else{ // fan==1
}
break;
// Tar emot trigger för att öka tiden
case 248:
//// println(val);
tid++;
if(tid > 789)
tid=0;
break;
// Fläktkontroll
case 247:
val1 = port.read(); // read it and store it in val
fan = val1;
//// println(val,"=",val1);
break;
// Fläkt AV/PÅ min delta värde
case 246:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1);
stroke(myblue);
point (81+tid, 580-(val1-102)*4.85);
noStroke();
break;
// Fläkt AV eller PÅ
case 245:
val1 = port.read(); // read it and store it in val
//// println(val,"=",val1," fan=",fan);
if (val1==0 && fan == 1) {
stroke(mygreen);
}
if (val1==1 && fan == 1) {
stroke(myred);
}
line (81+tid, 41,81+tid, 49);
noStroke();
break;
case 244:
//// println(val);
stroke(255);
//// println("Linjer och tid =",tid);
if (tid <2){
line (81+tid+1, 41, 81+tid+1, 579);
line (81+tid+2, 41, 81+tid+2, 579);
line (81+tid+3, 41, 81+tid+3, 579);
line (81+tid+4, 41, 81+tid+4, 579);
line (81+tid+5, 41, 81+tid+5, 579);
line (81+tid+6, 41, 81+tid+6, 579);
line (81+tid+7, 41, 81+tid+7, 579);
line (81+tid+8, 41, 81+tid+8, 579);
line (81+tid+9, 41, 81+tid+9, 579);
line (81+tid+10, 41, 81+tid+10, 579);
line (81+tid+11, 41, 81+tid+11, 579);
} // end if (tid < 2)
if (tid >1 && tid < 789){
line (81+tid+12, 41, 81+tid+12, 579);
} // end if (tid < 535)
noStroke();
break;
// default:
// println("annat värde =",val1);
// break;
} // end switch(val)
} // end if port.available()
} // end draw()
/************************************************************
* Nedan finns functions() som används av programmet
*
*
*
*
*************************************************************/
void mousecheck(){
int dtime=200; // delay time to avoid mouse click bouncing
//// print("Musklick kontroll ");
//// println(mouseclick);
mouseclick++;
// checking if the mouse is pressed inside the START button
if (mousePressed && mouseX<60 && mouseX>20 && mouseY<80 && mouseY>40 ){
}
// checking if the mouse is pressed inside the STOP button
//// if (mousePressed && mouseX<60 && mouseX>20 && mouseY<140 && mouseY>100 ){
//// delay(dtime);
//// }// end if
// checking if the mouse is pressed inside the SAVE button
//// if (mousePressed && mouseX<60 && mouseX>20 && mouseY<200 && mouseY>160 ){
//// saving = 1;
//// delay(dtime);
//// }// end if the mouse is pressed inside the SAVE button
// checking if the mouse is pressed inside the T1+ button
if (mousePressed && mouseX<140 && mouseX>120 && mouseY<35 && mouseY>15 ){
sendref(1);
delay(dtime);
}// end if the mouse is pressed inside the T1+ button
// checking if the mouse is pressed inside the T1- button
if (mousePressed && mouseX<170 && mouseX>150 && mouseY<35 && mouseY>15 ){
sendref(2);
delay(dtime);
}// end if the mouse is pressed inside the T1- button
// checking if the mouse is pressed inside the T1min+ button
if (mousePressed && mouseX<260 && mouseX>240 && mouseY<35 && mouseY>15 ){
sendref(3);
delay(dtime);
}// end if the mouse is pressed inside the T1min+ button
// checking if the mouse is pressed inside the T1min- button
if (mousePressed && mouseX<290 && mouseX>270 && mouseY<35 && mouseY>15 ){
sendref(4);
delay(dtime);
}// end if the mouse is pressed inside the T1min- button
// checking if the mouse is pressed inside the T2+ button
if (mousePressed && mouseX<380 && mouseX>360 && mouseY<35 && mouseY>15 ){
sendref(5);
delay(dtime);
}// end if the mouse is pressed inside the T2+ button
// checking if the mouse is pressed inside the T2- button
if (mousePressed && mouseX<410 && mouseX>390 && mouseY<35 && mouseY>15 ){
sendref(6);
delay(dtime);
}// end if the mouse is pressed inside the T2- button
// checking if the mouse is pressed inside the PID button
if (mousePressed && mouseX<525 && mouseX>490 && mouseY<35 && mouseY>15 ){
sendref(7);
delay(dtime);
}// end if the mouse is pressed inside the PID button
// checking if the mouse is pressed inside the AV/PÅ button
if (mousePressed && mouseX<585 && mouseX>535 && mouseY<35 && mouseY>15 ){
sendref(8);
delay(dtime);
}// end if the mouse is pressed inside the AV/PÅ button
// checking if the mouse is pressed inside the AV/PÅ+ button
if (mousePressed && mouseX<615 && mouseX>595 && mouseY<35 && mouseY>15 ){
sendref(9);
delay(dtime);
}// end if the mouse is pressed inside the AV/PÅ+ button
// checking if the mouse is pressed inside the AV/PÅ- button
if (mousePressed && mouseX<645 && mouseX>625 && mouseY<35 && mouseY>15 ){
sendref(0);
delay(dtime);
}// end if the mouse is pressed inside the AV/PÅ- button
// check if
////if (mousePressed && mouseX<20 && mouseX>0 && mouseY<20 && mouseY>0 ){
//// exit();
//// }
delay(dtime);
} // end mousecheck()
// procedure to send referens levels to the Arduino
void sendref(int r) {
//// print("Skickar data ");
//// println(r);
port.write(r);
//// println(r);
} // end sendref(int r)
--------
Answers
And this is the code that save the data in a csv file:
java.lang.ArrayIndexOutOfBoundsException: 1 at projekt_ui_processingV2.serialEvent(projekt_ui_processingV2.java:442) at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at processing.serial.Serial.serialEvent(Unknown Source) at jssc.SerialPort$EventThread.run(SerialPort.java:1112)
Can someone help me to debugg this code?
Thanks.
please review your post. it's unreadable.
to format, edit post, highlight the code and press ctrl-o.
Thanks for the tips
The csv file appears like this: id,hour,minute,second,sensor1,sensor2,Setpoint1,Min.Val 1,Setpoint2,Min.Val 2,On/Off Status Sensor 1,On/Off Status Sensor 2,PID or On/off 0,9,19,37,34.47,31.54,34.0,32.0,31.0,29.0,1.0,1.0,0.0 1,9,19,38,34.47,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0 2,9,19,39,34.47,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0 3,9,19,40,33.98,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 4,9,19,41,33.98,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0 5,9,19,42,34.47,31.05,34.0,32.0,31.0,29.0,1.0,1.0,0.0 6,9,19,43,34.47,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 7,9,19,44,33.98,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0 8,9,19,45,33.98,30.57,0.0,0.0,0.0,0.0,0.0,0.0,0.0 9,9,19,46,33.98,30.57,0.0,0.0,0.0,0.0,0.0,0.0,0.0 10,9,19,47,33.98,31.54,34.0,32.0,31.0,29.0,1.0,1.0,0.0 11,9,19,48,34.47,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 12,9,19,49,33.5,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0 13,9,19,50,33.98,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 14,9,19,51,33.98,30.57,0.0,0.0,0.0,0.0,0.0,0.0,0.0 15,9,19,52,33.5,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 16,9,19,53,34.47,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 17,9,19,54,34.47,31.05,0.0,0.0,0.0,0.0,0.0,0.0,0.0 18,9,19,55,33.98,30.57,0.0,0.0,0.0,0.0,0.0,0.0,0.0
As you can see there are a lot of missing data that are not variables but constants, like Setpoint1 or 2 Good reading: 0,9,19,37,34.47,31.54,34.0,32.0,31.0,29.0,1.0,1.0,0.0 Bad reading: 1,9,19,38,34.47,31.54,0.0,0.0,0.0,0.0,0.0,0.0,0.0
And time to time I get this error meassegae too:
(you can format csv the same way you format code... leave a blank line above and below)
this suggests you are trying to access elements of an array that don't exist.
this looks like a likely candidate, given that it's using 1 as the index (yes, the line number is a bit out but that's because the pre-processor adds a bunch of imports. you can see the generated java file by exporting the project)
sensVal is generated by this
sensValTemp = splitTokens(sensVal[0], "ù");
which looks a bit odd, accented character and all.try printing the size of sensValTemp after line 412...
So I did save the size of sensVal and in fact changes depending on if the temporal variable has saved or not all the values that I request.
The reason from the odd character is becasue the string of data read from the arduino give a series of values that are not all numbers, so in some way last year I manage to use that character to decoded the series. Take a look on the pic.
I think that there is the issue. I did this code in a rush last year to fix the improve version but this was my first and only time I used processing. Now I have to do the same project but since last year I have not used it.