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!