Read up on functions and parameter passing: (an example)
Adding "int face" inside the parenthesis of the cycle function definition signals that the function should expect a parameter named face, with a type of int. The function than can reference that parameter as face in the function body.
When you call the function, you put a parameter value inside the parenthesis:
cycle(0) will result in face having the value 0
cycle(100) will result in face having the value of 100
cycle(frontFace) will result in face having the current value of frontFace
cycle(rightFace) will result in face having the current value of rightFace
You can think of it as declaring a local variable in the function, and initializing that variable with the value that was passed in when the function is called. The key is that the initial value of the "face" variable will change at run time according to how it was called.
ShapeShifter:
Read up on functions and parameter passing: (an example)
Adding "int face" inside the parenthesis of the cycle function definition signals that the function should expect a parameter named face, with a type of int. The function than can reference that parameter as face in the function body.
When you call the function, you put a parameter value inside the parenthesis:
cycle(0) will result in face having the value 0
cycle(100) will result in face having the value of 100
cycle(frontFace) will result in face having the current value of frontFace
cycle(rightFace) will result in face having the current value of rightFace
You can think of it as declaring a local variable in the function, and initializing that variable with the value that was passed in when the function is called. The key is that the initial value of the "face" variable will change at run time according to how it was called.
Noted, so when the function is called, it will result in cmFace being whatever the function returns?
AWOL:
You'd need to consider a delay after the servo write to allow the servo to reach the position written.
ShapeShifter:
Read up on functions and parameter passing: (an example)
Adding "int face" inside the parenthesis of the cycle function definition signals that the function should expect a parameter named face, with a type of int. The function than can reference that parameter as face in the function body.
When you call the function, you put a parameter value inside the parenthesis:
cycle(0) will result in face having the value 0
cycle(100) will result in face having the value of 100
cycle(frontFace) will result in face having the current value of frontFace
cycle(rightFace) will result in face having the current value of rightFace
You can think of it as declaring a local variable in the function, and initializing that variable with the value that was passed in when the function is called. The key is that the initial value of the "face" variable will change at run time according to how it was called.
So the code should look like this now, right?
#include <Servo.h>
Servo servo;
#define trigPin 3; // Trig Pin (Digital)
#define echoPin 2; // Echo Pin (Digital)
int alertRange = 30; // alert rainge in cm
// #define maximumRange 10000;
// #define minimumRange 0;
long duration, cmFront, cmLeft, cmRight; // Duration used to calculate distance
void setup()
{
servo.attach(9);
// Test the servo before begining
servoTest();
Serial.begin(9600);
}
void loop()
{
long duration, cmFront, cmLeft, cmRight; // Duration used to calculate distance
int frontFace = 85; // The Servo's front facing angle
int rightFace = 40; // The Servo's right facing angle
int leftFace = 130; // The Servo's left facing angle
cmFront = cycle(frontFace);
cmLeft = cycle(leftFace);
cmRight = cycle(rightFace);
Serial.print("Front: ");
Serial.print(cmFront);
Serial.print("Left: ");
Serial.print(cmLeft);
Serial.print("Right: ");
Serial.println(cmRight);
}
void servoTest()
{
servo.write(frontFace);
delay(1000);
servo.write(rightFace);
delay(1000);
servo.write(frontFace);
delay(1000);
servo.write(leftFace);
delay(1000);
servo.write(frontFace);
delay(1000);
servo.write(0);
delay(1000);
servo.write(180);
delay(1000);
}
long cycle(int face)
{
servo.write(face);
delay(100);
// Trig/Echo cycle
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return microsecondsToCentimeters(duration);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Yes, except you've got duplicated global and locals (variables with the same name).
This isn't a problem if you know what you're doing, but is confusing if you don't.