Go Down

Topic: controlling an image sequence in processing with adruino (Read 3779 times) previous topic - next topic

ettaler18

Ok,the code was totally messed up,so I structured it and worked on it.Now the ardruino code works as well as the processing code.
But no images are displayed and I can`t find a fault.Can you find it?
This is the actual processing code:
Code: [Select]

/*


*/

import processing.serial.*;

// Variables for putting image sequences on screen.
int imagewidth = 320;
int imageheight = 240;
int numPics = 510;

float highValue = 0;
float average;
int sequences = 0;

// Variables for serial connection.
char header = 'A'; // For controlling the carriage return and bla... bla... bla...
int NEWLINE = 10;
float value;
String buff = ""; // Buffing :P

Serial port;

PImage[] beer = new PImage[numPics];

void setup() {
  size(imagewidth, imageheight);
  imageMode(CENTER);

  // Initializing the image sequences.
  for (int i = 0; i < beer.length; i++) {
    println(i);
      beer[i] = loadImage("picture"+ i +".png");
     
    }
 

  // Establish connection to Serial port.
  println(Serial.list()); // Formality.
  port = new Serial(this, Serial.list()[0], 9600); // Establish Serial connection.
}

void draw() {


  // Checking the highest value.
  if (value > highValue) {
    highValue = value;
    average = highValue / 2;
  }
 
  if(value > average){
    sequences++;
   
    if(sequences > numPics - 1){
      sequences = 0;
    }
   
   
    // Debug.
    println("Increased");
  }
  else if(value < highValue){
    sequences--;
   
    if(sequences < 0){
      sequences = numPics - 1;
    }
   
    // Debug.
    println("Decreased");
  }
 


  // Debug.
  // println(i);

  // From connection to Serial ports.
  while (port.available () > 0) {
    serialEvent(port.read());

    // Debug.
     println(port.read());
  }
 
  delay(50);

  // Debug.
  // println(value);
}

void serialEvent(int serial) {
  try {
    if (serial != NEWLINE) {
      buff += char(serial);
    }
    else {
      char c = buff.charAt(0);
      buff = buff.substring(1);
      buff = buff.substring(0, buff.length() - 1);
      if (c == header) {
        value = Integer.parseInt(buff);
      }

      buff = "";
    }
  }
  catch(Exception e) {
    println("no valid data");
  }
}



PaulS

Code: [Select]
  // From connection to Serial ports.
  while (port.available () > 0) {
    serialEvent(port.read());

    // Debug.
     println(port.read());
  }

What?

The only thing wrong with the code that you had was that you were not doing anything with the serial data. Now, you are, but you are doing it all wrong. port.read() gets one byte.

Get rid of this stuff. Put serialEvent() back the way it was, and add:
currImageIndex = int(myString);
after the code to read the string.

You might want to print myString and currImageIndex in the serialEvent method, to confirm that all is working.
The art of getting good answers lies in asking good questions.

ettaler18

Okay,thank you for the advice.did I get it right that I need to take the last code and just have to change the serial event like this :
Code: [Select]


void serialEvent(Serial myPort)
{
  // Read the serial data
  String myString = myPort.readStringUntil('\n');
  currImageIndex = int(myString);
}
void draw()

{
  if (currImageIndex != prevImageIndex)
  {
   
  background(0);
  image(images[currImageIndex], 0, 0);
  }
}



now the first picture loads,but no further pictures.
Why?

ettaler18


AWOL


Go Up