arrz
March 12, 2024, 10:20pm
1
Hy, Let me first tell that I am a complete beginner.
I wanted to controll two HC-SR04 ultrasonic sensors with one controllerboard for an project,
I tryed to do that with this code:
const int trig = 6;
const int echo = 5;
int lamp = 3;
int lamp1 = 9;
const int trig1 = 10;
const int echo1 = 11;
long totaltime;
int distance;
long totaltime1;
int distance1;
void setup ()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(lamp, OUTPUT);
pinMode(lamp1, OUTPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds (10);
digitalWrite(trig, LOW);
digitalWrite(trig1, LOW);
delayMicroseconds (5);
digitalWrite(trig1, HIGH);
delayMicroseconds (10);
digitalWrite(trig1, LOW);
totaltime = pulseIn(echo, HIGH);
totaltime1 = pulseIn(echo1,HIGH);
delay(500);
distance= (totaltime / 2) / 29.1;
distance1= (totaltime1 / 2) / 29.1;
Serial.print("Distance:");
Serial.println(distance);
if (distance > 10)
{
digitalWrite (lamp, HIGH);
}
else
{
digitalWrite(lamp, LOW);
}
if (distance1 > 10);
{
digitalWrite (lamp1, HIGH);
}
}
And yes, I put a '1' after every piece of code for the second sensor because I tought that that would be the most logic way to to this, but it does not work.
so, What did I wrong? or is there a better way?
arrz:
it does not work.
That is a poor description of the problem
What is stopping the second sensor picking up the signal from the first one ?
arrz
March 12, 2024, 10:30pm
3
no it's reading nothing, It's just giving zero's
Start by using only one sensor in the code and circuit
Does that work ?
Post your code that works with a single sensor
arrz
March 12, 2024, 10:51pm
7
const int trig = 6;
const int echo = 5;
int lamp = 3;
long totaltime;
int distance;
void setup ()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(lamp, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds (10);
digitalWrite(trig, LOW);
totaltime = pulseIn(echo, HIGH);
delay(500);
distance= (totaltime / 2) / 29.1;
distance1= (totaltime1 / 2) / 29.1;
Serial.print("Distance:");
Serial.println(distance);
if (distance > 10)
{
digitalWrite (lamp, HIGH);
}
else
{
digitalWrite(lamp, LOW);
}
}
You need to trigger them one at a time, with a short delay between them, so the echos from the first one don't get read by the second one.
xfpd
March 13, 2024, 4:20pm
9
Does this code (Post 7) show a valid distance with each sensor?
AresXT
March 14, 2024, 4:36am
10
Should we suggest him using ultra sonic sensor library or let him learn by this one what you guys suggest
xfpd
March 14, 2024, 4:45am
11
AresXT:
sensor library
Why? Make a pulse train, read a pulse train. What else is needed?
AresXT
March 14, 2024, 4:50am
12
It would make him easy to handle multiple sensors .If he using only 2 then i think it would be fine
xfpd
March 14, 2024, 5:13am
13
How many do you count? What does the topic say?
AresXT
March 14, 2024, 5:21am
14
Yeah i know , It was just my suggestion that's all
AresXT
March 14, 2024, 5:24am
15
I'll Made few changes to the code . I'll hope it helps
const int trig = 6;
const int echo = 5;
int lamp = 3;
int lamp1 = 9;
const int trig1 = 10;
const int echo1 = 11;
long totaltime;
int distance;
long totaltime1;
int distance1;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(lamp, OUTPUT);
pinMode(lamp1, OUTPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);
delayMicroseconds(2);
totaltime = pulseIn(echo, HIGH);
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(20);
digitalWrite(trig1, LOW);
delayMicroseconds(2);
totaltime1 = pulseIn(echo1, HIGH);
delay(100);
distance = (totaltime / 2) / 29.1;
Serial.print("Distance:");
Serial.println(distance);
distance1 = (totaltime1 / 2) / 29.1;
Serial.print("Distance1:");
Serial.println(distance1);
if (distance > 10) {
digitalWrite(lamp, HIGH);
} else {
digitalWrite(lamp, LOW);
}
if (distance1 > 10)
{
digitalWrite(lamp1, HIGH);
} else {
digitalWrite(lamp1, LOW);
}
}
xfpd
March 14, 2024, 3:36pm
17
You deleted half the code and no library to help?
AresXT
March 15, 2024, 5:36am
18
AresXT:
digitalWrite(lamp, HI
Thank you for spotting the missing lines. As per your suggestion i didn't used any library just changed the order of calling the pulseIN
system
Closed
September 11, 2024, 5:37am
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.