Pages: [1]   Go Down
Author Topic: Problem using 2 Ultrasonic sensors together  (Read 946 times)
0 Members and 1 Guest are viewing this topic.
Cardiff, Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im working on a project where the user moves their hand up and down above 2 ultrasonic sensors and a line of LEDs follow your hand up and down. basically, the problem im having means that if you only use one ultrasonic sensor (and get a value back in cm), it subtracts this value from the other sensors value. e.g. you have your hand over the left sensor, but not the right one. as you increase the value of the left sensor, you decrease the value of the right one :S
I've looked at everything and have no idea why this is happening!

My code is too long to post in these forums so i've put it on pastebin: http://pastebin.com/KhDTa7rZ. I'm using an arduino mega. pins 24-53 are the leds. the left and right ultrasonic sensors are attached to 8 & 9, and 10 & 11 respectively.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My code is too long to post in these forums
Can you see the minimal difference between this
Code:
void right14() {
  digitalWrite (R1, HIGH);
  digitalWrite (R2, HIGH);
  digitalWrite (R3, HIGH);
  digitalWrite (R4, HIGH);
  digitalWrite (R5, HIGH);
  digitalWrite (R6, HIGH);
  digitalWrite (R7, HIGH);
  digitalWrite (R8, HIGH);
  digitalWrite (R9, HIGH);
  digitalWrite (R10, HIGH);
  digitalWrite (R11, HIGH);
  digitalWrite (R12, HIGH);
  digitalWrite (R13, HIGH);
  digitalWrite (R14, HIGH);
  digitalWrite (R15, LOW);
}
and this
Code:
void right15() {
  digitalWrite (R1, HIGH);
  digitalWrite (R2, HIGH);
  digitalWrite (R3, HIGH);
  digitalWrite (R4, HIGH);
  digitalWrite (R5, HIGH);
  digitalWrite (R6, HIGH);
  digitalWrite (R7, HIGH);
  digitalWrite (R8, HIGH);
  digitalWrite (R9, HIGH);
  digitalWrite (R10, HIGH);
  digitalWrite (R11, HIGH);
  digitalWrite (R12, HIGH);
  digitalWrite (R13, HIGH);
  digitalWrite (R14, HIGH);
  digitalWrite (R15, HIGH);}
?

If you spent a little time refactoring your code, it might make spotting your problem a lot easier.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

The snippet that AWOL posted just screams for arrays. Perhaps, then, your code wouldn't be so long that it couldn't be posted.

If you strip away all the stuff NOT involved with reading the ultrasonic sensors, what do your serial print statements tell you is happening?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The snippet that AWOL posted just screams for arrays
I just took another look - the code is around 600 (+/- 100) lines too long.
Remember, computers, even teeny-weeny ones like Arduinos, are really good at repetition.

But yes, the serial prints will tell all.
My guess is that the second sensor is picking up a return from the first.
A good long delay of, say 50 ms, between the two will tell you.
« Last Edit: May 14, 2012, 06:56:38 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did not look at your code but the first think I thought of was you may have been trying to read both sensor at the same time or close enough together at the signals were interfering.
Just a thought.
Logged

Cardiff, Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've just stuck a delay in and it has solved the problem perfectly! smiley its so obvious i'm kicking myself! smiley-razz cheers guys! smiley

so...umm..how do I make this smaller?
Code:
void right14() {
  digitalWrite (R1, HIGH);
  digitalWrite (R2, HIGH);
  digitalWrite (R3, HIGH);
  digitalWrite (R4, HIGH);
  digitalWrite (R5, HIGH);
  digitalWrite (R6, HIGH);
  digitalWrite (R7, HIGH);
  digitalWrite (R8, HIGH);
  digitalWrite (R9, HIGH);
  digitalWrite (R10, HIGH);
  digitalWrite (R11, HIGH);
  digitalWrite (R12, HIGH);
  digitalWrite (R13, HIGH);
  digitalWrite (R14, HIGH);
  digitalWrite (R15, LOW);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Stick all the pin numbers (and ranges) into an array, and use a for loop (or two).
I think you'll be able to get your entire sketch under 100 lines.

Quote
its so obvious i'm kicking myself!
Don't be hard on yourself - it isn't all that obvious - we expect the results returned by a sensor to relate to only that sensor, but sound travels a long way, very slowly.
« Last Edit: May 14, 2012, 08:38:02 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so...umm..how do I make this smaller?
Code:
void right14() {
  for (short i=R1; i<R15; i++)
    digitalWrite (i, HIGH);
  digitalWrite (R15, LOW);
}

Works fine as long as the RXX are sequential.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Works fine as long as the RXX are sequential.
...which is why arrays are such a good idea.
"right14" and all its siblings shouldn't be there anyway.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm just glad to see that I am not the only one that makes these sorts of simple mistake.  We can sometimes get "tunnel vision" and focus our attention in the wrong places.  I sometimes help to have a second pair of eyes on the problem.  Especially a pair that does know know enough to have preconceptions.

As soon as read the title, I wondered it they were being used "together" and getting interference.
Logged

Pages: [1]   Go Up
Jump to: