Arduino, Processing and 2 HC-SR04 Ultrasonic Sensor problems. Please help....

I have tried different methods of doing it

the experience works better if you show us your attempts.

if (right(d2>= dist)){

"right" is a non-void function taking a boolean parameter?

Given the missing semicolons, I'm not surprised

(Generally we say something didn't work if it compiled, but didn't perform as expected.
If it didn't even compile we say it didn't compile)

Ok, thanks for letting us know.

How about the missing braces?

Have you read this?

So, it's now compiling - good.
OK, as long as you're happy to keep plugging away at it, it's all cool
If you need any help, be sure to post code.

if (d1>=dist){
   currentState=currentState +1;

else
    if (d2>=dist){
      currentState=currentState -1;
    }

I'm guessing that the error message (which you didn't post) is something like "else without previous if", which is why I asked about the missing braces.

Please read my earlier definition of working vs. compiling.

Something can only be said to work/not work if it actually runs.
It can only run if it compiles, which because of the lack of a closing brace } before the else, your code cannot.

It kind of works now

So, you're saying this

if (d1>=dist){
   currentState=currentState +1;

else

compiles?

Keyhole debugging doesn't work for me.
Is that else clause really empty?
If it is, it doesn't need to be there at all.

I am keep getting error messages.

Let's play "Guess the error message"

Let's play "Guess the error message"

I's really not that hard:

pd1 = nums;
pd2= nums;
int [] nums = int(split(myString, ','));

You can't assign an array to an int. You can't assign an array before you allocate it to anything.

Hi there I have been trying to create another 2 variables that stores the previous d1 and d2 values in pd1 and pd2

Then, wouldn't:

pd1 = d1;
pd2 = d2;

make a hell of a lot more sense?

You want us to rewrite Java to make that work? Forget it. pd1 will NEVER be NULL. Don't bother testing for an impossible situation.

Hi Paul, Please can you help me to get the other sensor "d2) to work.In order to go back to the previous image.

No. I've asked you to put each { on a new line, to use Tools + Auto Format, and to use only a reasonable amount of white space. You have ignored all those requests.

I've suggested, too, that you print() stuff, to understand what is going on.

Ok now I did the auto format.

But not the curly braces or the print statements.

Or the comparison between pd1 and d1 or between pd2 and d2.

We need to know what the problem is.

When you say put each { on a new line do you mean put all of them at the bottom sketch or with in each method???

if(whatever) { // Curly brace in wrong place

if(whatever)
{ // Curly brace on a new line, where it belongs.
int d1, d2 = 0;

Be careful where you write things like that.
At global scope, it's OK because both variables would be set to zero anyway, unless you specified a different value.
However, with local scope, only d2 would be initialsed, and d1 would pick up whatever junk was left on the stack.

  if  (currentState<displayImage.size()) 
  {
    image(displayImage.get(currentState), 0, 0);
  }
  if (d1>=dist)
  {
    currentState=currentState +1;
  }
  if (d2>=dist)
  {
    currentState=currentState -1;
  }

Now that the code is arranged in a nice readable fashion, lets talk about what it actually does.

Display an image, and then check to see if that was the right image. I would think that you'd want to do the checking first, and then display the correct image.

I also thought that you were concerned about the current value (d1) vs. the previous value (pd1) (or d2 vs. pd2) somehow.

For now, add print statements to draw() to print d1, d2, dist, and currentState. You'll need to print currentState more than once.

In the if(d1 >= dist) block, print that currentState is being incremented. In the if(d2 >= dist) block, print that currentState is being decremented.

I think that what you'll find is that currentState is being incremented AND decremented in each pass through draw().

Just like the Arduino has print() and println(), so does Processing. What do those numbers mean?

Why not do something meaningful, like:

print("d1: ");
println(d1);

The next step, then, is to add more print() and println() statements to serialEvent(). Is the data being converted to an array of ints correctly? Is the correct data being stored in d1 and d2?

You have (still) a global array called nums and a local array called nums. Bad idea. Get rid of one of them.