is my code slowing down maxbotix sensor read rate?

Hello,

I'm new to any sort of coding, I have been using others peoples code to make things work generally which has been fine up till not and has allowed me very slowly to build up an idea of how the code works....

I have adjusted code I found for maxbotix ultrasonic sensors (thanks to the author) to make it work with 8 sensors chained together... Maxbotix state that it should be possible for the sensors to provide readings every 100ms... but at the moment with my beginners code it seems to be reading about every 1 second.... could this be because of my ineffcient coding?

Is there something really obvious as to why this is happening with my code? could someone possibly point me in the right direction?

//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09

//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
int pwPin1 = 2;
int pwPin2 = 3;
int pwPin3 = 4;
int pwPin4 = 5;
int pwPin5 = 6;
int pwPin6 = 7;
int pwPin7 = 8;
int pwPin8 = 9;
int triggerPin = 13;        // output pin to start Ultrasound signals 

//variables needed to store values
long pulse1, pulse2, pulse3, pulse4, pulse5, pulse6, pulse7, pulse8, cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8;

void setup() {

  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);

  pinMode(pwPin1, INPUT);
  pinMode(pwPin2, INPUT);
  pinMode(pwPin3, INPUT);
  pinMode(pwPin4, INPUT);
  pinMode(pwPin5, INPUT);
  pinMode(pwPin6, INPUT);
  pinMode(pwPin7, INPUT);
  pinMode(pwPin8, INPUT);
  pinMode(triggerPin, OUTPUT); // set this pin to output 
  //give the sensors time to boot up 
  delay(250);

}

void loop() {

     //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.

  digitalWrite(triggerPin, HIGH); 

  pulse1 = pulseIn(pwPin1, HIGH);
  pulse2 = pulseIn(pwPin2, HIGH);
  pulse3 = pulseIn(pwPin3, HIGH);
  pulse4 = pulseIn(pwPin4, HIGH);
  pulse5 = pulseIn(pwPin5, HIGH);
  pulse6 = pulseIn(pwPin6, HIGH);
  pulse7 = pulseIn(pwPin7, HIGH);
  pulse8 = pulseIn(pwPin8, HIGH);
  
 delayMicroseconds(30);
 digitalWrite(triggerPin, LOW); 

 //58uS per cm
  cm1 = pulse1/58;
  cm2 = pulse2/58;
  cm3 = pulse3/58;
  cm4 = pulse4/58;
  cm5 = pulse5/58;
  cm6 = pulse6/58;
  cm7 = pulse7/58;
  cm8 = pulse8/58;


  Serial.print(cm1);
  Serial.print(" ");
  Serial.print(cm2);
  Serial.print(" ");
  Serial.print(cm3);
  Serial.print(" ");
  Serial.print(cm4);
  Serial.print(" ");
  Serial.print(cm5);
  Serial.print(" ");
  Serial.print(cm6);
  Serial.print(" ");
  Serial.print(cm7);
  Serial.print(" ");
  Serial.print(cm8);
  Serial.println();

 delay(100);

}

If the expected read rate is once per 100 ms, then reading 8 of them will take up to 800 ms. That seems consistent with your observations.

bassmagnetic:

  digitalWrite(triggerPin, HIGH); 

pulse1 = pulseIn(pwPin1, HIGH);
 pulse2 = pulseIn(pwPin2, HIGH);
 pulse3 = pulseIn(pwPin3, HIGH);
 pulse4 = pulseIn(pwPin4, HIGH);
 pulse5 = pulseIn(pwPin5, HIGH);
 pulse6 = pulseIn(pwPin6, HIGH);
 pulse7 = pulseIn(pwPin7, HIGH);
 pulse8 = pulseIn(pwPin8, HIGH);
 
delayMicroseconds(30);
digitalWrite(triggerPin, LOW);

That code looks dodgy to me. I guess the HIGH written to triggerPin initiates a pulse and then you call pulseIn to see how long it takes for the echo to return. However, you call pulseIn() separately for each sensor but don't trigger any other pulses. Don't you also need to send a new pulse for each sensor? Otherwise I don't see how the results from the second and subsequent sensors will contain anything useful; they may just be timing out, slowing down your loop and not returning any useful information.

I suspect it would be possible to detect the echo from a single trigger on all sensors concurrently by polling all the inputs instead of using pulseIn, or by using interrupts to detect the returned pulse, but I've never tried it. Perhaps Google would tell you whether that was viable.