Arduino UNO - Suddenly slow - same code slow reading!

Hi!

I wrote code to use four ultrasonic sensors at the same time, and it was working fine. (I'm absolutely new to this, so the code might not have the best syntax.)

A few days ago, I wanted to change the values of maximum and minimum distances, and when I uploaded the code again to the Arduin UNO, it now reads incredibly slowly.

It can't be something in the code, even if it is not perfect, because before I was reading well and it was the same code.

#define echoPin 11 //echo
#define trigPin 12 //trig

#define echoPin01 6 //echo01
#define trigPin01 7 //trig01

#define echoPin02 8 //echo02
#define trigPin02 9 //trig02

#define echoPin03 3 //echo03
#define trigPin03 4 //trig03


int maximumRange = 200;
int minimumRange = 0;
int maximumRange01 = 200;
int minimumRange01 = 0;
int maximumRange02 = 200;
int minimumRange02 = 0;
int maximumRange03 = 200;
int minimumRange03 = 0;

long duration, duration01, duration02, duration03;
long distance, distance01, distance02, distance03; //duration used to calculate distance


void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

pinMode(trigPin01, OUTPUT);
pinMode(echoPin01, INPUT);

pinMode(trigPin02, OUTPUT);
pinMode(echoPin02, INPUT);

pinMode(trigPin03, OUTPUT);
pinMode(echoPin03, INPUT);

Serial.begin(9600);

}

void loop() {
  
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

digitalWrite(trigPin01, LOW);
delayMicroseconds(2);
digitalWrite(trigPin01, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin01, LOW);
duration01 = pulseIn(echoPin01, HIGH);

digitalWrite(trigPin02, LOW);
delayMicroseconds(2);
digitalWrite(trigPin02, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin02, LOW);
duration02 = pulseIn(echoPin02, HIGH);

digitalWrite(trigPin03, LOW);
delayMicroseconds(2);
digitalWrite(trigPin03, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin03, LOW);
duration03 = pulseIn(echoPin03, HIGH);


//calculate distance in CM based on SOS
distance = duration / 58.2;
distance01 = duration01 / 58.2;
distance02 = duration02 / 58.2;
distance03 = duration03 / 58.2;

Serial.print("A ");
if (distance >= maximumRange || distance <= minimumRange) {
  Serial.println("300");
} else {
  Serial.println(distance);
}


Serial.print("B ");
if (distance01 >= maximumRange01 || distance01 <= minimumRange01) {
  Serial.println("300");
} else {
  Serial.println(distance01);
}


Serial.print("C ");
if (distance02 >= maximumRange02 || distance02 <= minimumRange02) {
  Serial.println("300");
} else {
  Serial.println(distance02);
}


Serial.print("D ");
if (distance03 >= maximumRange03 || distance03 <= minimumRange03) {
  Serial.println("300");
} else {
  Serial.println(distance03);
}

delay(100);

}

type or paste code here

I tried reinstalling Arduino and even reinstalling Windows.

Any idea what the problem could be?

Thanks!

Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

Please define slow. Is that the code on the Arduino that is slow?

You are aware that the further an object is from your sensor, the longer it takes to read the echo and hence the execution of your code can slow down.

Slow, I mean. When I wrote the code and tested it the first time, the loop was repeating many times per second and now it takes approximately more than one second per reading, as you can see in the picture of the console that I uploaded

Are the four sensors in different rooms? Because, if not, you're pinging far too frequently.

And the default timeout for pulseIn is . . ?

1 Like

Before it was working with all the sensors in the same room and the code is the same as before.

The thing is what could have changed in the process of uploading the code to the UNO board Tha makes it process the data slower than before.

I don't know what you mean by "working", but I'd suggest you check your wiring.

(You have far too much code, and are pinging far too frequently)

By "working" i mean that it was reading much faster than now.

How can I correct the pinging time?

Maybe the wall was closer to the sensor "than now". Sound waves in air travel at 3 milliseconds per meter.

The simplest way is to put a delay between each ping, say 30 - 40 ms

Nothing helped to improve the reading speed :frowning:

What I've tried is to come back to only one sensor and it's fast again.

The moment I add the code for the second sensor it gets slower.

Is there any possible way to make the code with multiple sensors faster?

one sensor looks like this

#define echoPin 11 //echo
#define trigPin 12 //trig

int maximumRange = 200;
int minimumRange = 0;

long duration;
long distance; //duration used to calculate distance

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin(9600);

}

void loop() {
  
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//calculate distance in CM based on SOS
distance = duration / 58;
Serial.print("A ");
if (distance >= maximumRange || distance <= minimumRange) {
  Serial.println("300");
} else {
  Serial.println(distance);
}

}

and now it reads many, many times in one second

Too many times

I don't know who introduced this, but I'm going to hunt them down, and be jolly unpleasant to them.

Fun but counterintuitive factlet, encountered when attempting to shift the sample point of a "carefully" timed, 800 point data sampling loop:

delayMicroseconds(4) + delaymicroSeconds(16) != delayMicroseconds(20).

Total sensor readout timing for former = 38576 us, for the latter = 40154 us, a 4% difference in duration. I expected the former to be slightly longer, due to the extra function call, but the reverse is true.

Sorry about my English!

How would write that code?

I don't have your hardware, so I can't test it, but the following code compiles

const struct SensorDesc {
  const byte echoPin;
  const byte trigPin;
  const int  maxRange;
  const int  minRange;
  const char name;
} sensorDesc [] { {11, 12, 200, 0, 'A'},
                  { 6,  7, 200, 0, 'B'},
                  { 8,  9, 200, 0, 'C'},
                  { 3,  4, 200, 0, 'D'}};

void setup() 
{
  for (auto& pins : sensorDesc) {
    pinMode(pins.trigPin, OUTPUT);
    digitalWrite (pins.trigPin, LOW);
    pinMode(pins.echoPin, INPUT);
  }
  Serial.begin(9600); // 115200 would be better...
}

int range (const SensorDesc& pins)
{
  digitalWrite (pins.trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite (pins.trigPin, LOW);
  uint32_t duration = pulseIn (pins.echoPin, HIGH, 30000); // timeout if no echo received
  return duration / 58.2;
}

void loop() 
{
  for (auto& desc : sensorDesc) {
    Serial.print(desc.name);
    int distance = range (desc);
    if (distance >= desc.maxRange || distance <= desc.minRange) {
      Serial.println("300");
    } else {
      Serial.println(distance);
    }
    delay (30); // limit the ping rate to about 30Hz
  }  
  delay(100); // Leave a gap ...
}

Thanks a lot! I'll try it

I think that's exactly the problem. I found that what I was doing wrong was that I made the code for four sensors and I was only using three. So when it was reading the disconnected sensor took it one second to restart.

If I have all the sensors working and connected is not working bad.

But probably using your method I coul have a more efficient real-time response.

Thanks a lot for your comments! @anon56112670 @Delta_G

It was really helpful!

It's always good to check wiring.
And debug print values, and see if they're the result of a sensor timeout.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.