Serial Port Post Delay

Hi All!

I’m having a really weird problem and I’m not sure what causing it. Basically I’m running the same packet of code 4 times for 4 ultrasonic sensors with really tiny delays between the ping and the echoe. This was working fine and communicating to the serial port at a good rate until a couple of days ago when it suddenly began to freak out to the point where I went out today to buy a new board as I fearing it had been damaged; needless to say, this was not the case and I am still stuck with the problem. If I comment out each section one by one it slowly gets faster until it’s at full speed working with only one sensor. Can anyone spot the issue in my code? I’m guessing it has something to do with pulseIn but I’m new to programming so I’m not sure how all this works internally.

Thanks in advance!

Jack

CODE:

#include <SimpleMessageSystem.h>

 
const int trigPin = 9;
const int echoPin = 10;
 
const int trigPin2 = 3;
const int echoPin2 = 4;

const int trigPin3 = 5;
const int echoPin3 = 6;
 
const int trigPin4 = 7;
const int echoPin4 = 8;
  
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, duration2, duration3, duration4, 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);
  
        delayMicroseconds(15);
        

         pinMode(echoPin, INPUT);
         duration = pulseIn(echoPin, HIGH);
          
        delayMicroseconds(15);
      
pinMode(trigPin2, OUTPUT); 
 
digitalWrite(trigPin2, LOW);
 
        delayMicroseconds(2);
  
digitalWrite(trigPin2, HIGH);
    
        delayMicroseconds(10);
    
digitalWrite(trigPin2, LOW);

        delayMicroseconds(15);

 
        pinMode(echoPin2, INPUT);
        duration2 = pulseIn(echoPin2, HIGH);
        
          delayMicroseconds(15);
      
pinMode(trigPin3, OUTPUT); 
 
digitalWrite(trigPin3, LOW);
 
        delayMicroseconds(2);
  
digitalWrite(trigPin3, HIGH);
    
        delayMicroseconds(10);
    
digitalWrite(trigPin3, LOW);

        delayMicroseconds(15);
 
        pinMode(echoPin3, INPUT);
        duration3 = pulseIn(echoPin3, HIGH);
        
          delayMicroseconds(15);
      
pinMode(trigPin4, OUTPUT); 
 
digitalWrite(trigPin4, LOW);
 
        delayMicroseconds(2);
  
digitalWrite(trigPin4, HIGH);
    
        delayMicroseconds(10);
    
digitalWrite(trigPin4, LOW);

        delayMicroseconds(15);
 
        pinMode(echoPin4, INPUT);
        duration4 = pulseIn(echoPin4, HIGH);
        
        delayMicroseconds(15);
 

 
  
                               // read the value from the sensor
//  messageSendInt(millis());    // along with a timestamp
  messageSendInt(duration);
    messageSendInt(duration2);
      messageSendInt(duration3);
        messageSendInt(duration4);
  messageEnd();
  
  delay(500);
  
}

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

How to use this forum

Hi Nick,

All done! Sorry, didn't know I could do that.

Thanks,

Jack

Can you show the implementation of messageSendInt() and messageEnd()?

You say that the sketch slows down as you add sensors. Currently the iteration rate is defined by the execution time plus the delay(500) at the end. That means it will vary according to the amount of time taken to read the sensors and send the message. If you want to maintain a constant rate you could control the timing explicitly, using the technique demonstrated in the blink without delay example sketch; you'd use that to execute the code current in loop at regular intervals. For example:

// incomplete, untested
void loop()
{
  static unsigned long lastTime = millis();
  const unsigned long INTERVAL = 1000;

  if(millis() - lastTime >= INTERVAL)
  {
    lastTime += INTERVAL;
    sample();
  }
}

void sample()
{
  // put here the logic previously in loop()
}

pulseIn and the delays won't be helping the speed.

There is a library that handles sensors like that (I think):

http://playground.arduino.cc/Code/NewPing

I think that handles multiple sensors without slowing down.

Since the trigger and echo pins are separate you can save some time by doing the pinMode() calls in setup() and not every time through the loop. You only have to do it every time where a single pin is used for both output (trigger) and input (echo).

The default timeout on pulseIn() is one second (a million microseconds). If you don’t get an echo back you may have to wait for the timeout. You might want to set it to something reasonable like 58773 (round trip time for a 10-meter range). That way if you don’t get an echo you wait less than 59 milliseconds instead of 1000 milliseconds.

#include <SimpleMessageSystem.h>

const int trigPin = 9;
const int echoPin = 10;

const int trigPin2 = 3;
const int echoPin2 = 4;

const int trigPin3 = 5;
const int echoPin3 = 6;

const int trigPin4 = 7;
const int echoPin4 = 8;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin, INPUT); 
  pinMode(echoPin2, INPUT); 
  pinMode(echoPin3, INPUT); 
  pinMode(echoPin4, INPUT);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, duration2, duration3, duration4, 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:

  digitalWrite(trigPin, LOW);   
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(15);
  duration = pulseIn(echoPin, HIGH, 58733UL);


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


  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(15);
  duration3 = pulseIn(echoPin3, HIGH, 58733UL);


  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(15);
  duration4 = pulseIn(echoPin4, HIGH, 58733UL);


  // read the value from the sensor
  //  messageSendInt(millis());    // along with a timestamp
  messageSendInt(duration);
  messageSendInt(duration2);
  messageSendInt(duration3);
  messageSendInt(duration4);
  messageEnd();

  delay(500);
}

johnwasser:
Since the trigger and echo pins are separate you can save some time by doing the pinMode() calls in setup() and not every time through the loop. You only have to do it every time where a single pin is used for both output (trigger) and input (echo).

The default timeout on pulseIn() is one second (a million microseconds). If you don’t get an echo back you may have to wait for the timeout. You might want to set it to something reasonable like 58773 (round trip time for a 10-meter range). That way if you don’t get an echo you wait less than 59 milliseconds instead of 1000 milliseconds.

#include <SimpleMessageSystem.h>

const int trigPin = 9;
const int echoPin = 10;

const int trigPin2 = 3;
const int echoPin2 = 4;

const int trigPin3 = 5;
const int echoPin3 = 6;

const int trigPin4 = 7;
const int echoPin4 = 8;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  pinMode(echoPin4, INPUT);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, duration2, duration3, duration4, 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:

digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(15);
  duration = pulseIn(echoPin, HIGH, 58733UL);

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

digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(15);
  duration3 = pulseIn(echoPin3, HIGH, 58733UL);

digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin4, LOW);
  delayMicroseconds(15);
  duration4 = pulseIn(echoPin4, HIGH, 58733UL);

// read the value from the sensor
  //  messageSendInt(millis());    // along with a timestamp
  messageSendInt(duration);
  messageSendInt(duration2);
  messageSendInt(duration3);
  messageSendInt(duration4);
  messageEnd();

delay(500);
}

Hi John!

That completely fixed the problem!!! I’ve been messing around with this for days and it kept crashing everything >_< Thank you SO much!!!

All the best,

Jack