Go Down

Topic: Code for 2 or 4 ultrasonic hc-sr04 (Read 1 time) previous topic - next topic

bobafet67

Hi Guys just starting here
I am making an R2D2 with arduino
my idea is to put on it 4 ultrasonic sensor hc-sr04
I got the code for one sensor and I am modifying it for 2 sensor
as result the LEFT sensor works and the Right sensor not.
Can you help please?
Thanks
the code:

/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/


//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx

//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx

#define LEDPin 13 // Onboard LED


int maximumRangelx = 300; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance

int maximumRangerx = 300; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationrx, distancerx; // Duration used to calculate distance

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT); 
 
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
 
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin1, LOW);
  digitalWrite(trigPin2, LOW);
 
  durationlx = pulseIn(echoPin1, HIGH);
  durationrx = pulseIn(echoPin2, HIGH);

  //Calculate the distance (in cm) based on the speed of sound.
  distancelx = durationlx/58.2;
  distancerx = durationrx/58.2;

  if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.print("LX -1");
    digitalWrite(LEDPin, HIGH);
   
  } 
 
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancelx);
    Serial.print( "  cm");
   // digitalWrite(LEDPin, LOW);
 
}
  if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.println("   RX -1");
   // digitalWrite(LEDPin, HIGH);
   
  }
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancerx);
    Serial.print( "  cm");
    //digitalWrite(LEDPin, LOW);
  }

  //Delay 50ms before next reading.
  delay(25);
}

johnwasser

Don't trigger the second sender until you get the echo from the first one:

Code: [Select]

/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/


//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx

//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx

#define LEDPin 13 // Onboard LED


int maximumRangelx = 300; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance

int maximumRangerx = 300; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationrx, distancerx; // Duration used to calculate distance

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT); 
 
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  //Calculate the distance (in cm) based on the speed of sound.
  durationlx = readHC-SR04(trigPin1, echoPin1)/58.2;
  durationrx = readHC-SR04(trigPin2, echoPin2)/58.2;

  if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.print("LX -1");
    digitalWrite(LEDPin, HIGH);
  } 
 
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancelx);
    Serial.print( "  cm");
   // digitalWrite(LEDPin, LOW);
}
  if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.println("   RX -1");
   // digitalWrite(LEDPin, HIGH);
   
  }
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancerx);
    Serial.print( "  cm");
    //digitalWrite(LEDPin, LOW);
  }

  //Delay 50ms before next reading.
  delay(25);
}

unsigned long readHC-SR04(int triggerPin, int echoPin)
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

bobafet67

Hi, first thanks
then I am sorry I didn't get it
If I use your code that I guess I should modify for my case, I get an error here:

void loop() {
  //Calculate the distance (in cm) based on the speed of sound.
  durationlx = readHC-SR04(trigPin1, echoPin1)/58.2;
  durationrx = readHC-SR04(trigPin2, echoPin2)/58.2;


this is the error:

In function 'void loop()':
sketch_jul04c:46: error: 'readHC' was not declared in this scope
sketch_jul04c:46: error: 'SR04' was not declared in this scope
sketch_jul04c.ino: At global scope:
sketch_jul04c:82: error: expected initializer before '-' token
sketch_jul04c:84: error: expected constructor, destructor, or type conversion before '(' token
sketch_jul04c:85: error: expected constructor, destructor, or type conversion before '(' token


and I see you add the last part on the code.
Please give me info
Thanks

johnwasser

Sorry, I forgot that '-' is not a legal identifier character.

Code: [Select]

/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/


//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx

//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx

#define LEDPin 13 // Onboard LED


int maximumRangelx = 300; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance

int maximumRangerx = 300; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationrx, distancerx; // Duration used to calculate distance

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT); 
 
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  //Calculate the distance (in cm) based on the speed of sound.
  durationlx = readHC-SR04(trigPin1, echoPin1)/58.2;
  durationrx = readHC-SR04(trigPin2, echoPin2)/58.2;

  if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.print("LX -1");
    digitalWrite(LEDPin, HIGH);
  } 
 
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancelx);
    Serial.print( "  cm");
   // digitalWrite(LEDPin, LOW);
}
  if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.println("   RX -1");
   // digitalWrite(LEDPin, HIGH);
   
  }
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancerx);
    Serial.print( "  cm");
    //digitalWrite(LEDPin, LOW);
  }

  //Delay 50ms before next reading.
  delay(25);
}

unsigned long readHC-SR04(int triggerPin, int echoPin)
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

bobafet67

Hi, no I did delete "-" already but it is not just that
I am going to read about the command "read".
But if you get the mistake please let me know because I would like to use 4 sensor.
I have modified my script moving the code order after your suggestion and it works.
please run it there is just a small issue I think because timing.

New code:  (btw I am on Mac and I can't paste the code like you do!!!)

/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com/
on 10 Nov 2012.
*/

//sensor lx
#define echoPin1 7 // Echo Pin lx
#define trigPin1 6 // Trigger Pin lx

//sensor rx
#define echoPin2 5 // Echo Pin dx
#define trigPin2 4 // Trigger Pin dx

#define LEDPin 13 // Onboard LED


int maximumRangelx = 200; // Maximum range needed
int minimumRangelx = 0; // Minimum range needed

int maximumRangerx = 200; // Maximum range needed
int minimumRangerx = 0; // Minimum range needed
long durationlx, distancelx; // Duration used to calculate distance
long durationrx, distancerx; // Duration used to calculate distance

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT); 
 
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  /* The following trigPin/echoPin cycle is used to determine the
   distance of the nearest object by bouncing soundwaves off of it. */
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
 
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(trigPin1, LOW);
  durationlx = pulseIn(echoPin1, HIGH);
  //Calculate the distance (in cm) based on the speed of sound.
  distancelx = durationlx/58.2;
 
 
  if (distancelx >= maximumRangelx || distancelx <= minimumRangelx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.print(" -1  LX ");
    //digitalWrite(LEDPin, HIGH);
   
  } 
 
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancelx);
    Serial.println( "  cm  LX ");
   // digitalWrite(LEDPin, LOW);
 
}
   delay(25);

 
 
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
 
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin2, LOW);
  durationrx = pulseIn(echoPin2, HIGH);
  //Calculate the distance (in cm) based on the speed of sound.
  distancerx = durationrx/58.2;

 
  if (distancerx >= maximumRangerx || distancerx <= minimumRangerx){
    /* Send a negative number to computer and Turn LED ON
     to indicate "out of range" */
    Serial.println("    -1   RX ");
   // digitalWrite(LEDPin, HIGH);
   
  }
  else {
    /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
    Serial.print(distancerx);
    Serial.println( "  cm RX ");
    //digitalWrite(LEDPin, LOW);
  }

  //Delay 50ms before next reading.
  delay(25);
}



Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy