need some help with code

hmmmmm

yeah its driving me nuts been working on this robot since march and getting nowhere with it

RoboProto5.ino: In function 'void loop()':
RoboProto5:57: error: 'ping' was not declared in this scope

this is the error i constantly get in my code yet in the original code this error never shows up

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include <Servo.h> //include Servo library

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  //Convert duration into distance
  return duration / 29 / 2;
}
#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int pingPin = 4;
const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Hello Jason");
  delay(1000);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //attach motors to proper pins
  panMotor.write(90);
}


void loop() 
{
  
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);

  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();  
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println();
  
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

the first code posted is the original and the second code posted is the one ive been working on

MillerJLee98579:
RoboProto5.ino: In function 'void loop()':
RoboProto5:57: error: 'ping' was not declared in this scope

this is the error i constantly get in my code yet in the original code this error never shows up

That is because you removed the function ping from the sketch.
And keep away from delay. With your current code the response time of the robot is way above 1 second. That is really slow.
Jantje

ok fixed the ping function but now my danger thresh hold is not working and neither is my pan servo

#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int pingPin = 4;
const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup() 
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Hello Jason");
  delay(1000);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //attach motors to proper pins
  panMotor.write(90);
}


void loop() 
{
   
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(500);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(500);
  }
}
long ping()
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();  
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println();
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

MillerJLee98579:
ok fixed the ping function but now my danger thresh hold is not working and neither is my pan servo

go back to need some help with code - #10 by wildbill - Robotics - Arduino Forum
Jantje

sweet it works now thanks

now if i add an ir sensor to the robot will it work like the ping sensor or do i need to add additional code to it? (i'll be using the ir sensor below the ping sensor to catch obstacles below the ping sensor)

ok so at some point in the future i want to add more sensors to my robot but i am out of pins how would i break this code down so i could use two arduinos via I2C with one running the sensors and the other running just the servos

#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 0; //full speed forward
const int RBackward = 180; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motors
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup() 
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason");
  delay(250);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(250);
  lcd.clear();
  rightMotor.attach(5); //attach left servo motor to proper pin
  leftMotor.attach(6); //attach right servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);
  lcd.print("KITT Online");
  delay(250);
  lcd.clear();
  
}


void loop() 
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0); 
    delay(100);
    rightDistance = ping(); //scan to the right
    delay(100);
    panMotor.write(180);
    delay(100);
    leftDistance = ping(); //scan to the left
    delay(100);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(100); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(100);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(100);
  }
}
long ping()
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();  
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches;
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

but i am out of pins

Use a serial LCD, instead. Free up a bunch a pins.

ok so at some point in the future i want to add more sensors to my robot but i am out of pins how would i break this code down so i could use two arduinos via I2C with one running the sensors and the other running just the servos

That could very well be a nice way to divide and conquer, but given your current level of experience, I suspect it would be a struggle. Consider upgrading to a Mega instead.

i will think about that but it all depends on whether or not the sensors and stuff i want to add will work with it

so far my robot is working well next step is to build a chassis and then eventually add more sensors possibly even that new pixy cam that i saw on kickstarter and a beaglebone or raspberry pi for wireless transmission of video and data

I would hold back on buying a pi.
I have one lying around doing nothing for over a year now. I wanted to use it to replace a network shield to go to a wireless router. However later I found out that you can connect the arduino directly to a hacked wireless router. I have connected my arduino mega to a openwrt supported wireless router via the serial port. This is basically what the yun is about (but then with a leonardo).

The yun (to be released in september) is already integrated with arduino and aims at the wireless powerful devices.
This is nearly the same as my solution but Arduino has solved (most of?) the communication problems for you. For instance most of those routers have very little memory and even worse eeprom and the yun has had a redesign to add more memory and eeprom.
The advantage of a router is that you can have plenty of other network devices connected (like IP cams) and you can pick the arduino of your choise (like a mega :slight_smile: ).
If the yun USB capabilities are good; one could also add a USB hub and USB cameras. Though a USB camera will put more load on the linux than a pure IP cam. But as the yun has more memory and disk space as normal router that may not be a problem.
By the way I ordered 2 pixy cams :smiley:
Best regards
Jantje

PS Now I think about it. It may be possible to use the yun as a wireless router and add a ethernet hub :smiley:

well if the yun has better processing than the beaglebone black ill think about it

MillerJLee98579:
well if the yun has better processing than the beaglebone black ill think about it

On processing level beaglebone beats yun easily. But do you need it?
And how well does beaglebone integrate with Arduino?
Jantje

from what ive read both beaglebone and raspberry pi will integrate with an arduino

uh oh i think something broke i went to test it out again to show some buddies and for some reason my right servo does not stop when the danger thresh hold is met the rest of the parts do what they are supposed to

#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 0; //full speed forward
const int RBackward = 180; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motors
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup() 
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason");
  delay(250);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(250);
  lcd.clear();
  rightMotor.attach(5); //attach left servo motor to proper pin
  leftMotor.attach(6); //attach right servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);
  lcd.print("KITT Online");
  delay(250);
  lcd.clear();
  
}


void loop() 
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0); 
    delay(100);
    rightDistance = ping(); //scan to the right
    delay(100);
    panMotor.write(180);
    delay(100);
    leftDistance = ping(); //scan to the left
    delay(100);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(100); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(100);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(100);
  }
}
long ping()
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();  
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches;
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

oh my i feel like an idiot went back through my wiring and found a loose ground it works now