We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › What is wrong with my code please
Page Index Toggle Pages: 1
What is wrong with my code please? (Read 1132 times)
What is wrong with my code please?
Oct 27th, 2008, 10:35pm
 
Please can someone take a look at this program and tell me why I get errors all the time......

Code:
import processing.serial.*;

int linefeed = 10;
Serial myPort;
float[][] lastTemp = new float[5][180];
int counter = 0;
boolean firstTime = true;
int MinVal, MaxVal;
float diff = 0, TempDiff = 0;;
float sensor1, sensor2;
int boxHeight = 200; // set the height of the graph box in case we want to change it later
PrintWriter output;

void setup()
{
    int s = second();  // Values from 0 - 59
    int m = minute();  // Values from 0 - 59
    int h = hour();    // Values from 0 - 23
    int d = day();    // Values from 1 - 31
    int mo = month();  // Values from 1 - 12
    int y = year();   // 2003, 2004, 2005, etc.
   
 output = createWriter(str(y) + str(mo) + str(d) + str(h) + str(m) + str(s) + "Temperature_Log.txt");
 println(Serial.list());
 size(650,450); // Set the window size
 background(50, 50, 50); // make it grey
 myPort = new Serial(this, Serial.list()[0], 9600);
 //read bytes until you get a linefeed
 myPort.bufferUntil(linefeed);
}

void draw()
{
   
}

// serialEvent is run automatically whenever the buffer reaches byte value
// set in the bufferUntil() method

void serialEvent(Serial myPort)
{
// read the serial buffer
 String myString=myPort.readStringUntil(linefeed);
 print ("String-" + myString);

 // if you get any bytes other than linefeed:
 if (myString != null)
 {
   myString = trim(myString);    // split the string at the commas and conver sections into integers
   float sensors[] = float(split(myString, ','));
   
    sensor1 = sensors[0];
    sensor2 = sensors[1];
   
    int s = second();  // Values from 0 - 59
    int m = minute();  // Values from 0 - 59
    int h = hour();    // Values from 0 - 23
    int d = day();    // Values from 1 - 31
    int mo = month();  // Values from 1 - 12
    int y = year();   // 2003, 2004, 2005, etc.
 
   
    TempDiff = truncate(sensor2-sensor1);
    if (TempDiff<0) {TempDiff=0;}
    if (TempDiff>1) {TempDiff = 1;}

    output.println(y + "/" + mo + "/" + d + "  " + h + ":" + m + ":" + s + "   " + sensor1 + "   " + sensor2 + "   " + TempDiff );
    output.flush();
   
   if (firstTime)
   {
     initialiseArray(sensors[0]);
     firstTime = false;
   }
   
   for (int sensorNum = 0; sensorNum < 4; sensorNum++)     //put values into array
   {
      lastTemp[sensorNum][counter] = sensors[sensorNum];
   }
   lastTemp[4][counter] = TempDiff;
   
   MinVal = 999; // Reset the values
   MaxVal = -999;
   for (int i=0; i <180; i++)     // Find the max and min values from the current array
   {
     for (int j=0; j<2; j++)
     {
       findMinMax(lastTemp[j][i]);
     }
   }
     
   drawGraph();
   counter++;
       if (counter == 180)
       {
        counter = 0;
       }
       
   drawIndicator();
   
         
    fill(255, 255, 255);
    PFont font;
    font = loadFont("ArialMT-16.vlw");
    textFont(font, 12);
    String hFill = "";
    if (m<10) {hFill = "0";}
    println (m + "hFill = " + hFill);
    text (y + "/" + mo + "/" + d + "    " + h + ":" + hFill + m, 300, 15);
   
 }
}

void drawGraph()
{
   smooth(); // Set antialising on
   background (50, 50, 50);  // Clear screen
   
     float ix = 0;
     for (int j=0; j<=boxHeight; j++) {
       stroke (int (ix), int (ix), int (ix)  );
 
          line(48, j+50,588, j+50);
           ix=ix + (256/boxHeight);    
     }
   
   
   PFont font;
 // The font must be located in the sketch's
 // "data" directory to load successfully
 font = loadFont("ArialMT-16.vlw");

 strokeWeight(1);
  // draw the tenth's of degrees lines
  stroke(0, 0, 40);
  for (float i=0; i<boxHeight; i=i+((boxHeight/diff)/10) )
  {
    line(48, i+50, 588, i+50);
  }
  int tempMax = MaxVal;
Re: What is wrong... ?
Reply #1 - Oct 27th, 2008, 10:36pm
 
Part 2.........

Code:

// Draw the degree lines

line(228, 53, 228, boxHeight+50);
line(408, 53, 408, boxHeight+50);

for (float i=0; i<=boxHeight; i=i+(boxHeight/diff) )
{
stroke(0, 0, 80);
line(48, i+50, 588, i+50);
fill (255, 255, 255);
textFont(font, 11);
text (tempMax + "°C", 595, i+55);
text("Clear", 12,60);
text("Cloudy", 12, boxHeight+50);

tempMax--;
}
textFont(font, 16);
fill(255, 255, 255);
text ("Nighttime Cloud Sensor Location: Orpington, England", 48, 40);
textFont(font, 14);
fill(200, 0, 0);
text ("Ground Sensor = " + sensor2 + "°C", 48, boxHeight+65);
fill(0, 0, 200);
text ("Sky Sensor = " + sensor1 + "°C", 220, boxHeight+65);
fill(0, 200, 0);
text ("Clear/Cloudy", 400, boxHeight+65);
textFont(font, 12);
text("Cloudy", 48, boxHeight+105);
text("Clear", 540, boxHeight+105);

strokeWeight(2);
// Draw the trace for the 2 external temp sensors
for (int sensorNum = 0; sensorNum < 2; sensorNum++)

{
int arrayPointer = counter+1;

beginShape();
for (int graphX = 180; graphX >=0; graphX--)
{
if (arrayPointer == 180)
{
arrayPointer = 0;
}

switch (sensorNum)
{
case 0:
stroke(0, 0, 255);
break;
case 1:
stroke(255, 0, 0, 190);
break;

}
float sensorPoint = (boxHeight/(diff/(lastTemp[sensorNum][arrayPointer]-MinVal)));

noFill();
curveVertex(588 - (graphX * 3), (boxHeight+50)-sensorPoint);

arrayPointer++;

}
endShape();
}

// Draw the trace for the clear/cloudy line

int arrayPointer = counter+1;

beginShape();
stroke (0, 200, 0, 150);
for (int graphX = 180; graphX >=0; graphX--)
{
if (arrayPointer == 180)
{
arrayPointer = 0;
}

float sensorPoint = (boxHeight*lastTemp[4][arrayPointer]);

noFill();
curveVertex(588 - (graphX * 3), (boxHeight-((lastTemp[4][arrayPointer]*(boxHeight)-48))));

arrayPointer++;

}
endShape();

stroke(0,0,0);
noFill();
strokeWeight(2);
rect(48,50,540,boxHeight);
}

void initialiseArray(float value)
{
for (int j=0; j <4; j++)
{
for (int i=0; i < 180; i++)
{
lastTemp[j][i] = value;
}
}
}

// Function for working out the max and min temperatures in the array
void findMinMax(float Temp)
{
if (Temp > MaxVal) {MaxVal = ceil(Temp);}
if (Temp < MinVal) {MinVal = floor(Temp);}
diff = truncate(MaxVal - MinVal);
}

float truncate(float x){
if ( x > 0 )
return float(floor(x * 100))/100;
else
return float(ceil(x * 100))/100;
}

void drawIndicator() {
float i = 255;
for (int j=48; j<=588; j++) {
stroke (int (i), int (i), int (i) );

line(j, boxHeight+80, j, boxHeight+90);
i=i - 0.45;
}

stroke (255,255,0);
fill(255,255,0);
int TempPos = int(48+(TempDiff*(540/10)));
print (TempDiff + " +);
triangle(TempPos, boxHeight+75, TempPos-2, boxHeight+72, TempPos+2, boxHeight+72);

}
Re: What is wrong with my code please?
Reply #2 - Oct 28th, 2008, 10:18pm
 
Without the appropriate hardware, it might be hard to test this code...
And I don't feel like analyzing this long code searching for errors.
If you indicated which kind of errors you get (syntax? runtime? wrong results?), it might help.
Re: What is wrong with my code please?
Reply #3 - Nov 1st, 2008, 12:09pm
 
It is designed to read serial data over the USB port from an Arduino connected to 3 temperature sensors.

The errors I get are :-

Code:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at antlr.ANTLRStringBuffer.append(ANTLRStringBuffer.java:36)
at antlr.CharScanner.append(CharScanner.java:64)
at antlr.CharScanner.consume(CharScanner.java:82)
at antlr.CharScanner.match(CharScanner.java:205)
at processing.app.preproc.PdeLexer.mSTRING_LITERAL(PdeLexer.java:1376)
at processing.app.preproc.PdeLexer.nextToken(PdeLexer.java:198)
at antlr.TokenStreamHiddenTokenFilter.consume(TokenStreamHiddenTokenFilter.java:38)
at antlr.TokenStreamCopyingHiddenTokenFilter.nextToken(TokenStreamCopyingHiddenTokenFilter.java:209)
at antlr.TokenBuffer.fill(TokenBuffer.java:69)
at antlr.TokenBuffer.LA(TokenBuffer.java:80)
at antlr.LLkParser.LA(LLkParser.java:52)
at processing.app.preproc.PdeRecognizer.unaryExpressionNotPlusMinus(PdeRecognizer.java:3726)
at processing.app.preproc.PdeRecognizer.unaryExpression(PdeRecognizer.java:3637)
at processing.app.preproc.PdeRecognizer.multiplicativeExpression(PdeRecognizer.java:3545)
at processing.app.preproc.PdeRecognizer.additiveExpression(PdeRecognizer.java:3502)
at processing.app.preproc.PdeRecognizer.shiftExpression(PdeRecognizer.java:3453)
at processing.app.preproc.PdeRecognizer.relationalExpression(PdeRecognizer.java:3382)
at processing.app.preproc.PdeRecognizer.equalityExpression(PdeRecognizer.java:3339)
at processing.app.preproc.PdeRecognizer.andExpression(PdeRecognizer.java:3310)
at processing.app.preproc.PdeRecognizer.exclusiveOrExpression(PdeRecognizer.java:3281)
at processing.app.preproc.PdeRecognizer.inclusiveOrExpression(PdeRecognizer.java:3252)
at processing.app.preproc.PdeRecognizer.logicalAndExpression(PdeRecognizer.java:3223)
at processing.app.preproc.PdeRecognizer.logicalOrExpression(PdeRecognizer.java:3194)
at processing.app.preproc.PdeRecognizer.conditionalExpression(PdeRecognizer.java:3163)
at processing.app.preproc.PdeRecognizer.assignmentExpression(PdeRecognizer.java:1250)
at processing.app.preproc.PdeRecognizer.expression(PdeRecognizer.java:854)
at processing.app.preproc.PdeRecognizer.expressionList(PdeRecognizer.java:3093)
at processing.app.preproc.PdeRecognizer.argList(PdeRecognizer.java:933)
at processing.app.preproc.PdeRecognizer.identPrimary(PdeRecognizer.java:1149)
at processing.app.preproc.PdeRecognizer.primaryExpression(PdeRecognizer.java:988)
at processing.app.preproc.PdeRecognizer.postfixExpression(PdeRecognizer.java:3762)
at processing.app.preproc.PdeRecognizer.unaryExpressionNotPlusMinus(PdeRecognizer.java:3727)
Re: What is wrong with my code please?
Reply #4 - Nov 3rd, 2008, 8:02am
 
Wow, you tripped the pre-preprocessor... Smiley

Anyway, I took your code and pasted in my editor. It shows unterminated strings with red background, so I spotted immediately the line

print (TempDiff + "   +);

near the bottom of your code. You should remove or finish this line to compile/run the code correctly.
Re: What is wrong with my code please?
Reply #5 - Nov 7th, 2008, 3:20pm
 
Thanks for that Smiley
Page Index Toggle Pages: 1