ultrasonic wall detector

I'm making a robot that can sense walls and navigate around them for a project, but am having problems with the code. can anyone give me some advice?

This is my current code:

#include <Servo.h>

const int pingPin = 7;
int pos = 1;
Servo myservo;
Servo myservo2;

void setup() {

Serial.begin(9600);
myservo.attach(9);
myservo2.attach(8);
}

void loop()
{
long duration, inches, cm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);

for(pos = 0; pos < 180; cm > 100; pos += 1)
{
myservo.write(pos);
delay(100);
}
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

If anyone can help, i would greatly appreciate it!

but am having problems with the code

And they are...?

for(pos = 0; pos < 180; cm > 100; pos += 1)

What do you want this to do?

When posting code, please use the # (code) button on the editor's toolbar

#include <Servo.h>


const int pingPin = 7;
int pos = 1;
Servo myservo;
Servo myservo2;

void setup() {

 Serial.begin(9600);
 myservo.attach(9);
 myservo2.attach(8);
}

void loop()
{
 long duration, inches, cm;

 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);


 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);

 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(100);
 
 for(pos = 0; pos < 180; cm > 100; pos += 1)
 {
   myservo.write(pos);            
   delay(100);
 }
}

long microsecondsToInches(long microseconds)
{
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}

If anyone can help, i would greatly appreciate it!

for(pos = 0; pos < 180; cm > 100; pos += 1)

What do you want this to do?

it's to check if the position of the motor is 0, less than 180, and the wall is less than 1 m away and then increase the position by 1

Didi you mean:
; pos < 180 && cm > 100;?

You can't just invent arbitrary syntax with C.

ohh, well i'll test and see

Very nice. Works like a charm!
Thanks!

Think of a "for" loop as a "while":

for (; ; <end_loop>) {
<loop_body>
}

translates to

while () { }

}

Ok, so now i recently decided, that because servos could only have a maximum rotation of 180 degress, i decided to use a regular hobby motor with the code and replace the servo code with "digitalwrite". i am having problems yet again and am getting the error
"a function-definition is not allowed here before a { token"

On my versions with the servo, they worked (with a little help from the person above), but now they're showing up again.

Can anyone help with this?

oh and this is my new code

const int pingPin = 7;
int pos = 1;
int eng1 = 13; //sets up motors pins
int eng2 = 12;


void setup() {

 Serial.begin(9600);
 pinMode(eng1, OUTPUT);  // sets motors to output
 pinMode(eng2, OUTPUT);  
}

void loop()
{
 long duration, inches, cm;

 pinMode(pingPin, OUTPUT); //sends a pulse
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);


 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);

 Serial.print(inches);
 Serial.print("in, ");// gets the distance in inches and cm
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();

 delay(100);

 if(cm < 50)
 {
  digitalWrite(eng1, HIGH);   // turn for 1 second
  delay(250);                  
  digitalWrite(eng1, LOW);    // set the engine off
 }
 else
 {
  digitalWrite(eng2, HIGH);
  digitalWrite(eng1, HIGH);  // run engines
  delay(2000);                  // wait 2 seconds
  digitalWrite(eng2, LOW);
  digitalWrite(eng2, LOW);  // set the engine off 
}



long microsecondsToInches(long microseconds) // error here
{
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}
 else
 {
  digitalWrite(eng2, HIGH);
  digitalWrite(eng1, HIGH);  // run engines
  delay(2000);                  // wait 2 seconds
  digitalWrite(eng2, LOW);
  digitalWrite(eng2, LOW);  // set the engine off
} //No, error here

I havent ever seen a error like this before. How do you fix it, anyways?

oh, nvmd. I found the solution. I forgot a second curly brace to end the statement. Thanks for helping me find it.