ArrayIndexOutofBoundsException??

edited December 2016 in Library Questions

I cannot see where this error comes from in this sketch: Any help would be appreciated . . .

import processing.io.*; I2C i2c;

void setup() { size(420, 420); i2c = new I2C(I2C.list()[0]); background(255); }

float celsius;

void draw() { for (int pixel = 0; pixel < 64; pixel++) {

i2c.beginTransmission(0x68);
i2c.write(0x80);
i2c.endTransmission();

i2c.beginTransmission(0x68);
i2c.write(0x68);
byte[] in = i2c.read(2);
int lowerLevel = in[0];
int upperLevel = in[1];
i2c.endTransmission();

int temperature = ((upperLevel << 8) | lowerLevel);
if (temperature > 2047) {
  temperature = temperature - 4096;
}

celsius = temperature * 0.25;
print(celsius);
print(" ");

if ((pixel + 1) % 8 == 0) {
  print("\r\n");
}

} }

Tagged:

Answers

  • Edit post, highlight code, press Ctrl-o to format code.

    The processing editor will highlight the line with the error when you run it, save us having to guess.

    This line looks a bit odd (but might be OK)

    i2c = new I2C(I2C.list()[0]); 
    
  • Thanks for looking at this . . . the line you mention seems to be the problem.

  • Answer ✓

    I2C.list() returns an array with the names of your connected I2C-devices, if this array is empty you get this error when you try to access an element of this array.

    Seems like there are no I2C-devices connected on your system. Check if your hardware setup is ok.

  • Thank you for that info. I would not have thought to look at hardware.

Sign In or Register to comment.