Creating a function, needs some help

hey everybody! I am building an arduino based robot, and need to collect data from 3 Ultrasonic Sensors(parallax ping to be specific), and am trying to get a measureDistance function to work. The way I would like to do so is to be able to just have measureDistance(leftUSpin) to return the value of the left US sensor, measureDistance(rightUSpin) to return the right, etc.
The problem is, that I cannot get the actual function part to work. The rest of the code, when run outside of the function, works perfectly.
Here are the important bits of the code

//Ultrasonic Variables
int frontUSpin=2; //pin assignment
int frontUSdist; //distance var in cm
int leftUSpin=3;
int leftUSdist;
int rightUSpin=4;
int rightUSdist;
int USside;
int USdist;
unsigned long pulseduration=0;

void setup()
{

Serial.begin(9600);
}

int measureDistance(USside)
{
pinMode(USside, OUTPUT);
digitalWrite(USside, LOW);
delayMicroseconds(5);
digitalWrite(USside, HIGH);
delayMicroseconds(5);
digitalWrite(USside, LOW);
pinMode(USside, INPUT);
pulseduration=pulseIn(USside, HIGH);
pulseduration=pulseduration/2;
USdist = int(pulseduration/29);
}

void loop()
{
Serial.print("Distance - ");
Serial.print(measureDistance(leftUSpin));
Serial.println(" cm");
delay(500);
}

Also, here is the error output:

ffrobot:10: error: 'USside' was not declared in this scope
ffrobot:36: error: redefinition of 'int measureDistance'
ffrobot:10: error: 'int measureDistance' previously defined here
ffrobot.cpp: In function 'void loop()':
ffrobot:67: error: 'measureDistance' cannot be used as a function

Any help would be greatly appreciated!

int measureDistance( int USside )
{

These statements require a pin number:

 pinMode(USside, OUTPUT);
 digitalWrite(USside, LOW);

In your variables area you have this but, no assignment. I believe the default is 0.

int USside;

You will need a return statement in your function:

return USside;

Consider making USside a local variable in the function too - it's generally not good practice to touch globals in a function if you don't have to.

i thought that maybe I didn't need that, since USside was already declared before everything, but I tried it anyways and still got the same errors. I also uploaded the actual file as an attachment to this post, if it helps.

cyclegadget:
These statements require a pin number:

 pinMode(USside, OUTPUT);

digitalWrite(USside, LOW);




In your variables area you have this but, no assignment. I believe the default is 0. 


int USside;

Isn't USside just a variable? such that if I have measureDistance(leftUSpin); then USside = leftUSpin = 3? I am really not sure, but that is what I want to be able to do.

ffrobot.ino (2.03 KB)

There is two ways that you are mixing when using a function call. The first method is something like this:

int GetSomeValue(int data) {
  int value;
  // value= something based on data
  return value;
}

To run this, in the loop you would do something like this:

int myData = 2;
int myValue;
myValue = GetSomeValue(myData);

int myData2 = 3;
int myValue2;
myValue2 = GetSomeValue(myData2);

Notice the differences in variable names and how the function is independent of what you do outside it. This allows it to be used with different values. All it cares about is returning a value based on the data it's sent.

The other method is no return types and no arguments passed. This doesn't allow you to use the function with different variables and is more about "organizing the code".

It sounds like you're looking at the first option more than the last.

I am not sure what you need to do with your pins to work with attached sensors so i would need to know more to give you advice about setting up your pins in the setup() area. At the moment, the way your sketch is now, I am not sure what you want your pins to do or which ones you plan to use.

To be more specific, this part is not clear to me:

int USside;
int USdist;

Are these names of pins, or variables to store int values in?

With this part of the code it is clear that frontUSpin is used as a name for pin 2. It is easy to understand especially with the comment added to it.

int frontUSpin=2; //pin assignment

I got it! Thanks everybody! I kept putting the return at the top of the function definition, and when I put it at the bottom and had it return USdist, a variable for the distance calculated by the function, it works!

//#############################################################################################
//Initial Variables/Constants
//#############################################################################################

//Ultrasonic Variables
int frontUSpin=2; //pin assignment
int frontUSdist; //distance var in cm
int leftUSpin=3;//pin assignment
int leftUSdist;//distance var in cm
int rightUSpin=4;//pin assignment
int rightUSdist;//distance var in cm

int USdist;//distance var for measureDistance function
unsigned long pulseduration=0;//used in calculating distance

//#############################################################################################
//Setup
//#############################################################################################

void setup()
{

Serial.begin(9600);
}

//#############################################################################################
//Measure Ultrasonic Distance
//#############################################################################################
int measureDistance(int USside) //I want to be able to put measureDistance(frontUSpin) or measureDistance(leftUSpin) or measureDistance(rightUSpin)
//and have it give me the distance by replacing the var USside with one of the 3 vars I put in
{

// set pin as output so we can send a pulse
pinMode(USside, OUTPUT);
// set output to LOW
digitalWrite(USside, LOW);
delayMicroseconds(5);

// now send the 5uS pulse out to activate Ping)))
digitalWrite(USside, HIGH);
delayMicroseconds(5);
digitalWrite(USside, LOW);

// now we need to change the digital pin
// to input to read the incoming pulse
pinMode(USside, INPUT);

// finally, measure the length of the incoming pulse
pulseduration=pulseIn(USside, HIGH);
// divide the pulse length by half
pulseduration=pulseduration/2;

// now convert to centimetres. We're metric here people...
USdist = int(pulseduration/29);
return USdist;
}

//#############################################################################################
//Loop Code
//#############################################################################################
void loop()
{

//The following should be a test, where it should print the number of cm the distance is for the leftUSpin ultrasonic sensor
Serial.print("Distance - ");
Serial.print(measureDistance(leftUSpin));
Serial.println(" cm");
delay(500);
}