How to use two Ultrasonic Sensor (HC-SR04) in just one arduino duemilanove!?
I can not use>3<
I just follow this
fritzing.org/projects/arduino-ultrasonic-ranging-with-hc-sr04/
Um...get 2 times each components and rewrite the programming so it take 2 rangefinder? (Didn't openned the link)
The links nothing more than an ad from the dude who sells those units on ebay. I bought one. Dubious quality.
Anyway, getting 2 to work is trivial. It sounds like you should try to get 1 working first and expand from there... If you already have 1 working, then you should no try to understand why and how it works. Then adding a 2nd or 3rd should be an exercise in utter-obviousness.
And finally, you'll probably get more sensible replies if you actually try it out and then post with specific questions referring to your current implementation and the problem/s it exhibits.
G.
I can use one Ultrasonic Sensor to control a servo and PWM(Drive Motor).
Code:
#include <Servo.h>
Servo myservo;
int pos1 = 90;
int pos2 = 150;
int pingPin = 13;
int inPin = 12;
void setup() {
myservo.attach(10);
pinMode(0, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
** long duration;**
** pinMode(pingPin, OUTPUT);**
** digitalWrite(pingPin, LOW);**
** delayMicroseconds(2);**
** digitalWrite(pingPin, HIGH);**
** delayMicroseconds(10);**
** digitalWrite(pingPin, LOW);**
** pinMode(inPin, INPUT);**
** duration = pulseIn(inPin, HIGH);**
** delay(100);**
if (duration <= 2900)
{ **
** analogWrite(11, 0);
** delay(50);**
** digitalWrite(0, HIGH); **
** delay(50);**
** analogWrite(11, 100);**
** delay(1000);**
** myservo.write(pos2);**
** delay(200);**
** delay(700);**
** myservo.write(pos1);**
** analogWrite(11, 0);**
** digitalWrite(0, LOW);**
** delay(100);**
** analogWrite(11,130);**
** delay(100);**
** }**
}
else if (duration > 2900 && duration <= 11600)
{
** digitalWrite(0, LOW);**
** analogWrite(11,95);**
** delay(100);**
** myservo.write(pos1);**
}
else
{
** digitalWrite(0, LOW);**
** analogWrite(11, 130);**
** delay(100);**
** myservo.write(pos1);**
}
}
But, when i put the second one and without change the program.
The second ultrasonic Sensor work and same with the first one.
without change the program
You're going to have to change your program to achieve whatever it is you're trying to achieve (what that is is clearly a secret you're not willing to share).
Hope you're not expecting too many more replies...
Enjoy...
I have change the program to This:
#include <Servo.h>
Servo myservo;
int pos1 = 90;
int pos2 = 150;
int pos3 = 30;
int pingPin = 13;
int inPin = 12;
int pingPin_1 = 9;
int inPin_1 = 8;
void setup() {
myservo.attach(10);
pinMode(0, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
** long duration, duration_1;**
** pinMode(pingPin, OUTPUT);**
** digitalWrite(pingPin, LOW);**
** delayMicroseconds(2);**
** digitalWrite(pingPin, HIGH);**
** delayMicroseconds(10);**
** digitalWrite(pingPin, LOW);**
** pinMode(inPin, INPUT);**
** duration = pulseIn(inPin, HIGH);**
** delay(100);**
** pinMode(pingPin_1, OUTPUT);**
** digitalWrite(pingPin_1, LOW);**
** delayMicroseconds(2);**
** digitalWrite(pingPin_1, HIGH);**
** delayMicroseconds(10);**
** digitalWrite(pingPin_1, LOW);**
** pinMode(inPin_1, INPUT);**
** duration_1 = pulseIn(inPin_1, HIGH);**
** delay(100);**
if (duration <= 2900)
{ **
** analogWrite(11, 0);
** delay(50);**
** digitalWrite(0, HIGH); **
** delay(50);**
** analogWrite(11, 100);**
** delay(1000);**
** myservo.write(pos2);**
** delay(200);**
** if (duration_1 <= 1200)**
** {**
** }**
** else**
** {**
** delay(700);**
** myservo.write(pos1);**
** analogWrite(11, 0);**
** digitalWrite(0, LOW);**
** delay(100);**
** analogWrite(11,130);**
** delay(100);**
** }**
}
else if (duration > 2900 && duration <= 11600)
{
** digitalWrite(0, LOW);**
** analogWrite(11,95);**
** delay(100);**
** myservo.write(pos1);**
}
else
{
** digitalWrite(0, LOW);**
** analogWrite(11, 130);**
** delay(100);**
** myservo.write(pos1);**
}
}
The second ultrasonic Sensor also work and same with the first one.
The second ultrasonic Sensor also work and same with the first one
If both ultrasonic sensors work, what's the problem?
Please use the # button when posting code.
the line:-
pinMode(0, OUTPUT);
looks like you are using pin 0, this is the serial port. Generally you should avoid pins 0 and 1.
Re: PaulS
The problem is the both sensor have the same function.
Re:Grumpy_Mike
I will try to use another Pin.
Thank you for your remind! =]
The problem is the both sensor have the same function
Generally, in a mass-produced product, this is a Good ThingTM
Can you tell us what you want to happen, and how this differs from what is happening?
RE:AWOL
I want to use the Ultrasonic Sensor in my toy car.
One in the front, another in the behind.
The car will having a action(Astern) when Ultrasonic Sensor(Front) sense a object
[I want to stop the action when Ultrasonic Sensor(Behind) sense a object ]
First off put the two pinMode lines in setup.
next as you don't actually use duration_1 to do anything then it is no supprise that it does nothing. The line
if (duration_1 <= 1200)
{
}
else
{
might as well not be in the code for all it does.
I modify the program to follow:
#include <Servo.h>
Servo myservo;
int pos1 = 90;
int pos2 = 150;
int pos3 = 30;
int number = 0;
int decrease =0;
void setup() {
myservo.attach(10);
pinMode(7, OUTPUT);
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
}
void loop()
{
long front;
// pinMode(pingPin, OUTPUT);
digitalWrite(13, LOW);
delayMicroseconds(2);
digitalWrite(13, HIGH);
delayMicroseconds(10);
digitalWrite(13, LOW);
// pinMode(inPin, INPUT);
front = pulseIn(12, HIGH);
delay(100);
if (front <= 2900)
{
analogWrite(11, 0);
delay(50);
digitalWrite(7, HIGH);
delay(50);
analogWrite(11, 90);
delay(600);
myservo.write(pos2);
delay(100);
delay(600);
myservo.write(pos1);
delay(100);
number = 1;
decrease = 0;
}
else if (front > 2900 && front <= 11600)
{
if (number = 1)
{ digitalWrite(7, LOW);
analogWrite(11,95);
delay(30);
}
if (decrease >= 50)
{
digitalWrite(7, LOW);
analogWrite(11,45);
delay(15);
myservo.write(pos1);
}
else
{
digitalWrite(7, LOW);
analogWrite(11,75);
delay(15);
myservo.write(pos1);
decrease = decrease + 1;
}
}
else
{
digitalWrite(7, LOW);
analogWrite(11, 130);
delay(15);
myservo.write(pos1);
decrease = decrease + 1;
}
}
First, There are two ultrasonic sensor, i call them front ultrasonic and back ultrasonic.
Pin 12,13 Front Ultrasonic
Pin 8,9 Back Ultrasonic
This program only use the front ultrasonic
But i don't know why there are some problems
1. When i connect both ultrasonic sensor without changing the program. The back ultrasonic work same as the front ultrasonic.
That means when back ultrasonic detect object, the car move backward.
2. When i connect only the back ultrasonic vcc and ground to same voltage source, same problem
3. When i connect the back ultrasonic vcc and ground to an external voltage source, same problem
Maybe you would ask me why use this program rather than two ultrasonic program
Because i wanna step by step, If only connect front ultrasonic, it works perfect,
But i really don't understand why if i connect the other ultrasonic sensor to it, it also works
I would be happy if anyone can help me to fix this problem
You need to draw out exactly how you have connected things.
You need to control both ultrasonic boards in your sketch. If not then the board with the unconnected trigger will keep firing and interfere with the other board. So connect both and keep the other trigger low. I think you will find that it does not then interfere with the first.
You need to set the pinMode for pins 8 and 9, to OUTPUT, so that the back sensor does not do anything.
You should use meaningful names for the pins. What is connected to pins 7, 11, 12, and 13?