Pages: [1]   Go Down
Author Topic: Creating a function, needs some help  (Read 773 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12861
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


int measureDistance( int USside )
{
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

In your variables area you have this but, no assignment. I believe the default is 0.
Code:
int USside;
Logged


New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3684
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You will need a return statement in your function:
Code:
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.
« Last Edit: July 09, 2012, 10:23:05 am by wildbill » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


int measureDistance( int USside )
{


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:
pinMode(USside, OUTPUT);
 digitalWrite(USside, LOW);

In your variables area you have this but, no assignment. I believe the default is 0.
Code:
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 - downloaded 10 times.)
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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.

Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


  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:
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:
int frontUSpin=2; //pin assignment



 


 
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: