Potentiomter/ RFID values going wrong when I run processing ?

Hi, I am looking to use RFID to load a panorama like image and then use the potentiometer to scroll from left and right? But I am having a few problems processing reading the values.

When I look at the arduino code in the arduino serial monitor the potentiometer values and the RFID tag number run fine and I get reading like;
958,
955,
951,
953,
957,5100C2EB6F17
954,
958,
957,
951,

But when I run my processing code I get readings like these;
944,
950,
942,
9439543,
942,959
9,46,
940,948949
947949
94599,
9
49441,
3,9509
942,9,
94941,
95,
9

What do I need to change, is it the processing code or the arduino ?

Thanks

Screen Shot 2013-05-26 at 16.40.00.png

Screen Shot 2013-05-26 at 16.32.58.png

Line 121 in your program, add "if ( Val>666 ) Serial.println('\09') ;

Oh, did I MindRead the wrong program?

This is the Arduino Code;

/* RFID ID12
*/

char val = 0; // variable to store the data from the serial port

void setup() {
Serial.begin(9600); // connect to the serial port
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
}

void loop () {
// read the serial port
int sensorValue = analogRead(A0);
Serial.print(sensorValue);
Serial.print(",");
if(Serial.available() > 15) {
  for(int byte=0;byte<=15;byte++)
  {
    val = Serial.read();
    if(byte<=12) Serial.print(val, BYTE);
  }
}
Serial.println();
delay(50);


}

And this is the Processing code;

import processing.serial.*;
import ddf.minim.*;//declare
AudioPlayer player;
Minim minim;

Serial portOne;    // the first serial port
Serial portTwo;    // the second serial port

boolean showImage;  // whether or not to show the image
// the list of names
String[] tags = {
  "5100C2F495F2","5100C2EB6F17","59003206E68B","59003219B7C5","590031BDCE1B","590031C48B4"};
  
  String tag="";
  
  float sensorFloat;

// the list of people
PImage thisImage;
PImage bg;

int maxPics =1;

int pictureNumber = 1;

void setup() {
  size(1000, 664);
  showImage = false;
  // list the serial ports
  println(Serial.list());
  // open the serial ports:
  portOne = new Serial(this, Serial.list()[16], 9600);
  // set both ports to buffer information until you get 0x03:
  //portOne.bufferUntil('\n');
   minim=new Minim(this);
   bg = loadImage("backimage.jpg");

 
 }
 

 void draw() {
 background(bg);
 if(tag!="")
  {
    getImage(tag,pictureNumber);
    image(thisImage, 0, 0);
  }
  // if there's an image to show, show it:
  if (showImage) {
  image(thisImage, 0, 0);
  }
}

void serialEvent(Serial thisPort) {
  // read the incoming serial data:

  String potString = thisPort.readStringUntil(',');
  String rfidString = thisPort.readStringUntil('\n');

  
  if(potString != null && potString.length()>2)
  {
    potString = trim(potString);
    sensorFloat = float(potString);
  }

  // if the string is not empty, do stuff with it:
  if (rfidString != null && rfidString.length()>2) {
    // if the string came from serial port one:
    if (thisPort == portOne) {
      print ("Data from port one: ");
    }
    // if the string came from serial port two:
    if (thisPort == portTwo) {
      print ("Data from port two: ");
    }
    // print the string:
    println(rfidString);
    
    println(rfidString.substring(1,13)+".jpg");
   // images[i] = loadImage(rfidString.substring(1,13)+ "/" + i + ".jpg" ); 
    
    println(rfidString.substring(1,13)+"/music.mp3");
    if(player!=null)
    {
      player.pause();
      player.close();
    }
    player=minim.loadFile(rfidString.substring(1,13)+"/music.mp3");//locate file
    println(player);
     player.play();//when opened play this

    // the tag ID is only bytes 1 through 13. Get it:
    tag = rfidString.substring(1, 13);
  }
}




// Get random image from the folder filelist array
void getImage(String tag,int picture) {
  String filenames[];

  try{
    // open the directory:
    File directory = new File(sketchPath("data")+"/"+tag);
    
    // list the files in the directory:
    filenames = directory.list();

    int picCounter=0;
    for(int thisFile = 0; thisFile<filenames.length; thisFile++){
      // if the file is a .jpg:
      if (filenames[thisFile].endsWith(".jpg")) {
        picCounter++;
        if(picCounter==picture)
        {
        // get the filename without the .jpg extension:
        String thisName = filenames[thisFile].substring(0, filenames[thisFile].length() -4);
        //if the filename matches the name of the person:
          // load the image from the file:
          thisImage = loadImage("data/"+tag+"/"+filenames[thisFile]);
          // show it:
          showImage = true;
        }
       }
    }
  }
  catch(Exception f){
    //  if there was a problem reading the directory:
    println("Image import error - missing thisFile or folder");
  }
}

Thanks

I don't know how Processing handles serial port input but that Arduino sketch will be dumping messages out at the maximum speed of the serial port and depending how much time it takes the Processing application to do that other stuff in serialEvent() it could easily be receiving serial input faster than it is handling it. In that case you'd expect Processing to have a finite receive buffer and to discard any input that can't be buffered. The results would be arbitrary gaps in the input, which could explain what you're seeing.

Start by doing nothing at all except printing the received value. This should consistently show that you're receiving the same data that is being sent. When printing the received values don't just print the raw string, print some text to tell you what is being printed and to show the start and end of the value printed. I assume the output from your Processing application is going to some local device with ample speed to handle output as fast as your Processing sketch can generate it.

Processing has a couple of things that make reading serial data easier. There is a bufferUntil() method that tells Processing to buffer data until a specific character, like the carriage return or line feed, arrives.

Then, there is the serialEvent() method that you can define, that gets called only when the bufferUntil character has arrived.

You should use both of these tools to solve your issue.

The reason why the numbers were going like that, was because I had both Processing and Arduino at the same time. But I have copied the code that I need for the potentiometer, but its still not responding.

The reason why the numbers were going like that, was because I had both Processing and Arduino at the same time.

What does this mean? The Arduino and Processing have to be running at the same time, if one is to communicate with the other.

But I have copied the code that I need for the potentiometer, but its still not responding.

Copied what code from where to where? "it" is a pronoun that has no referent. We have no idea what is not responding.

There were several suggestions made that would solve some, or all, of your problems. It is not clear whether you have adopted any of them.

I had both serial monitor in arduino and running the processing sketch at the same time. The Arduino sketch above reads the Potentiometer Values and then I have this code below for the processing sketch; The sketch loads an image and then I want to use the potentiometer to scroll across the x pos, like a panoramic image.

import processing.serial.*;
import ddf.minim.*;//declare
AudioPlayer player;
Minim minim;
int x, moveSpeed = 5;

Serial portOne;    // the first serial port
Serial portTwo;    // the second serial port

boolean showImage;  // whether or not to show the image
// the list of names
String[] tags = {
  "5100C2F495F2","5100C2EB6F17","59003206E68B","59003219B7C5","590031BDCE1B","590031C48B4"};
  
  String tag="";
  
  float sensorFloat;

// the list of people
PImage thisImage;
PImage bg;

int maxPics =1;

int pictureNumber = 1;

void setup() {
  size(1000, 664);
  showImage = false;
  // list the serial ports
  println(Serial.list());
  // open the serial ports:
  portOne = new Serial(this, Serial.list()[16], 9600);
  // set both ports to buffer information until you get 0x03:
  //portOne.bufferUntil('\n');
   minim=new Minim(this);
   bg = loadImage("backimage.jpg");

 
 }
 

 void draw() {
 background(bg);
 if(tag!="")
  {
    getImage(tag,pictureNumber);
    image(thisImage, 0, 0);
  }
  // if there's an image to show, show it:
  if (showImage) {
  image(thisImage, 0, 0);
   float potentiometer = sensorFloat;
   float movement = map(potentiometer, 0, 1023, -moveSpeed, moveSpeed); // map from range (0, 1023) to (-moveSpeed, moveSpeed)
   x += movement; // add movement to location
   x = constrain(x, -(thisImage.width-width), 0); // constrain image by 0 (at the beginning) and -600 (aka a remaining visible image width of 1000 equal to the canvas width).
   image(thisImage, x, 0); // draw the image
   fill(0);
   frame.setTitle(x + ""); // see the x in the top-left
  }
}

void serialEvent(Serial thisPort) {
  // read the incoming serial data:

  String potString = thisPort.readStringUntil(',');
  String rfidString = thisPort.readStringUntil('\n');

  
  if(potString != null && potString.length()>2)
  {
    potString = trim(potString);
    sensorFloat = float(potString);
  }

  // if the string is not empty, do stuff with it:
  if (rfidString != null && rfidString.length()>2) {
    // if the string came from serial port one:
    if (thisPort == portOne) {
      print ("Data from port one: ");
    }
    // if the string came from serial port two:
    if (thisPort == portTwo) {
      print ("Data from port two: ");
    }
    // print the string:
    println(rfidString);
    
    println(rfidString.substring(1,13)+".jpg");
   // images[i] = loadImage(rfidString.substring(1,13)+ "/" + i + ".jpg" ); 
    
    println(rfidString.substring(1,13)+"/music.mp3");
    if(player!=null)
    {
      player.pause();
      player.close();
    }
    player=minim.loadFile(rfidString.substring(1,13)+"/music.mp3");//locate file
    println(player);
     player.play();//when opened play this

    // the tag ID is only bytes 1 through 13. Get it:
    tag = rfidString.substring(1, 13);
  }
}




// Get random image from the folder filelist array
void getImage(String tag,int picture) {
  String filenames[];

  try{
    // open the directory:
    File directory = new File(sketchPath("data")+"/"+tag);
    
    // list the files in the directory:
    filenames = directory.list();

    int picCounter=0;
    for(int thisFile = 0; thisFile<filenames.length; thisFile++){
      // if the file is a .jpg:
      if (filenames[thisFile].endsWith(".jpg")) {
        picCounter++;
        if(picCounter==picture)
        {
        // get the filename without the .jpg extension:
        String thisName = filenames[thisFile].substring(0, filenames[thisFile].length() -4);
        //if the filename matches the name of the person:
          // load the image from the file:
          thisImage = loadImage("data/"+tag+"/"+filenames[thisFile]);
          // show it:
          showImage = true;
        }
       }
    }
  }
  catch(Exception f){
    //  if there was a problem reading the directory:
    println("Image import error - missing thisFile or folder");
  }
}

I had both serial monitor in arduino and running the processing sketch at the same time.

On what OS? Not Windows.

Why? Which application do you want the Arduino talking to?

  portOne = new Serial(this, Serial.list()[16], 9600);

You have 17 or more active COM ports? What do you have generating all of them? Some bluetooth crap?

Where does portTwo get assigned a value? Why are you comparing the serial port that data came in on to an uninitialized variable?

Where are your print() statements? Where is the output that they produce?

Most importantly, what is the problem we are trying to solve?

On OS yeah, Arduino talking to processing.

Yeah its a lot of bluetooth crap, my main problem that I am trying to solve is getting the potentiometer to scroll left and right through an image. This is the only part that I can't get working, all the RFID tags are working fine, they load the image and the music. It's just when the image is loaded, I can't get the potentiometer to scroll through the image?

I have tried different things, but it still wont work?

I have tried different things, but it still wont work?

First, you need to get the potentiometer value into Processing. Have you got that working?

my main problem that I am trying to solve is getting the potentiometer to scroll left and right through an image.

Second, you need to explain what this means. As far as I know, Processing simply displays an image, at a fixed size and position. What is it you are trying to do to this image?

If manipulating the image in some way IS possible, the Processing forum seems a more likely place to learn how.