Please help me declare my functions properly.

Hi Everyone,

This is basically a sketch of a sketch. This is my first attempt to control a servo using a ping sensor. I would like to keep things very neat. At the beginning I declared integers for the following commands: forward, backward, left, and right. This is the main problem I have. I feel if I could write sub programs for these commands using “function” i.e. void forward() I could have a nice neat setup and clearly see my full program as a bunch of smaller programs working together. If I just type:

void forward(){

“stuff here”

}

I get errors. So how do I declare my functions properly ? My idea code would be as follows:

void setup()
{


}

void loop()
{


void forward()
{


}

// etc. I hope I explained this well. Thank you for your time.

}

#include <Servo.h> 
Servo myservo;  
 
int pos = 90;     
int brake = 90; 

int forward =0;
int backward = 0;
int right = 0;
int left = 0;


const int pingPing = 7;



void setup() 
{ 
  myservo.attach(4);   
  Serial.begin(9600);
  pinMode(pingPing, INPUT);
} 
 
 
void loop() 
{ 

  
myservo.write(180);            


long duration, inches;

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

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

 
  inches = microsecondsToInches(duration);

 Serial.print(inches);
 Serial.println();
 delay(50);



if (inches < 30) {myservo.write(0), delay(2500), myservo.write(90), delay(1500);}

else {myservo.write(90);}



}




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

Hard to help without seeing the errors or the code that produced them, but it sounds like you may have declared a function called forward when you already have an int called forward. If so, the compiler certainly won't like that. Try calling the function MoveForward and see if that helps. If not, post your code.

cheekid: void loop()

{


void forward()

You're missing a closing brace; that's probably why you're getting an error.

Because you didn't post the error, nor did you post the code that could reproduce the error, it's hard to say if that is truly the case.

Hi,

If i create a function:

void move forward()

{



}

what do i do to make this work? The error i get is: a function-definition is not allowed here before "{" token

You can't have spaces in function (or variable) names. Try this:

void moveforward()

Pete

cheekid: ``` void move forward()

{



} ```

A row of hyphens is not valid 'C'.

If this is a placeholder then put a 'C' comment here. For example:

void moveForward()
{
    // not implemented yet
}

cheekid: Hi,

If i create a function:

void move forward()

{



}

what do i do to make this work? The error i get is: a function-definition is not allowed here before "{" token

As mentioned, you need a different name, but you would need to make sure that that code is outside of any other function. If you're still getting an error, you need to post the full code that you are trying to compile.

Hi,

Thank you everyone. I will continue until I get it right. I’ll give it a few more tries. Take care everyone.

Hi,

The program will not allow me to write this:

void moveforward()
{
myservo.write(180);
if (inches<30) {myservo.write(0),delay(1500);}
else{myservo.write(90);
}

It would be a lot easier to help you if you actually posted the full code, not just the part that doesn't work. You also should avoid saying things like "this doesn't work" or "it won't let me do it." If you're getting error messages, post the error messages. If it's not doing what it's supposed to, post what it's doing and what you want it to do.

My best guess is that inches is not a global variable, nor is it declared in moveforward().

 if (inches < 30) {myservo.write(0), delay(1500);}

needs a semicolon rather than a comma. You should also only have one command per line except in special cases:

  if (inches < 30) {
    myservo.write(0);
    delay(1500);
  }