errore programma ambient light

Salve ragazzi ho trovato questo interessantissimo progetto che mi piacerebbe adottare (http://blog.oscarliang.net/diy-tv-ambilight-arduino-processing-ozilight-1/).

Ho riscontrato un errore nel codice di processing:

"ArrayIndexOutOfBoundsException: 77"

e mi evidenzia la riga:

"serialData[data_index++] = (byte)ledColor[i][0];"

ho provato a cercare soluzioni ma non ci sono riuscito =( =( =(
potete aiutarmi voi per favore?
grazie

Codice processing in questione:

import java.awt.*;
import java.awt.image.*;
import processing.serial.*;

/*
// using 12 RGB LEDs
static final int led_num_x = 4;
static final int led_num_y = 4;
static final int leds[][] = new int[][] {
  {1,3}, {0,3}, // Bottom edge, left half
  {0,2}, {0,1}, // Left edge
  {0,0}, {1,0}, {2,0}, {3,0}, // Top edge
  {3,1}, {3,2}, // Right edge
  {3,3}, {2,3}, // Bottom edge, right half
};

*/

// using 25 RGB LEDs
static final int led_num_x = 10;
static final int led_num_y = 6;
static final int leds[][] = new int[][] {
  {2,5}, {1,5}, {0,5}, // Bottom edge, left half
  {0,4}, {0,3}, {0,2}, {0,1}, // Left edge
  {0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0}, {7,0}, {8,0}, {9,0}, // Top edge
  {9,1}, {9,2}, {9,3}, {9,4}, // Right edge
  {9,5}, {8,5}, {7,5}, {6,5}  // Bottom edge, right half

};

static final short fade = 70;

static final int minBrightness = 120;

// Preview windows
int window_width;
int window_height;
int preview_pixel_width;
int preview_pixel_height;

int[][] pixelOffset = new int[leds.length][256];

// RGB values for each LED
short[][]  ledColor    = new short[leds.length][3],
      prevColor   = new short[leds.length][3];  

byte[][]  gamma       = new byte[256][3];
byte[]    serialData  = new byte[ leds.length * 3 + 2];
int data_index = 0;

//creates object from java library that lets us take screenshots
Robot bot;

// bounds area for screen capture         
Rectangle dispBounds;

// Monitor Screen information    
GraphicsEnvironment     ge;
GraphicsConfiguration[] gc;
GraphicsDevice[]        gd;

Serial           port;

void setup(){

  int[] x = new int[16];
  int[] y = new int[16];

  // ge - Grasphics Environment
  ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  // gd - Grasphics Device
  gd = ge.getScreenDevices();
  DisplayMode mode = gd[0].getDisplayMode();
  dispBounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());

  // Preview windows
  window_width      = mode.getWidth()/5;
  window_height      = mode.getHeight()/5;
  preview_pixel_width     = window_width/led_num_x;
  preview_pixel_height   = window_height/led_num_y;

  // Preview window size
  size(window_width, window_height);

  //standard Robot class error check
  try   {
    bot = new Robot(gd[0]);
  }
  catch (AWTException e)  {
    println("Robot class not supported by your system!");
    exit();
  }

  float range, step, start;

  for(int i=0; i<leds.length; i++) { // For each LED...

    // Precompute columns, rows of each sampled point for this LED

    // --- for columns -----
    range = (float)dispBounds.width / led_num_x;
    // we only want 256 samples, and 16*16 = 256
    step  = range / 16.0; 
    start = range * (float)leds[i][0] + step * 0.5;

    for(int col=0; col<16; col++) {
      x[col] = (int)(start + step * (float)col);
    }

    // ----- for rows -----
    range = (float)dispBounds.height / led_num_y;
    step  = range / 16.0;
    start = range * (float)leds[i][1] + step * 0.5;

    for(int row=0; row<16; row++) {
      y[row] = (int)(start + step * (float)row);
    }

    // ---- Store sample locations -----

    // Get offset to each pixel within full screen capture
    for(int row=0; row<16; row++) {
      for(int col=0; col<16; col++) {
        pixelOffset[i][row * 16 + col] = y[row] * dispBounds.width + x[col];
      }
    }

  }

  // Open serial port. this assumes the Arduino is the
  // first/only serial device on the system.  If that's not the case,
  // change "Serial.list()[0]" to the name of the port to be used:
  // you can comment it out if you only want to test it without the Arduino
 //port = new Serial(this, Serial.list()[0], 115200);

  // A special header expected by the Arduino, to identify the beginning of a new bunch data.  
  port = new Serial(this, "COM7", 115200);
  serialData[0] = 'o';
  serialData[1] = 'z';

}

void draw(){

  //get screenshot into object "screenshot" of class BufferedImage
  BufferedImage screenshot = bot.createScreenCapture(dispBounds);

  // Pass all the ARGB values of every pixel into an array
  int[] screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();

  data_index = 2; // 0, 1 are predefined header

  for(int i=0; i<leds.length; i++) {  // For each LED...

    int r = 0;
    int g = 0;
    int b = 0;

    for(int o=0; o<256; o++) {       //ARGB variable with 32 int bytes where       int pixel = screenData[ pixelOffset[i][o] ];       r += pixel & 0x00ff0000;       g += pixel & 0x0000ff00;       b += pixel & 0x000000ff;     }     // Blend new pixel value with the value from the prior frame     ledColor[i][0]  = (short)(((( r >> 24) & 0xff) * (255 - fade) + prevColor[i][0] * fade) >> 8);
    ledColor[i][1]  = (short)(((( g >> 16) & 0xff) * (255 - fade) + prevColor[i][1] * fade) >> 8);
    ledColor[i][2]  = (short)(((( b >>  8) & 0xff) * (255 - fade) + prevColor[i][2] * fade) >> 8);

    serialData[data_index++] = (byte)ledColor[i][0];
    serialData[data_index++] = (byte)ledColor[i][1];
    serialData[data_index++] = (byte)ledColor[i][2];

    float preview_pixel_left  = (float)dispBounds.width  /5 / led_num_x * leds[i][0] ;
    float preview_pixel_top    = (float)dispBounds.height /5 / led_num_y * leds[i][1] ;

    color rgb = color(ledColor[i][0], ledColor[i][1], ledColor[i][2]);
    fill(rgb);  
    rect(preview_pixel_left, preview_pixel_top, preview_pixel_width, preview_pixel_height);

  }

  if(port != null) {

    // wait for Arduino to send data
    for(;;){

      if(port.available() > 0){
        int inByte = port.read();
        if (inByte == 'y')
          break;
      }

    }
    port.write(serialData); // Issue data to Arduino

  }

  // Benchmark, how are we doing?
  println(frameRate);
  arraycopy(ledColor, 0, prevColor, 0, ledColor.length);

}
}

Ti dice che sei andato fuori dagli elementi dell'array.
Quindi o i di ledcolor oppure (molto probabile) data_index per l'array serialData

grazie per la risposta! come posso risolvere? non ne capisco molto di processing

Siete OT, qui si parla di un problema su un programma di Processing.

Ma senza il quale il mio Arduino non lavora :frowning: per favore aiutami a risolvere =( =(

Non potete proprio darmi una soluzione? :frowning:

ragazzi per favore... solo questa volta :frowning:

@imagider, mica non rispondiamo perchè parli di Processing.
E' che il tuo programma è complesso. Mica facile trovare l'errore. :smiley:

Suggerimento: bisogna capire a quale indice si riferisce quell'errore, perciò modifica questo:

serialData[data_index++] = (byte)ledColor[i][0];

e forza il ledcolor:

serialData[data_index++] = (byte)ledColor[0][0];

Non fà la cosa giusta ma cosi di indice ne hai uno solo.
Se ti da errore allora sappiamo che è data_index che va oltre ultimo elemento di serialData[], altrimenti è i a dare problemi.

Appurato che il programma non è tuo, mi par di capire che lo hai preso dal 1° link giusto?, e prendendo per buono il fatto che di modifiche non ne hai fatte, scrivi al suo autore facendogli presente che è errato, no? :wink:

Fatta una verifica (ma non totale, perchè in ufficio non ho arduino ne porte seriali), comunque:
Viene dichiarato questo:

byte[]    serialData  = new byte[ leds.length * 3 + 2];

Se fai println(leds.length); dentro la setup(), dove vuoi, anche per prima cosa a me risponde 25.
Ora 25*3+2=>75+2=> 77 serialData ha 77 elementi.

Il codice che da errore è qui:

data_index = 2; // 0, 1 are predefined header
  for (int i=0; i<leds.length; i++)   // For each LED...
  { int r = 0; int g = 0; int b = 0;
    for (int o=0; o<256; o++)
    { ledColor[i][1]  = (short)(((( g >> 16) & 0xff) * (255 - fade) + prevColor[i][1] * fade) >> 8);
      ledColor[i][2]  = (short)(((( b >>  8) & 0xff) * (255 - fade) + prevColor[i][2] * fade) >> 8);
      serialData[data_index++] = (byte)ledColor[i][0];
      serialData[data_index++] = (byte)ledColor[i][1];
      serialData[data_index++] = (byte)ledColor[i][2];
...

data_Index parte da 2 poi viene incrementata ben 3 volte dentro 2 cicli for nidificati. Uno da 0-24 e l'altro da 0-255
Essendo nidificati data_index deve poter arrivare a 242553=>18360 di elementi ne hai 77.
Sicuro di non aver toccato nulla ?

E di modifiche ne hai fatte eccome!!! Ca...volo nel codice originale non c'e' nessuna gestione della seriale ne di serialData[]
Quella è roba tua.

l'unica modifica che ho fatto è stato aggiungere
port = new Serial(this, "COM7", 115200); (anche senza questo mi dava l'errore)
e una parentesi graffa alla fine.
scusate se ho insistito grazie a tutti per le risposte :slight_smile:

al creatore ho già scritto nei commenti del suo progetto, potete trovare qui la sua risp "http://blog.oscarliang.net/arduino-ambilight-tv-processing-ozilight-2/"

Ah, okay, il codice è quello del secondo progetto, non della prima pagina che hai linkato.
http://blog.oscarliang.net/arduino-ambilight-tv-processing-ozilight-2/

Si, sopra ho linkanto il progetto dalla prima pagina, quello mi funziona.
Il programma che mi da problemi sta nella seconda pagina che ho linkato nel post precedente. Li si può trovare sia quello di processing che quello di arduino

avete trovato qualche soluzione?

ok risolto :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:
posto il codice estatto

import java.awt.*;
import java.awt.image.*;
import processing.serial.*;

/*
// using 12 RGB LEDs
static final int led_num_x = 4;
static final int led_num_y = 4;
static final int leds[][] = new int[][] {
  {1,3}, {0,3}, // Bottom edge, left half
  {0,2}, {0,1}, // Left edge
  {0,0}, {1,0}, {2,0}, {3,0}, // Top edge
  {3,1}, {3,2}, // Right edge
  {3,3}, {2,3}, // Bottom edge, right half
};

*/

// using 25 RGB LEDs
static final int led_num_x = 10;
static final int led_num_y = 6;
static final int leds[][] = new int[][] {
  {2,5}, {1,5}, {0,5}, // Bottom edge, left half
  {0,4}, {0,3}, {0,2}, {0,1}, // Left edge
  {0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}, {6,0}, {7,0}, {8,0}, {9,0}, // Top edge
  {9,1}, {9,2}, {9,3}, {9,4}, // Right edge
  {9,5}, {8,5}, {7,5}, {6,5}  // Bottom edge, right half

};

static final short fade = 70;

static final int minBrightness = 120;

// Preview windows
int window_width;
int window_height;
int preview_pixel_width;
int preview_pixel_height;

int[][] pixelOffset = new int[leds.length][256];

// RGB values for each LED
short[][]  ledColor    = new short[leds.length][3],
      prevColor   = new short[leds.length][3];  

byte[][]  gamma       = new byte[256][3];
byte[]    serialData  = new byte[ leds.length * 3 + 2];
int data_index = 0;

//creates object from java library that lets us take screenshots
Robot bot;

// bounds area for screen capture         
Rectangle dispBounds;

// Monitor Screen information    
GraphicsEnvironment     ge;
GraphicsConfiguration[] gc;
GraphicsDevice[]        gd;

Serial           port;

void setup(){

  int[] x = new int[16];
  int[] y = new int[16];

  // ge - Grasphics Environment
  ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  // gd - Grasphics Device
  gd = ge.getScreenDevices();
  DisplayMode mode = gd[0].getDisplayMode();
  dispBounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());

  // Preview windows
  window_width      = mode.getWidth()/5;
  window_height      = mode.getHeight()/5;
  preview_pixel_width     = window_width/led_num_x;
  preview_pixel_height   = window_height/led_num_y;

  // Preview window size
  size(window_width, window_height);

  //standard Robot class error check
  try   {
    bot = new Robot(gd[0]);
  }
  catch (AWTException e)  {
    println("Robot class not supported by your system!");
    exit();
  }

  float range, step, start;

  for(int i=0; i<leds.length; i++) { // For each LED...

    // Precompute columns, rows of each sampled point for this LED

    // --- for columns -----
    range = (float)dispBounds.width / led_num_x;
    // we only want 256 samples, and 16*16 = 256
    step  = range / 16.0; 
    start = range * (float)leds[i][0] + step * 0.5;

    for(int col=0; col<16; col++) {
      x[col] = (int)(start + step * (float)col);
    }

    // ----- for rows -----
    range = (float)dispBounds.height / led_num_y;
    step  = range / 16.0;
    start = range * (float)leds[i][1] + step * 0.5;

    for(int row=0; row<16; row++) {
      y[row] = (int)(start + step * (float)row);
    }

    // ---- Store sample locations -----

    // Get offset to each pixel within full screen capture
    for(int row=0; row<16; row++) {
      for(int col=0; col<16; col++) {
        pixelOffset[i][row * 16 + col] = y[row] * dispBounds.width + x[col];
      }
    }

  }

  // Open serial port. this assumes the Arduino is the
  // first/only serial device on the system.  If that's not the case,
  // change "Serial.list()[0]" to the name of the port to be used:
  // you can comment it out if you only want to test it without the Arduino

 //port = new Serial(this, Serial.list()[0], 115200);

  // A special header expected by the Arduino, to identify the beginning of a new bunch data.  
  serialData[0] = 'o';
  serialData[1] = 'z';

}

void draw(){

  //get screenshot into object "screenshot" of class BufferedImage
  BufferedImage screenshot = bot.createScreenCapture(dispBounds);

  // Pass all the ARGB values of every pixel into an array
  int[] screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();

  data_index = 2; // 0, 1 are predefined header

  for(int i=0; i<leds.length; i++) {  // For each LED...

    int r = 0;
    int g = 0;
    int b = 0;

    for(int o=0; o<256; o++)    
    {       //ARGB variable with 32 int bytes where               
        int pixel = screenData[ pixelOffset[i][o] ];            
        r += pixel & 0x00ff0000;            
        g += pixel & 0x0000ff00;            
        b += pixel & 0x000000ff;        
    }           
    // Blend new pixel value with the value from the prior frame   
  
    ledColor[i][0]  = (short)(((( r >> 24) & 0xff) * (255 - fade) + prevColor[i][0] * fade) >> 8);
    ledColor[i][1]  = (short)(((( g >> 16) & 0xff) * (255 - fade) + prevColor[i][1] * fade) >> 8);
    ledColor[i][2]  = (short)(((( b >>  8) & 0xff) * (255 - fade) + prevColor[i][2] * fade) >> 8);

    serialData[data_index++] = (byte)ledColor[i][0];
    serialData[data_index++] = (byte)ledColor[i][1];
    serialData[data_index++] = (byte)ledColor[i][2];

    float preview_pixel_left  = (float)dispBounds.width  /5 / led_num_x * leds[i][0] ;
    float preview_pixel_top    = (float)dispBounds.height /5 / led_num_y * leds[i][1] ;

    color rgb = color(ledColor[i][0], ledColor[i][1], ledColor[i][2]);
    fill(rgb);  
    rect(preview_pixel_left, preview_pixel_top, preview_pixel_width, preview_pixel_height);

  }

  if(port != null) {

    // wait for Arduino to send data
    for(;;){

      if(port.available() > 0){
        int inByte = port.read();
        if (inByte == 'y')
          break;
      }

    }
    port.write(serialData); // Issue data to Arduino

  }

  // Benchmark, how are we doing?
  println(frameRate);
  arraycopy(ledColor, 0, prevColor, 0, ledColor.length);

}

grazie a tutti ragazzi

uffa... finalmente è arrivata la strisci... e indovinate un po! un nuovo problema oleee :smiley: :smiley:
carico il programma su arduino avvio processing e la striscia non da segni di vita gli specifico la seriale su processing

port = new Serial(this, "COM7", 115200);

e il programma non mi da più la schermata con i quadrati che cambiano coloro ma una bella schermata tutta grigia e ovviamente la striscia non fa nulla.
La striscia funziona l'ho testata con l'esembio della neopixel.

per favore riuscite a indicarmi il motivo?

Ho letto, ma io personalmente, non saprei aiutarti, sorry. Mi sà che l'argomento è un pò complesso o poco diffuso tra gli utenti del forum.