Hello all. I have a hopefully easy problem I have not been able to solve. I am using 0023 and have been able to make a few changes to the attached code but am getting:
VarSpeedServoPingneedsWork.cpp: In function 'void loop()':
VarSpeedServoPingneedsWork:41: error: 'ping' was not declared in this scope
I have been searching the internet and this forum for a couple hours and have tried the solutions from other posts but non seem to work here. I left in the code attempts but commented them out. The overall goal is to adapt this code to use a Ping sonar sensor to scan for and detect an object, then move to the object using continuous motion servos and stop a pre-determined distance away. Moving forward, back, right, left, and stop all need to function based on the Ping return distance. The code is hanging up at the very beginning of the loop. I am new to programming so explanations may need to be a little verbose. Thanks in advance!!
#include <VarSpeedServo.h> //include Servo library
void setup();
void loop();
//void ping();
long microsecondsToInches(long microseconds);
long microsecondsToCentimeters(long microseconds);
//#define ping 1
//#include <Servo.h> //include Servo library
const int RForward = 65; //speed value of drive servo, value 0 for full speed rotation
const int RBackward = 130; //speed value of drive servo, value 180 for full speed rotation
const int LForward = RBackward; //the servos must rotate in opposite directions in order to drive in the same direction
const int LBackward = RForward; //the servos must rotate in opposite directions in order to drive in the same direction
const int RNeutral = 95; //value 90 for center, my servo is a little off
const int LNeutral = 95; //value 90 for center, my servo is a little off
const int pingPin = 8; //signal pin on parallax ping sensor connected to this arduino pin "remember the ping sensor is 5V, not source"
const int irPin = 0; //Sharp infrared sensor connected to this arduino pin "not currently used"
const int dangerThresh = 20; //threshold for obstacles (in cm), this was 10 by default
int leftDistance, rightDistance; //distances on either side
//int distanceFwd = 0;
//int Scan;
//int ping;
VarSpeedServo panMotor; //servo that pans the ping sensor
VarSpeedServo leftMotor; //continuous rotation servo that drives the left wheel
VarSpeedServo rightMotor; //continuous rotation servo that drives the right wheel
long duration; //time it takes to recieve PING))) signal
void setup()
{
rightMotor.attach(11); //signal wire on right countinuous rotation "drive" servo connected to this arduino pin
leftMotor.attach(10); //signal wire on left continuous rotation "drive servo connected to this arduino pin
panMotor.attach(6); //signal wire on pan servo for ping sensor connected to this arduino pin
panMotor.write(90); //set PING))) pan to center "90 by default"
}
void loop()
{
// Scan = distanceFwd(ping()); //ping stright ahead
int distanceFwd = ping(); //ping stright ahead
//int distanceFwd = ping(); //ping stright ahead
if (distanceFwd>dangerThresh) //if the path is clear
{
leftMotor.write(LForward); //move forward
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral); //stop left drive motor
rightMotor.write(RNeutral); //stop right drive motor
panMotor.write(0); //turn ping sensor right, 0 for full right
delay(500); //wait this many milliseconds
rightDistance = ping(); //ping
delay(500); //wait this many milliseconds
panMotor.write(170); //turn ping sensor left, 180 for full right "my servo hits stop limit and binds at 180"
delay(500); //wait this many milliseconds
leftDistance = ping(); //ping
delay(500); //wait this many milliseconds
panMotor.write(83); //return to center, "90 by default" my servo is off center
delay(100); //wait this many milliseconds
compareDistance(); //compare the two distances measured
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward); //rotate left drive servo bacward "skid steer"
rightMotor.write(RForward); //rotate right drive servo forward "skid steer"
delay(1000); //run motors this many milliseconds
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward); //rotate left drive servo forward "skid steer"
rightMotor.write(RBackward); //rotate right drive servo backward "skid steer"
delay(1000); //run motors this many milliseconds
}
else //if they are equally obstructed
{
leftMotor.write(LForward); //rotate left drive servo forward
rightMotor.write(RBackward); //rotate right drive servo backward
delay(2000); //run motors this many milliseconds
}
}