Ok, here is the problem.

I am in the process of writing my code for a roving robot. I am having issues with creating functions within my code.
My goal is to have functions for different actions I would like the robot to perform. I need to get one working correctly then I know I won’t have a problem creating additional functions.

myservoA // this servo keeps stopping and starting. I assume my code has set pingPing equal to 7.
// If this is the case how do I create a function based on the data collected from pin 7.
// I have enclosed a copy of my current test code:

#include <Servo.h> 
Servo myservoA;  
Servo myservoB;

int pos = 90;     
int brake = 90; 

const int pingPing = 7;



void setup() 
{ 
  myservoA.attach(4);   
  myservoB.attach(3);
  Serial.begin(9600);
  pinMode(pingPing, INPUT);

} 


void loop() 
{ 

  myservoA.write(180);
  myservoB.write(0);            



  long duration, inches;

  pinMode(pingPing, OUTPUT);
  digitalWrite(pingPing, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPing, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPing, LOW);

  pinMode(pingPing, INPUT);
  duration = pulseIn(pingPing, HIGH);

  inches = microsecondsToInches(duration);

  Serial.print(inches);
  Serial.println();
  delay(50);

  robotRight();

}



void robotRight()
{ 
  int rightCheck = digitalRead(pingPing);
  if (pingPing < 10) { 
    delay(500); 
    myservoA.write(90);
   delay(2000);
} 
  else{
    myservoA.write(0);

  }

}




long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

I am having issues with creating functions within my code.

What sort of issues are these?
That code compiles.

Hi,

The issue is that the code is not written correctly. I want to create a function to control the servo base upon the
data received from the Ping sensor. I'm not sure how to refer to the Ping sensor. I guess I can not address it by the pin. I tried
and it did not work. Thanks.

The value returned by your sensor is called duration. You then convert that to a number in a variable called inches. What you you want the servo to do with that value?
Servos work by turning through an angle, this is normally in degrees. I can’t see what you are trying to do. You can’t get a servo to move a number of inches.

Hi,

I can control the direction of the servo and stop the servo based upon what value is read by the Ping sensor. I have it written
down somewhere but I believe it is something like value 0 and 180 will rotate one direction, 270 will reverse that direction and 90 will stop the servo.

Could someone tell me how to address the servo within a function ? Thanks, I've been at this for awhile so far no luck.

Could someone tell me how to address the servo within a function ?

void loop() 
{ 

  myservoA.write(180);
  myservoB.write(0);

"loop" is a function.
You seem to be addressing both servos adequately within it.

Your function robotRight() sends the value of 90 to the servo correctly. However within the context of the whole code if the if statement is not met it will be overridden immediately by the servo functions in the loop function.

By the way, what you describe is no longer a real servo, it sounds like it has been modified for continuous rotation and all the angle does is define the direction.

Hi,

I am unable to address ping sensor using "duration". I guess something is incorrect. I'm not going to give up. Thank you all for the help. I will check out my "if statement" to verify if the conditions are written correctly.

I am unable to address ping sensor using “duration”

“duration” is the length of the pulse returned by the sonar, so I’m afraid I don’t understand what you are trying to do.

 int rightCheck = digitalRead(pingPing);
  if (pingPing < 10) {

Whether digitalRead returns HIGH or LOW, both will always be less than 10.

Edit: Misread: dxw00d is correct.
There are plenty of examples for the Ping, where a function is written that returns the range, for a Ping on a given pin.

...
const int pingPing = 7;
...
  if (pingPing < 10) { 
...

Seven is always going to be less than ten.

This bit reads the ping sensor:

  long duration, inches;

  pinMode(pingPing, OUTPUT);
  digitalWrite(pingPing, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPing, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPing, LOW);

  pinMode(pingPing, INPUT);
  duration = pulseIn(pingPing, HIGH);

  inches = microsecondsToInches(duration);

It gives you two results, duration and inches. I suggest you modify your robotRight() function to take one of them as a parameter (inches is probably easier) then use that to determine what to do with the servo.

unsigned long pingRange (byte pingPin)
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}

In response to the PM that the OP sent me, saying:

My ping sensor is located on pin number 7. I don’t know how to address the sensor other than referring to the pin it is connected to. This is where my issue is. This is the only problem I have. This is why I am having trouble creating a proper function.

First, look on the playground - http://arduino.cc/playground/Main/InterfacingWithHardware#DistanceSensing - which has a lot of information, including a new library for using ping sensors.

The ping sensors don’t provide a output that can be read directly. They are called ping sensors because you send out a ping, and see how long it takes for the echo to come back, just like a sonar. That is what this code does:

  long duration, inches;

// Set the pin for output
  pinMode(pingPing, OUTPUT);

// Send out a ping
  digitalWrite(pingPing, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPing, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPing, LOW);

// Set the pin for input
  pinMode(pingPing, INPUT);

// Wait for the echo to return, store the result in duration.
  duration = pulseIn(pingPing, HIGH);

See http://arduino.cc/en/Reference/PulseIn for details on pulseIn().

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

This function takes the duration passed to it, and returns a number that represents the distance, in inches, between the sensor, and whatever the ping reflected back from.

AWOL, in the previous post, has provided a function that you can add to your sketch, to replace the inline ping code. If you add it, you can incorporate it into your robotRight() function, something like:

void robotRight()
{ 
  if (microsecondsToInches(pingRange(pingPing)) < 10)
  { 
    delay(500); 
    myservoA.write(90);
    delay(2000);
  } 
  else
  {
    myservoA.write(0);
  }
}

Hopefully this will help you. If not, please stick to using the forum, as it can help others too.

Hi,

Thank you very much it was very helpful. I am using my Arduino on a MAC. I'm new to MAC so I'm not sure how to add libraries. I can download the files however I don't see where I copy them. I don't have a Libraries folder. I created a "new ping" folder on my desktop and I tried to drag it into the Arduino main folder without any luck. Hopefully I will be back on a PC soon. Thanks for the help now I'm going to see if I can finally get somewhere.

Ok, I tried it. I get an error stating: 'pingRange' was not declared in this scope I know there is a learning process but this is kind of getting to me. On the plus side I don't quit and I'm off this weekend. So back to the drawing board. Thank you.

Ok, I am trying to combine all the information I have been given. I tried to make things clearer. I am getting this error now:

‘pingRange’ was not declared in this scope

Here is a current copy of the program. Thank you all for the help. I have a few h bridges I could use but I don’t feel I would learn as much that way. Besides I saving them for a build up of this project once I grasp a better understanding of what I’m doing.

#include <Servo.h> 
Servo myservoA;  
const int pingPin = 7; 


void robotRight()
{ 
   if (microsecondsToInches(pingRange(pingPin)) < 10) 
  
    myservoA.write(90);
    delay(1000);
  } 
  else
  {
    myservoA.write(0);
  }
}




void setup() 
{ 
  myservoA.attach(4);   
  delay(5000);
  myservoA.write(0);
  Serial.begin(9600);
  
} 


void loop() 
{ 

  
  unsigned long pingRange (byte pingPin) 

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}


long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}







[code/]

cheekid:
I don’t have a Libraries folder.

Yes, you do–several of them, most likely, but the one that’s relevant is in /Users//Documents/Arduino. The contents of any add-on libraries you want to use should go there, each in its own folder.

Hi,

Thank you. Can someone take a look at my code on my prior post ? I can't seem to get this. Thanks.

  unsigned long pingRange (byte pingPin) 

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}

You can’t declare functions inside other functions. You also need curly braces surrounding what code is to be part of the function.