Hi folks,
I am trying desperately to get 4 Ultrasonic Sensors to output to serial as fast as possible. The thing is when I use a single Ultrasonic Sensor I can output "in real time" once every 10ms with the first patch I will copy here.
After trying to multiply the code lines from this first sketch to adapt it to 4 sensors, I found a thread which suggested using arrays for multiple sensors (that I will copy here as a second sketch).
Well I tried multiplying the code lines, I tried the sketch with arrays, I tried stripping this second sketch, I even deleted the delays which I know is not such a good idea, all with no luck!
Is there a fix somewhere so as an arduino with multiple sensors would output to serial as fast as an arduino with only one sensor?
This is the typical sketch for outputing to serial from a single Ultrasonic Sensor
/* Ping))) Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
http://www.arduino.cc/en/Tutorial/Ping
created 3 Nov 2008
by David A. Mellis
modified 30 Jun 2009
by Tom Igoe
This example code is in the public domain.
*/
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;
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, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print(" cm");
Serial.println();
delay(10);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
This is the second sketch which is skipping a few "beats" even though I tried everything I could! It's for multiple Ultrasonic Sensors
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236272388
int ultraSoundSignalPins[] = {7,8,9,12}; // Front Left,Front, Front Right, Rear Ultrasound signal pins
char *pingString[] = {"a "," b ", " c ", " d "}; // just something to print to indicate direction
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long ultrasoundValue;
for(int i=0; i < 4; i++)
{
ultrasoundValue = ping(i);
Serial.print(pingString[i]);
Serial.print(ultrasoundValue);
delay(10);
}
Serial.println();
delay(10);
}
//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 / 58.138); //convert to CM
}
As I said, I tried many things to make it work faster (but it still skips a few readings):
- deleted char *pingString[] = {"a ","b ", "c ", "d "}; and anything related
- changed return (echo / 58.138); to return (echo); as to not convert to centimeters on the arduino and even deleted it
- decreased the two delays in the void loop (even lower than 10 each) and even deleted the two delays
Nothing seems to work!
Any solutions are truly appreciated!
ygreq