2

I am doing my final year project and here I am attaching the code I am using. According to the code, I will get the sensor output in serial monitor. But I want to get reading once n 10ms. Since serial monitor can't work with that resolution, I need to save the reading in an array and then output the array in serial monitor or any other way. Please help me in modifying the code.

The code is as follows:

/* 
   The circuit:
  * VCC connection of the sensor attached to +5V
  * GND connection of the sensor attached to ground
  * TRIG connection of the sensor attached to digital pin 2
  * ECHO connection of the sensor attached to digital pin 4
 */

const int trigPin = 2;
const int echoPin = 4;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  long duration, cm;

  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  cm = microsecondsToCentimeters(duration);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
  • At 9600 bps there should be no problem sending up to 9 bytes every 10 ms. – Edgar Bonet Jul 20 '16 at 10:56
2

There is one fundamental error with your code that I feel I should point out - one that I see time and time again.

The basic structure of your code is:

  • Forever:
    • Send a ping
    • Wait for the pong
    • Print the result
    • Delay for 100ms (or whatever time you choose)

And you expect to get a result every (delay) milliseconds.

However, you won't. What you will actually get is a result every so often, where that time is defined as 100ms + the time to send and receive a ping.

So if it takes 11.6ms (200cm, the limit of most pin sensors) to receive the pong, then you also delay for 100ms, you're going to end up with 111.6ms between each print. It's even worse if the pong never arrives back - then pulseIn() will wait for up to a second before returning. You can, of course, reduce that to say 15ms by adding the timeout parameter as per the manual

When you reduce your printing delay to just 10ms you actually run the risk of delaying for more than double the time you expect to (21.6ms instead of 10ms).

Instead you need to use millis() to check to see if it is time to print something so that it doesn't matter how long the pong took to arrive, as long as that never exceeds the amount of time you want between prints, otherwise it's going to get delayed regardless.

0

Yeah it shouldn't be an issue, remember delay(time) is a millisecond command, so if you want to get a reading every 10ms, simply put delay(10), and not delay(100). Like the comment above mentions, you should have no problem with a 9600 baud rate serial monitor link showing data every 10ms.

Also, I would highly reccomend checking out the NewPing library (https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home) which is an update library for the ultrasonic distance sensor which I assume you are using based off your code. With that, you can send up to 30 "pings" per second, so if the above solution doesn't fix it for you, this definitely should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.