troubleshooting the ping))) sensor

Hi, I'm very new to this arduino project. I used mjmcondor's code to interfaced the Ping))) sensors,http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236272388.

int ultraSoundSignalPins[] = {2,3,4,5}; // Left,Front, Front Right, Rear Ultrasound signal pins
int farthest_Dist=ping(0);
int pinFWD=7;
int pinREAR=8;
int pinLEFT=9;
int pinRIGHT=10;[/i]

[i]void setup() {
  Serial.begin(9600);
  pinMode(pinFWD,OUTPUT);
  pinMode(pinREAR,OUTPUT);
  pinMode(pinLEFT,OUTPUT);
  pinMode(pinRIGHT,OUTPUT);[/i]

[i]unsigned long ultrasoundValue;
  for(int i=0; i < 4; i++)
  {
    ultrasoundValue = ping(i);
    Serial.print(ultrasoundValue);
    Serial.print("in, ");    
    Serial.println();
    
  }
  {
      {

    
    
     if (ping(0)==ping(1)==ping(2)==ping(3))
   {
     digitalWrite(pinLEFT,LOW);
     digitalWrite(pinFWD,LOW);
     digitalWrite(pinRIGHT,LOW);
     digitalWrite(pinREAR,LOW);
   }
   else if (ping(2)>ping(0))
     {
       farthest_Dist=ping(2);
       digitalWrite(pinRIGHT,HIGH);
         digitalWrite(pinLEFT,LOW);
     digitalWrite(pinFWD,LOW);
     digitalWrite(pinREAR,LOW);
     }
     else if (ping(3) > ping(0))
     {
       farthest_Dist=ping(3);
       digitalWrite(pinREAR,HIGH);
         digitalWrite(pinLEFT,LOW);
     digitalWrite(pinFWD,LOW);
     digitalWrite(pinRIGHT,LOW);
   }
     else if (ping(1) > ping(0))
     {
       farthest_Dist=ping(1);
       digitalWrite(pinFWD,HIGH);
       digitalWrite(pinLEFT,LOW);
       digitalWrite(pinRIGHT,LOW);
       digitalWrite(pinREAR,LOW);
     }
     else
     {
       farthest_Dist = ping(0);
       digitalWrite(pinLEFT,HIGH);
     digitalWrite(pinFWD,LOW);
     digitalWrite(pinRIGHT,LOW);
     digitalWrite(pinREAR,LOW);
   }

Serial.print (farthest_Dist);
    Serial.println();
    }
  }
  
}



//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) * .39; //convert to CM then to inches

However, I got the following errors.

AeroQuad2.cpp: In function 'long unsigned int ping(int)':
Eeprom:24: error: a function-definition is not allowed here before '{' token
float readFloat(int address) 
{
  union floatStore {
    byte floatByte[4];
    float floatVal;
  } floatOut;
  
  for (int i = 0; i < 4; i++) 
    floatOut.floatByte[i] = EEPROM.read(address + i);
  return floatOut.floatVal;
}

Can someone tell me where I went wrong?

Thanks.

Strange. I copied the the second of your code snippets into one of my .pde files and it works without a problem. I just had to #include <EEPROM.h>, but I guess you do that too.

Korman

The second code works fine on its own. However, after adding the first code into the whole program then the errors come about. Is there something else which i have not done?

Can someone tell me where I went wrong?

Using "crap" code?

Here's the whole code after I cleaned it up (there were like missing braces, out of place braces, a missing loop() definition, etc):

#include <EEPROM.h>

int ultraSoundSignalPins[] = {2,3,4,5}; // Left,Front, Front Right, Rear Ultrasound signal pins
int farthest_Dist=ping(0);
int pinFWD=7;
int pinREAR=8;
int pinLEFT=9;
int pinRIGHT=10;

void setup() {
  Serial.begin(9600);
  pinMode(pinFWD,OUTPUT);
  pinMode(pinREAR,OUTPUT);
  pinMode(pinLEFT,OUTPUT);
  pinMode(pinRIGHT,OUTPUT);

  unsigned long ultrasoundValue;
  
  for(int i=0; i < 4; i++) {
    ultrasoundValue = ping(i);
    Serial.print(ultrasoundValue);
    Serial.print("in, ");    
    Serial.println();    
  }
    
  if (ping(0)==ping(1)==ping(2)==ping(3)) {
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else if (ping(2)>ping(0)) {
    farthest_Dist=ping(2);
    digitalWrite(pinRIGHT,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else if (ping(3) > ping(0)) {
    farthest_Dist=ping(3);
    digitalWrite(pinREAR,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
  }
  else if (ping(1) > ping(0)) {
    farthest_Dist=ping(1);
    digitalWrite(pinFWD,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else {
    farthest_Dist = ping(0);
    digitalWrite(pinLEFT,HIGH);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }

  Serial.print (farthest_Dist);
  Serial.println();
}

void loop() {
}

//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) * .39; //convert to CM then to inches 
}

float readFloat(int address)
{
  union floatStore {
    byte floatByte[4];
    float floatVal;
  } floatOut;
  
  for (int i = 0; i < 4; i++) {
    floatOut.floatByte[i] = EEPROM.read(address + i);
  }
  
  return floatOut.floatVal;
}

This seems to compile fine under 0019; I don't know where the extra characters came from, or why stuff was missing - but as a case of "copypasta programming" it left a lot to be desired. Also note that I cannot vouch that the code above will run as expected (I am curious why all the code is in setup, and nothing in loop()?) - just that this compiles without error...

Hmm - looking at the code from your first link, here's what the code should -probably- look like (ignore the code above):

#include <EEPROM.h>

int ultraSoundSignalPins[] = {2,3,4,5}; // Left,Front, Front Right, Rear Ultrasound signal pins
int farthest_Dist=ping(0);
int pinFWD=7;
int pinREAR=8;
int pinLEFT=9;
int pinRIGHT=10;

void setup() {
  Serial.begin(9600);
  pinMode(pinFWD,OUTPUT);
  pinMode(pinREAR,OUTPUT);
  pinMode(pinLEFT,OUTPUT);
  pinMode(pinRIGHT,OUTPUT);
}

void loop() {

  unsigned long ultrasoundValue;
  
  for(int i=0; i < 4; i++) {
    ultrasoundValue = ping(i);
    Serial.print(ultrasoundValue);
    Serial.print("in, ");    
    Serial.println();    
  }
    
  if (ping(0)==ping(1)==ping(2)==ping(3)) {
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else if (ping(2)>ping(0)) {
    farthest_Dist=ping(2);
    digitalWrite(pinRIGHT,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else if (ping(3) > ping(0)) {
    farthest_Dist=ping(3);
    digitalWrite(pinREAR,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
  }
  else if (ping(1) > ping(0)) {
    farthest_Dist=ping(1);
    digitalWrite(pinFWD,HIGH);
    digitalWrite(pinLEFT,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }
  else {
    farthest_Dist = ping(0);
    digitalWrite(pinLEFT,HIGH);
    digitalWrite(pinFWD,LOW);
    digitalWrite(pinRIGHT,LOW);
    digitalWrite(pinREAR,LOW);
  }

  Serial.print (farthest_Dist);
  Serial.println();
}

//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) * .39; //convert to CM then to inches 
}

float readFloat(int address)
{
  union floatStore {
    byte floatByte[4];
    float floatVal;
  } floatOut;
  
  for (int i = 0; i < 4; i++) {
    floatOut.floatByte[i] = EEPROM.read(address + i);
  }
  
  return floatOut.floatVal;
}

Once again - pure guesswork, but it does compile...

Something else -all- Arduino newbies should note: Do you notice something different between the code I posted and the OP's post?

Not to rag on the OP, but to point out a truism: Structured code is maintainable code.

Keep the structure of your code consistent (as you can see, I am partial to K&R styling); keep your statements aligned, orderly, and neat - and you will be able to spot errors (before and after compiling) much more quickly than you would otherwise. Proper structure also promotes readability.

If you copy-n-paste code examples that aren't structured - structure them after you paste them; for all you know, the author left in a bug or inadvertently caused one when they pasted it on their site (as I suspect is what happened to the OP here, when he cut-n-pasted from the example in the other thread). By doing so, you may spot an issue before it becomes a problem (that doesn't necessarily mean you'll be able to fix it, but it may help after the compiler throws a flag).

:slight_smile:

I am actually building an aeroquad to avoid obstacles from 4 sides using the PING ultrasonic sensors hence the above codes. I got the main codes from:
http://code.google.com/p/aeroquad/downloads/detail?name=AeroQuad_v1.7.1.zip&can=2&q=

However, the source codes i got from aeroquad seems a little complicating for a newbie like me. Is it possible to enlighten me on how to interface the sensors if i am aiming to make the quad move away from the object of concern?

That code is pretty basic, albeit not very pretty. What's readFloat used for? It's never called from what I can see. Also what's the deal with the sensor pins in an array? The array makes the rest of the code less readable - self-documenting by use of sensible variable names helps immensely

A few minor changes (and comments!) in the code would go along way to unfolding its mysteries... this compiles but I can't vouch for the functionality.

int farthest_Dist = 0;

// Left, Front, Right, Rear Ultrasound signal pins
int pinLEFT_SENSOR = 2;
int pinFRONT_SENSOR = 3;
int pinRIGHT_SENSOR = 4;
int pinREAR_SENSOR = 5;

// I'm guessing these are pins for 4 motors...
int pinFWD = 7;
int pinREAR = 8;
int pinLEFT = 9;
int pinRIGHT = 10;

void setup()
{
  Serial.begin( 9600 );
  pinMode( pinFWD, OUTPUT );
  pinMode( pinREAR, OUTPUT );
  pinMode( pinLEFT, OUTPUT );
  pinMode( pinRIGHT, OUTPUT );
}

void loop()
{
  unsigned long left, front, right, rear;

  // retrieve the distance from each sensor - does this take into account the round-trip nature of the measurement?
  left = ping( pinLEFT_SENSOR );
  front = ping( pinFRONT_SENSOR );
  right = ping( pinRIGHT_SENSOR );
  rear = ping( pinREAR_SENSOR );

  // display the retrieved distances
  Serial.print( left );
  Serial.println( "in, " );
  Serial.print( front );
  Serial.println( "in, " );
  Serial.print( right );
  Serial.println( "in, " );
  Serial.print( rear );
  Serial.println( "in, " );

  // determine which way to head based on the relative distances recorded
  if ( left == front == right == rear )
  {
    // equal distances all around - stay put
    digitalWrite( pinLEFT, LOW );
    digitalWrite( pinFWD, LOW );
    digitalWrite( pinRIGHT, LOW );
    digitalWrite( pinREAR, LOW );
  }
  else if ( right > left )
  {
    // more space to the right - head that way
    farthest_Dist = right;
    digitalWrite( pinRIGHT, HIGH );
    digitalWrite( pinLEFT, LOW );
    digitalWrite( pinFWD, LOW );
    digitalWrite( pinREAR, LOW );
  }
  else if ( rear > left )
  {
    // more space to the rear - head that way
    farthest_Dist = rear;
    digitalWrite( pinREAR, HIGH );
    digitalWrite( pinLEFT, LOW );
    digitalWrite( pinFWD, LOW );
    digitalWrite( pinRIGHT, LOW );
  }
  else if ( front > left )
  {
    // more space to the front - head that way
    farthest_Dist = front;
    digitalWrite( pinFWD, HIGH );
    digitalWrite( pinLEFT, LOW );
    digitalWrite( pinRIGHT, LOW );
    digitalWrite( pinREAR, LOW );
  }
  else
  {
    // left it is then
    farthest_Dist = left;
    digitalWrite( pinLEFT, HIGH );
    digitalWrite( pinFWD, LOW );
    digitalWrite( pinRIGHT, LOW );
    digitalWrite( pinREAR, LOW );
  }

  // display the largest distance just measured
  Serial.println( farthest_Dist );
}

// Ping function
unsigned long ping( int pin )
{
  unsigned long echo;

  pinMode( pin, OUTPUT ); // Switch signalpin to output
  digitalWrite( pin, LOW ); // Send low pulse
  delayMicroseconds( 2 ); // Wait for 2 microseconds
  digitalWrite( pin, HIGH ); // Send high pulse
  delayMicroseconds( 5 ); // Wait for 5 microseconds
  digitalWrite( pin, LOW ); // Holdoff
  pinMode( pin, INPUT ); // Switch signalpin to input
  digitalWrite( pin, HIGH ); // Turn on pullup resistor
  echo = pulseIn( pin, HIGH ); //Listen for echo
  return ( echo / 58.138 ) * .39; //convert to CM then to inches
}

Just my $0.02 worth...

G.