Go Down

Topic: Arduino, Processing and 2 HC-SR04 Ultrasonic Sensor problems. Please help.... (Read 5519 times) previous topic - next topic

PaulS

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().

PaulS

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

Why not do something meaningful, like:
Code: [Select]
print("d1: ");
println(d1);

PaulS

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.

PaulS

Quote
and I removed the global array is that ok?

Yes, that is fine.

Post the code that you have now.

PaulS

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

Have you forgotten about Tools + Auto Format already?

Code: [Select]
int d1=0;
int d2 = 0;

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

Code: [Select]
int pd1 = 0;
int pd2= 0;
int left = 0;
int right = 0;

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

Code: [Select]
print("currentState,");
println (currentState);
print("currentState,");
println(currentState);

Just in case it changed magically?

Code: [Select]
    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().


PaulS

Code: [Select]
  print("currentState,");
  println (currentState);
  print("currentState,");
  println(currentState);

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

Code: [Select]
    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?

PaulS

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.

PaulS

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.

PaulS

Quote
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.

Go Up