Pages: 1 2 [3]   Go Down
Author Topic: Arduino, Processing and 2 HC-SR04 Ultrasonic Sensor problems. Please help....  (Read 4758 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and I removed the global array is that ok?
Yes, that is fine.

Post the code that you have now.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you forgotten about Tools + Auto Format already?

Code:
int d1=0;
int d2 = 0;
Consistency is a good habit. You don't seem to have developed that yet.

Code:
int pd1 = 0;
int pd2= 0;
int left = 0;
int right = 0;
You don't use these. Find the delete key.

Code:
print("currentState,");
println (currentState);
print("currentState,");
println(currentState);
Just in case it changed magically?

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  print("currentState,");
  println (currentState);
  print("currentState,");
  println(currentState);
Why? currentState is not going to change between println statements.

Code:
    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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 2 [3]   Go Up
Jump to: