problem with ping ultrasonic sensor code

Hi guys , I’m having a problem with the ultrasonic programming, the sample code thats on arduino site is working fine with me , but when i intgrated it with my code which is controlling RC car via wifi , the ultrasonic gives me results on the siral always 0 i don’t know why , what i want to detect the destance and if its more than 10 cm the car go forward , pleas can you help me with it ,
here is my code

/* 
* A simple sketch that uses WiServer to serve a web page 
*/

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1 
#define WIRELESS_MODE_ADHOC    2

// Wireless configuration parameters ?¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€?- 
unsigned char local_ip[]    = {192,168,0,1};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"test"};   // max 32 bytes
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};	// max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = { 
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
};

// setup the wireless mode 
// infrastructure ?¢â‚¬â€œ connect to AP 
// adhoc ?¢â‚¬â€œ connect to another WiFi device 
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;

unsigned char ssid_len; 
unsigned char security_passphrase_len; 
// End of wireless configuration parameters ?¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€?-
  
  
  

int forwardPin = 4; 
int backPin = 5; 
int rightPin = 6; 
int leftPin = 7; 

const int pingPin = 12; // ping pin


boolean mainpage()
{
            WiServer.print("<html><head></head>");


          WiServer.print("<body>");
           WiServer.print("
</font><font size=13> Remote control of the RC car </font>
");
          WiServer.print(   "<meta name = 'viewport' content = 'width = device-width'>");
    // WiServer.print(   "   <meta name='apple-mobile-web-app-status-bar-style' content='black' />");

          
                     
          WiServer.print("<table border= 0>");
          WiServer.print("<tr>");
          WiServer.print("<th>  </th>");
          WiServer.print("<th>");
          WiServer.print("<form  method=get>");
          WiServer.print("<input type=hidden name=V value=F />
");
//          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=UP>");
          WiServer.print("<input type=submit value=FORWARD>");
            WiServer.print("</form>");
              WiServer.print("</th><th> </th>");
          WiServer.print("</tr>");
          WiServer.print("<tr><th>");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=L />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=LEFT>"); */
          WiServer.print("<input type=submit value=LEFT>");
          WiServer.print("</form>");
          WiServer.print("</th><th>");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=S />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=STOP>"); */
          WiServer.print("<input type=submit value=STOP>");
          WiServer.print("</form>");
         WiServer.print("</th><th> ");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=R />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=RIGHT>"); */
          WiServer.print("<input type=submit value=RIGHT>");
          WiServer.print("</form>");
          WiServer.print("</th></tr> <tr> <th>  </th> <th> ");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=B />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=DOWN>"); */
          WiServer.print("<input type=submit value=BACKWARD>");
          WiServer.print("</form>");
          WiServer.print(" </th> <th> </th> </tr>");
          WiServer.print("</table>");
          WiServer.print("
");


          WiServer.print("</body>");
          WiServer.print("</html>");
    
          return true; 
}


boolean  controlpage(char* URL)
{
//    Serial.println(URL);
    if (strcmp(URL, "/") == 0)
    {
          mainpage();  
         return true;
    }
    else
    {
         if(URL[1] == '?')
         {
           if((URL[2] == 'V') && (URL[3] == '='))
           {
              switch(URL[4])
              {
                 case 'F': 
                 
                  if (cm >= 10)  digitalWrite(forwardPin, HIGH) ;


                        
                              
                             

                         //   forward(0, 0); 
                          break;
                 case 'B': 
                            digitalWrite(backPin, HIGH); 
                          // back(0, 0); 
                          break;
                 case 'L': 
                            digitalWrite(leftPin, HIGH); 
                          // left(0, 0); 
                          break;
                 case 'R': 
                   digitalWrite(rightPin, HIGH); 
                          // right(0, 0); 
                          break;
                 case 'S': 
                                                      digitalWrite(forwardPin, LOW); 
                                                                                 digitalWrite(backPin, LOW); 
                                                                                                            digitalWrite(rightPin, LOW); 
                                                                                                                                       digitalWrite(leftPin, LOW);  
                          break;
               
              }
             
           }
          mainpage();  
          return true;          
         }      


    }
}

void forward(int a, int b)
{
  
}
void back(int a, int b)
{
  
}
void left(int a, int b)
{
  
}
void right(int a, int b)
{
  
}

void motorstop(int a, int b)
{
  
}

void setup() { 
  // Initialize WiServer and have it use the sendMyPage function to serve pages 
  WiServer.init(controlpage); 
  // Enable Serial output and ask WiServer to generate log messages (optional) 
  Serial.begin(57600); 
  WiServer.enableVerboseMode(true); 
  pinMode(forwardPin, OUTPUT); 
  pinMode(backPin, OUTPUT); 
    pinMode(rightPin, OUTPUT); 
      pinMode(leftPin, OUTPUT); 

}

void loop(){
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:


  // 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);
   long duration, inches, cm;
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

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;
}
  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

thanks

Are you sure that's your code? I can't imagine that compiling.

const int pingPin = 12; // ping pin

Isn't your shield using pins 10 to 13 (the SPI pins)?

PaulS: const int pingPin = 12; // ping pin

Isn't your shield using pins 10 to 13 (the SPI pins)?

no , i have blackdimond so pin 12 is free , plus it was working with the sample code

AWOL: Are you sure that's your code? I can't imagine that compiling.

it compile fine without errors if that's your question

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;
}
  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

Really?

no , i have blackdimond so pin 12 is free

Post a link to your WiFi shield.

PaulS:

no , i have blackdimond so pin 12 is free

Post a link to your WiFi shield.

here it is http://bit.ly/vOO6DN

AWOL: 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; }   // Run WiServer   WiServer.server_task();   delay(10); }

Really?

what ? any idea ?

Tell me how that code compiles.

AWOL: Tell me how that code compiles.

compiles fine , it works and run , but the code not functioning well , Cm as the distance gives me 0 on the serial output

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;
}
// Run WiServer
WiServer.server_task();
delay(10);
}
The highlighted code is outside of a function, or the microsecondsToCentimeters function is nested inside of another function.

Neither of these conditions is allowed, so the code you posted can not possibly compile.

Period. End of story.

Either what you posted is not what you are compiling, or you are ignoring the error messages. Which is it?

PaulS:
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;
}
// Run WiServer
WiServer.server_task();
delay(10);
}

The highlighted code is outside of a function, or the microsecondsToCentimeters function is nested inside of another function.

Neither of these conditions is allowed, so the code you posted can not possibly compile.

Period. End of story.

Either what you posted is not what you are compiling, or you are ignoring the error messages. Which is it?

oh im sorry my bad , here its the code , now its compiling fine ,

/* 
* A simple sketch that uses WiServer to serve a web page 
*/

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1 
#define WIRELESS_MODE_ADHOC    2

// Wireless configuration parameters ?¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€??¢â‚¬â€?- 
unsigned char local_ip[]    = {192,168,0,1};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"test"};   // max 32 bytes
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};	// max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = { 
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
};


unsigned char wireless_mode = WIRELESS_MODE_ADHOC;

unsigned char ssid_len; 
unsigned char security_passphrase_len; 
  
  
  

int forwardPin = 4; 
int backPin = 5; 
int rightPin = 6; 
int leftPin = 7; 

const int pingPin = 12; // ping pin
   long duration, inches, cm;

boolean mainpage()
{
            WiServer.print("<html><head></head>");


          WiServer.print("<body>");
           WiServer.print("
</font><font size=13> Remote control of the RC car </font>
");
          WiServer.print(   "<meta name = 'viewport' content = 'width = device-width'>");

          
                     
          WiServer.print("<table border= 0>");
          WiServer.print("<tr>");
          WiServer.print("<th>  </th>");
          WiServer.print("<th>");
          WiServer.print("<form  method=get>");
          WiServer.print("<input type=hidden name=V value=F />
");
          WiServer.print("<input type=submit value=FORWARD>");
            WiServer.print("</form>");
              WiServer.print("</th><th> </th>");
          WiServer.print("</tr>");
          WiServer.print("<tr><th>");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=L />
");
          WiServer.print("<input type=submit value=LEFT>");
          WiServer.print("</form>");
          WiServer.print("</th><th>");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=S />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=STOP>"); */
          WiServer.print("<input type=submit value=STOP>");
          WiServer.print("</form>");
         WiServer.print("</th><th> ");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=R />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=RIGHT>"); */
          WiServer.print("<input type=submit value=RIGHT>");
          WiServer.print("</form>");
          WiServer.print("</th></tr> <tr> <th>  </th> <th> ");
          WiServer.print("<form  method=get >");
          WiServer.print("<input type=hidden name=V value=B />
");
/*          WiServer.print("<input type=submit style='height: 55px; width: 100px' value=DOWN>"); */
          WiServer.print("<input type=submit value=BACKWARD>");
          WiServer.print("</form>");
          WiServer.print(" </th> <th> </th> </tr>");
          WiServer.print("</table>");
          WiServer.print("
");


          WiServer.print("</body>");
          WiServer.print("</html>");
    
          return true; 
}


boolean  controlpage(char* URL)
{
//    Serial.println(URL);
    if (strcmp(URL, "/") == 0)
    {
          mainpage();  
         return true;
    }
    else
    {
         if(URL[1] == '?')
         {
           if((URL[2] == 'V') && (URL[3] == '='))
           {
              switch(URL[4])
              {
                 case 'F': 
                 
                  if (cm >= 10)  digitalWrite(forwardPin, HIGH) ;


                        
                              
                             

                         //   forward(0, 0); 
                          break;
                 case 'B': 
                            digitalWrite(backPin, HIGH); 
                          // back(0, 0); 
                          break;
                 case 'L': 
                            digitalWrite(leftPin, HIGH); 
                          // left(0, 0); 
                          break;
                 case 'R': 
                   digitalWrite(rightPin, HIGH); 
                          // right(0, 0); 
                          break;
                 case 'S': 
                                                      digitalWrite(forwardPin, LOW); 
                                                                                 digitalWrite(backPin, LOW); 
                                                                                                            digitalWrite(rightPin, LOW); 
                                                                                                                                       digitalWrite(leftPin, LOW);  
                          break;
               
              }
             
           }
          mainpage();  
          return true;          
         }      


    }
}

void forward(int a, int b)
{
  
}
void back(int a, int b)
{
  
}
void left(int a, int b)
{
  
}
void right(int a, int b)
{
  
}

void motorstop(int a, int b)
{
  
}

void setup() { 
  // Initialize WiServer and have it use the sendMyPage function to serve pages 
  WiServer.init(controlpage); 
  // Enable Serial output and ask WiServer to generate log messages (optional) 
  Serial.begin(57600); 
  WiServer.enableVerboseMode(true); 
  pinMode(forwardPin, OUTPUT); 
  pinMode(backPin, OUTPUT); 
    pinMode(rightPin, OUTPUT); 
      pinMode(leftPin, OUTPUT); 
      
      }

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

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;
}



void loop(){
{
    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
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}


  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

oh im sorry my bad , here its the code , now its compiling fine ,

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}


  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

No, it's the same crap you posted before that won't compile.

Psst, check “void loop”.
It isn’t pretty, but it works.
(At least it works for the compiler)

PaulS:

oh im sorry my bad , here its the code , now its compiling fine ,

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

  // Run WiServer   WiServer.server_task();   delay(10); }



No, it's the same crap you posted before that won't compile.

if its crap as u say tell me what to do ?, plus i compile it b4 i post it here

AWOL: Psst, check "void loop". It isn't pretty, but it works. (At least it works for the compiler)

what should i do , its on the example on the arduino site

No, my comment was directed at PaulS.

No, my comment was directed at PaulS.

I'm just about ready to quit trying to help people that can't put the { on a new line where it belongs, and can't properly indent code.