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 › project due tomorrow!!online waiting for debug!
Page Index Toggle Pages: 1
project due tomorrow!!online waiting for debug! (Read 577 times)
project due tomorrow!!online waiting for debug!
Apr 15th, 2010, 6:37pm
 
Hey guys~~i have some problems on my program. I was trying to get the readings from the arduino and then using that number as a index to load image from my computer. but i keep getting the "ArrayIndexOutOfBoundsException: 111" report.....
my project is due tomorrow...really appreciate if you could help!!!
code:

import processing.serial.*;

Serial myPort;  
int val;  
int i;
PImage[] images = new PImage[i];

void setup()
{
 size(400, 400);
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
 
 images[111] = loadImage("111.jpg");
 . // i just omit the other 68 commands between these two
 .
 .//
 images[444] = loadImage("444.jpg");
}

void draw()
{
 if ( myPort.available() > 0) {  
   val = myPort.read();      
 }
 i = val;
 image(images[i], 0, 0);
}
Re: project due tomorrow!!online waiting for debug!
Reply #1 - Apr 15th, 2010, 7:03pm
 
part of my arduino code:
void loop() {
 pulseCount = 0;    
 while(digitalRead(pulse_in) == 0) {}
 while(digitalRead(pulse_in) == 1) {  

 pulseCount++; }
 val1 = (pulseCount * .0000053) * 2;
 val1 = 1/val1;
 rval1 = val1;
 if (rval1 <= 300)
 {
   inde1 = 1;
 }
 else if (rval1 > 300 && rval1 <= 1200)
 {
   inde1 = 2;
 }
 else if (rval1 > 1200 && rval1 <= 3000)
 {
   inde1 = 3;
 }
 else
 {
   inde1 = 4;
 }
 delay(8000);
 
 pulseCount = 0;
 while(digitalRead(pulse_in) == 0) {}
 while(digitalRead(pulse_in) == 1) {
 pulseCount++; } // loop while Hi
 val2 = (pulseCount * .0000053) * 2;
 val2 = 1/val2; // calculate frequency
 rval2 = val2;
 if (rval2 <= 300)
 {
   inde2 = 1;
 }
 else if (rval2 > 300 && rval2 <= 1200)
 {
   inde2 = 2;
 }
 else if (rval2 > 1200 && rval2 <= 3000)
 {
   inde2 = 3;
 }
 else
 {
   inde2 = 4;
 }
 delay(8000);
 
 pulseCount = 0;
 while(digitalRead(pulse_in) == 0) {}
 while(digitalRead(pulse_in) == 1) {
 pulseCount++; } // loop while Hi
 val3 = (pulseCount * .0000053) * 2;
 val3 = 1/val3;
 rval3 = val3;
 if (rval3 <= 300)
 {
   inde3 = 1;
 }
 else if (rval3 > 300 && rval3 <= 1200)
 {
   inde3 = 2;
 }
 else if (rval3 > 1200 && rval3 <= 3000)
 {
   inde3 = 3;
 }
 else
 {
   inde3 = 4;
 }
 delay(8000);

 inde8 = (inde1 * 100) + (inde2 * 10) + inde3;
 
 Serial.print(inde8);
 delay(8000);
 
}
Re: project due tomorrow!!online waiting for debug!
Reply #2 - Apr 16th, 2010, 12:18am
 
Quote:
Code:
int i;
PImage[] images = new PImage[i];

i is zero at this point, so you create an empty array. So images[111] = loadImage("111.jpg"); is making the error you see.
Your method can be argued (ie. there are better ways) but if you need to go fast and dirty, just hard-code the new PImage[MAX] with MAX being the higher number you use plus one.
Page Index Toggle Pages: 1