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

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.

and I removed the global array is that ok?

Yes, that is fine.

Post the code that you have now.

What's
with
the
code
all
jammed
to
the
right?

Have you forgotten about Tools + Auto Format already?

int d1=0;
int d2 = 0;

Consistency is a good habit. You don't seem to have developed that yet.

int pd1 = 0;
int pd2= 0;
int left = 0;
int right = 0;

You don't use these. Find the delete key.

print("currentState,"); 
println (currentState);
print("currentState,"); 
println(currentState);

Just in case it changed magically?

    pd1 = d1;
    pd2= d2;

Not used. Delete them.

Printing some different prefix in serialEvent() is necessary, so you know which prints happen in draw() and which happen in serialEvent().

  print("currentState,"); 
  println (currentState);
  print("currentState,"); 
  println(currentState);

Why? currentState is not going to change between println statements.

    print("currentState,"); 
    println (currentState);
    print("currentState,"); 
    println(currentState);

Ditto.

How do you distinguish between data printed in draw() and data printed in serialEvent()?

Where is your current output?

I'm sure that at some point in your journey to learn programming, you'll realize that when you change code, you need to post it. None of us are clairvoyant.

Before you do, though, explain why you are printing currentState twice in draw() and in serialEvent() (or stop doing it). Same for previousState.

I want to see your output look like:
se: d1: xxxx
se: d2: xxxx

d: d1: xxxx
d: d2: xxxx

That is done by changing the print() statements.

That way, we can tell whether the problem is that the data is not being collected correctly in serialEvent or if it is being trampled on somewhere else.

One other thing I'd try is to change the Arduino code to add another comma at the end. Perhaps the split function isn't getting the last token because it isn't delimited by a comma.

but instead of the comma going at the end it is going at the beginning of the line like this,

Of course. You need to understand that println() does something more than print(), and that there is a reason for using println() at the end.

Of course, what it really looks like is that the Arduino is sending 0 as the value for dist2 every time. That is why Processing keeps getting a 0 for d2.