the sampling rate of ultrasonic ping sensor

Hi guys,

I am new to arduino and I am trying out a couple of ultrasonic ping sensors. Initially, if I were to run the sketch provided in the library, the sensors they each sample at a fast rate. However, sampling rate gets slower as I increase my code to compare the readings between 2 sensors and see which sensor is nearer to an object. Is this inevitable or can I still have that initial sampling rate? I read somewhere in this forum that if I store my values into an array and compare the array, they will be faster. This is what I did. Yet they are still being sampled only at once per second. Initially this is sampled many times faster. Any advice to improve the sampling rate? I hope I am clear enough to comprehend.

cheers.

We can't see your code so it is difficult to say something . So please post your code.

Some things to consider:

  • reading two ping sensors take more time than one as read them sequentially (I've not heard of an async version yet)
  • the time to read a sensor is related (linear) to the distance of the object.
  • the more math you add to the sketch the more time it takes. But this time is minimal compared to the two above.

So this is my code:


int ultraSoundSignalPins[] = {7,9,10};   // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"Front Right ", "Front Left ", "Middle"};  // just something to print to indicate direction


 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };
  

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int tempstore[3];
  unsigned long ultrasoundValue;
  for(int i=0; i < 3; i++)
  {
    ultrasoundValue = ping(i);
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    Serial.print("cm, ");    
    delay(10);
  
    for(int j = i ; j < 3; j++)
     {
       tempstore[j] = ping(i);
     }
     
       
     if (tempstore[0] == tempstore[1]){
       Serial.print (" Same distance " );
       if (tempstore[0] == 5){
       Serial.print (" 5cm from obstacle ");
        // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 5; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes2 = noteDuration * 1.30;
    delay(pauseBetweenNotes2);
    // stop the tone playing:
    noTone(8);
  
  delay(10);
     }
       }   
     }
      else
       Serial.print (" Conditions not met " ); 


 }
  Serial.println();
  delay(3);
  
 } 

//Ping function
unsigned long ping(int i)
{
  unsigned long echo;

  pinMode(ultraSoundSignalPins[i], OUTPUT); // Switch signalpin to output
  digitalWrite(ultraSoundSignalPins[i], LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignalPins[i], LOW); // Holdoff
  pinMode(ultraSoundSignalPins[i], INPUT); // Switch signalpin to input
  digitalWrite(ultraSoundSignalPins[i], HIGH); // Turn on pullup resistor
  echo = pulseIn(ultraSoundSignalPins[i], HIGH); //Listen for echo
  return (echo / 29) / 2; //convert to CM 
}

I am looking into measuring the sensors and comparing the left and right sensors to see if the reading matches. If it does, it would play a melody on the buzzer. However, the sampling speed seem to reduce significantly after I used 3 sensors. Any advice on improving the code to be more efficient in sampling?

Cheers.

If it does, it would play a melody on the buzzer. However, the sampling speed seem to reduce significantly after I used 3 sensors. And advice on improving the code to be more efficient in sampling?

get rid of the use of delay() in your code. You are blocking its performance

esp these lines are killing performance

int pauseBetweenNotes2 = noteDuration * 1.30;
    delay(pauseBetweenNotes2);

Add a Serial.print() to see how much..

  • check the sketch "blink without delay" how to write delay-less