Go Down

Topic: Creating a function, needs some help (Read 864 times) previous topic - next topic

eovnu87435ds

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

Quote

//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:

Code: [Select]
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!

Coding Badly


[font=Courier New]int measureDistance( int USside )
{[/font]

cyclegadget


  These statements require a pin number:
Code: [Select]
pinMode(USside, OUTPUT);
digitalWrite(USside, LOW);


In your variables area you have this but, no assignment. I believe the default is 0.
Code: [Select]
int USside;
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

wildbill

#3
Apr 24, 2012, 07:43 pm Last Edit: Jul 09, 2012, 05:23 pm by wildbill Reason: 1
You will need a return statement in your function:
Code: [Select]

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.

eovnu87435ds



[font=Courier New]int measureDistance( int USside )
{[/font]


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.


 These statements require a pin number:
Code: [Select]
pinMode(USside, OUTPUT);
digitalWrite(USside, LOW);


In your variables area you have this but, no assignment. I believe the default is 0.
Code: [Select]
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.

Arrch

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

Code: [Select]
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:

Code: [Select]
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.


cyclegadget


  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:
Code: [Select]
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.
Code: [Select]
int frontUSpin=2; //pin assignment



 


 
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

eovnu87435ds

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!

Quote

//#############################################################################################
//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);
}


Go Up