Go Down

Topic: HC-SR04 Ultrasonic distance finder  (Read 529 times) previous topic - next topic

HeyDJ

Hi All this is my first post having spent 2 days with 6 different ways of doing multiple sensors i am hoping that some one has an answer

this is my current code

Code: [Select]
/*
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
   The Arduino then takes this information and illuminates a strip of
   NeoPixel's based on the distance of the object from the sensor.
   This code was developed partially from Ping))) code found in the public domain
   written by David A. Mellis, and adapted to the HC-SRO4 by Tautvidas Sipavicius,
   while other portions were written by Charles Gantt and Curtis Gauger from
   http://www.themakersworkbench.com.
 */

 //Tell the Arduino IDE to include the FastLED library
 #include <FastLED.h>
 
 //Setup the variables for the HC-SR04
const int trigPin = 6;
const int echoPin = 5;



//Setup the variables for the NeoPixel Strip
#define NUM_LEDS 8 // How many leds in your strip?
//#define DATA_PIN 6 // What pin is the NeoPixel's data line connected to?
#define DATA_PIN     2
#define CLOCK_PIN   3


#define COLOR_ORDER RGB
#define CHIPSET     WS2801
#define NUM_LEDS    8

#define BRIGHTNESS  60
CRGB leds[NUM_LEDS]; // Define the array of leds
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  FastLED.addLeds<CHIPSET, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );

}
 
void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
   if (inches <= 34)  {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::White); //{whibe(30);
     FastLED.show();
   delay(39600);
}
  else if (inches >= 36) {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Black);
     FastLED.show();
  }
 
  delay(100);
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


this functions correctly as i would expect if an object moves within the given distance parameter the led's switch on this only gives me one direction.
i would like to add a second sensor and have that trigger the same result from the opposite side (as in back to back sensors.
i have tried newPing  example 15 sensor array and get 0 cm from the second sensor also tried various other code samples to achieve the result with no joy.

i have tested all 5 of my current stock of sensors and all are good
can anyone point me in the right direction i am stumped

 
If I am meant to try how is it i fail and sometimes give up !!!!.
But normally i do not fail i just put it on the back burner and one day soon i won't be able to stop completing thing off the back burner

wvmarle

Please explain clearly what it does, and what it's supposed to do.

It is normally no problem to add a second sensor, just make sure that you give echoes time to die out or they may trigger each other.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

HeyDJ

Hi thanks for the reply

the idea is to set distance on each sensor so that if something moves in that range it triggers Ws2801 intelligent  led's to light up for a period of time and then go off again.

with the code posted i have it working with one sensor in one direction, but want to add a second sensor facing the opposite direction.

i have tried several different sketches that should work but to date only registers a distance on the first sensor the second one either does not register a distance or returns 0cm





If I am meant to try how is it i fail and sometimes give up !!!!.
But normally i do not fail i just put it on the back burner and one day soon i won't be able to stop completing thing off the back burner

wvmarle

I see code for only one sensor in the code you posted.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

HeyDJ

And in there is the problem as i stated in my first post i have used three or four multiple sensor sketches and all fail to operate the second sensor correctly  as in returning jibberish or 0cm as a result so i posted the single sensor sketch that works in  the hope that some one could point me in the right direction to add a second sensor to the code that would work.

once i can grasp the concept i can usually work out why my attempts have so far not worked, i have only been using Arduino for a couple of months an have limited programming experience.

any help would be appreciated
If I am meant to try how is it i fail and sometimes give up !!!!.
But normally i do not fail i just put it on the back burner and one day soon i won't be able to stop completing thing off the back burner

wvmarle

Without actual code of your attempts, what's there to help with? It's done over and over successfully, so that's not the problem.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

HeyDJ

Ok I Think i have Solved This but if any one can see a better solution i am open to comments

Code: [Select]
//Tell the Arduino IDE to include the FastLED library
 #include <FastLED.h>
 
#define echoPin1 5 // Echo Pin for sonar 1
#define trigPin1 6 // Trigger Pin for sonar 1
#define echoPin2 10 // Echo Pin for sonar 2
#define trigPin2 9 // Trigger Pin for sonar 2

//Setup the variables for the NeoPixel Strip
#define NUM_LEDS 8 // How many leds in your strip?
//#define DATA_PIN 6 // What pin is the NeoPixel's data line connected to?
#define DATA_PIN     2
#define CLOCK_PIN   3

#define COLOR_ORDER RGB
#define CHIPSET     WS2801
#define NUM_LEDS    8

#define BRIGHTNESS  90
CRGB leds[NUM_LEDS]; // Define the array of leds
 
long duration, inches, cm;
long duration1, distance1; // Duration used to calculate distance
long duration2, distance2;
int count=0;
int ledStatus =0;
int iterations = 8;
void setup() {

 Serial.begin (9600); // initiate serial communication
 
  FastLED.addLeds<CHIPSET, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
 
 pinMode(trigPin1, OUTPUT); // trigger pin as output
 pinMode(echoPin1, INPUT);  // echo pin as input
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */

/* At least 10 microsecond high level signal is required to trigger
 * pin.
 *               _____
 *              |     |
 * -------------!     !---------
 *         .....|10us |........
 * the module then produce eight 40KHz pulse signal and waits to receive echo
 */
 digitalWrite(trigPin1, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin1, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin1, LOW);
 // pulseIn( ) function determines a pulse width in time
 // duration of pulse is proportional to distance of obstacle
 duration1 = pulseIn(echoPin1, HIGH);

 digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);

   
 //  distance = (high level timeƗvelocity of sound (340M/S) / 2,
 //  in centimeter = uS/58


 
 distance1 = duration1/58.2;
 if(distance1<100)
   distance1 = 1;
 else distance1 = 0;
 
 
 distance2 = duration2/58.2;
 if(distance2<90)
   distance2 = 1;
 else distance2 = 0;
 

   
 if (ledStatus >= 2 ) {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::Magenta); //{whibe(30);
     FastLED.show();
     
   
}
  else if   (ledStatus >= 0) {fill_solid( &(leds[0]), NUM_LEDS /*number of leds*/, CRGB::White);
     FastLED.show();
     
     delay(20000);
}
 

 // add the result from all sensor
 count = distance1 + distance2;

 // LedStatus = Sensor reads movement
 ledStatus = 2 - count;
 
 Serial.println(ledStatus);
 //  status update
 delay(100);
 
}


Thanks for looking
If I am meant to try how is it i fail and sometimes give up !!!!.
But normally i do not fail i just put it on the back burner and one day soon i won't be able to stop completing thing off the back burner

zhomeslice

Ok I Think i have Solved This but if any one can see a better solution i am open to comments

Thanks for looking
This is one way to have multiple HC-SR04 Sensors. It uses interrupts and can sense watch the ECHO pulse from any pin.
I pulse all the HC-SR04 Sensors at the exact same time Connect the trigger pin on all the HC-SR04's to pin 2
if you want or need to use another pin change this line near the top:
Code: [Select]
#define TriggerPin  2                  // output pin all trigger pins are linked to.

Now attach each ECHO pin to a different Input
Un-remark the pins you have sensors on and remark out the ones you don't
Code: [Select]

void setup() {

  Serial.begin(115200);
  Serial.println("Send 1 for Readable Data, Send 0 For Interger Data");
  SensorCount = 0;
  pinMode(TriggerPin, OUTPUT);
  // enable interrupt for pin...
  // Select the pins you want to use for input
  // pciSetup(0); // Serial Communication
  // pciSetup(1); // Serial Communication
  // pciSetup(2); //This is my trigger pin
  pciSetup(3);
  pciSetup(4);
  pciSetup(5);
  pciSetup(6);
  pciSetup(7);
  pciSetup(8);
  pciSetup(9);
  pciSetup(10);
  pciSetup(11); // SPI communications
  pciSetup(12); // SPI communications
  pciSetup(13); // SPI communications
  pciSetup(14); // A0
  pciSetup(15); // A1
  pciSetup(16); // A2
  pciSetup(17); // A3
  pciSetup(18); // A4  i2c Communication
  pciSetup(19); // A5  i2c Communication
  pciSetup(20); // A6  for some clones
  pciSetup(21); // A7  for some clones
}


Here's my setup:

HC

hackerjoe


Go Up