Arduino Forum

Topics => Robotics => Topic started by: MillerJLee98579 on Aug 22, 2013, 11:33 pm

Title: need some help with code
Post by: MillerJLee98579 on Aug 22, 2013, 11:33 pm
so ive been working on this robot im building ive double checked my wiring and i think the problem im having is in the code because my ping sensor is not working
Code: [Select]
/*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
Edited by: Jason Miller for Project KITT
*/

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

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

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 = 4;
const int dangerThresh = 5; //threshold for obstacles (in inches)
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);
  lcd.clear();
  lcd.print("KITT Online");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //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
    lcd.print("Forward");
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral);
    panMotor.write(0);
    delay(500);
    rightDistance = ping(); //scan to the right
    lcd.print("Scan Right");
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    lcd.print("Scan 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
    lcd.print("Turn Left");
    delay(500);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    lcd.print("Turn Right");
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    lcd.print("Turn 180");
    delay(500);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  long duration, inches;
  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);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear(); 
  delay(100);
}

  long microsecondsToInches(long microseconds)
  {
  return duration / 29 / 2;
}
 
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 23, 2013, 12:35 am
also all 3 servos im using are on a seperate power supply and not running off of the arduino
Title: Re: need some help with code
Post by: robtillaart on Aug 23, 2013, 10:15 am
Quote
and i think the problem im having is in the code because my ping sensor is not working

You could strip your sketch to the ping sensor only and try to check its working?

A quick look at the code did show one thing:
pulseIn() returns unsigned long IIRC so you have to adjust types at some points. but that should not got your sensor failing immediately.
Title: Re: need some help with code
Post by: aarondc on Aug 23, 2013, 03:57 pm
I didn't read anywhere what the problem actually was?
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 23, 2013, 04:25 pm
ok i tested the ping sensor by itself and it works the problem i m having is when everything is running my danger thresh hold does not appear to be doing what its supposed to (I.E. the robot is constantly moving forward and not stopping)
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 23, 2013, 06:15 pm
also it is only displaying 0inches to obstacle on my lcd screen when the full code is running when just the ping code with print is running everything works fine please can someone help me figure out where my mistake is so i can get this running right
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 08:00 pm
ok tested ping sensor by itself and it works tested ping sensor and servo code without the lcd screen and they work i add in the code for the lcd screen and nothing works properly can someone please help me figure out this problem

Code: [Select]
/*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
Edited by Jason Miller for Project KITT
*/

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

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
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 = 4;
const int irPin = 0;  //Sharp infrared sensor pin
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);
  lcd.clear();
  lcd.print("KITT Online");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //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
  long duration, inches;
  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);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();
  delay(100);
}

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


ive been racking my brain on this for the last couple of days and cannot figure out why it isnt working
Title: Re: need some help with code
Post by: wildbill on Aug 24, 2013, 08:04 pm
You might want to post the working code too. However, one issue I see is that there should be a return inches; at the end of the ping function.
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 08:33 pm
Code: [Select]
[code]ok the first code listed is the working code with everything but the lcd screen the second code is the working code for just the ping sensor with lcd screen
I basically merged the two different codes into one and thats when all the problems happened

/*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 = 4;
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(5);
  leftMotor.attach(6);
  panMotor.attach(3); //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;
}
  [/code]

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const int pingPin = 4;


void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Hello Jason");
  delay(1000);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(1000);
}


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);
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Title: Re: need some help with code
Post by: wildbill on Aug 24, 2013, 08:39 pm
Yup, the difference is the missing return statement.
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 08:47 pm
ok so how would i work that in?
Title: Re: need some help with code
Post by: wildbill on Aug 24, 2013, 08:58 pm
Put this at the end of your ping function:
Code: [Select]
return inches;
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 09:26 pm
ok i added the line you suggested and now it still doesnt display the distance on the screen and just spins in circles

Code: [Select]
/*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
Edited by Jason Miller for Project KITT
*/

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

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
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 = 4;
const int irPin = 0;  //Sharp infrared sensor pin
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);
  lcd.clear();
  lcd.print("KITT Online");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //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
  long duration, inches;
  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);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();
  delay(100);
  return inches;
}

  long microsecondsToInches(long microseconds)
  {
  return duration / 74 / 2;
}
 
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 09:28 pm
also after adding that line the screen refresh rate slowed down drastically
Title: Re: need some help with code
Post by: wildbill on Aug 24, 2013, 10:13 pm
I assume that the number of inches returned is always less than your danger threshold, so your comparedistance function with its delays are being called, slowing the refresh down. I'd clear the LCD before you print to it, not afterwards. Right now you have a 10th of a second to see what the ping measurement is. Try making the delay there longer and verify what you're getting from the ping, which I suspect will be less than 5.
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 10:18 pm
i tried that now the screen doesnt print anything and the robot still goes in circles even with a 6 foot clearance between any obstacle
Title: Re: need some help with code
Post by: wildbill on Aug 24, 2013, 11:14 pm
In that case I suggest putting the robot up on blocks, attach it to your PC and put in some debugging serial.prints to show you what's going on. Finding out what distance the ping is giving would be a good start. Is there anything on the robot that's close enough to the ultrasonic sensor to cause a reflection?
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 24, 2013, 11:32 pm
no there is nothing within 6 feet of the robot
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 25, 2013, 02:20 am
added a serial print commands to see what the ping sensor is seeing and still getting nowhere (nothing is showing up in the serial monitor) seriously what is going on with this code?

Code: [Select]
/*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
Edited by Jason Miller for Project KITT
*/

#include <Servo.h> //include Servo library
#include <LiquidCrystal.h> //include lcd library
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
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 = 4;
const int irPin = 0;  //Sharp infrared sensor pin
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);
  lcd.clear();
  lcd.print("KITT Online");
  delay(1000);
  rightMotor.attach(5);
  leftMotor.attach(6);
  panMotor.attach(3); //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
  long duration, inches;
  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);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear();
  delay(100);
  return(inches);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println();
}

  long microsecondsToInches(long microseconds)
  {
  return duration / 74 / 2;
}
 
Title: Re: need some help with code
Post by: wildbill on Aug 25, 2013, 02:17 pm
Move the return after the serial prints. Read up about return, it does two things - specifies the return value, but also exits the function. Your debugging code is never executed.
Title: Re: need some help with code
Post by: PaulS on Aug 25, 2013, 04:01 pm
Code: [Select]
long ping()

Code: [Select]
  int distanceFwd = ping();
Why, when ping() returns a long, is the result stored in an int?
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 26, 2013, 06:47 am
well until i can figure out why this isnt working im either going to give up or buy a premade arduino bot
Title: Re: need some help with code
Post by: TomGeorge on Aug 26, 2013, 03:24 pm
Quote
In that case I suggest putting the robot up on blocks, attach it to your PC and put in some debugging serial.prints to show you what's going on. Finding out what distance the ping is giving would be a good start. Is there anything on the robot that's close enough to the ultrasonic sensor to cause a reflection?

I heartily agree, start with small steps, only program the arduino to get the ultrasonic working, forget any programming for any of the other inputs and outputs.
When you have reliable results from the ultrasonic, then setup the piece of code to react to in or out of distance threshold.
Then once that is done , think about motor control, even if you have to do the motor control separately to the ultrasonic and put them together later.
The intro screen stuff can be done last.
If you do it step by step it will minimise errors and help diagnose any conflicts that may occur as you add code.
This may take a lot of time, but in the long term, less frustration and you learn as you go.

Tom
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 26, 2013, 09:58 pm
ok i still havent figured out why the two codes work seperately but not together no matter how much i try or where i try putting everything in
Title: Re: need some help with code
Post by: aarondc on Aug 26, 2013, 10:15 pm
hmmmmm (http://forum.arduino.cc/index.php?topic=181018.0)
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 26, 2013, 10:27 pm
yeah its driving me nuts been working on this robot since march and getting nowhere with it
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 26, 2013, 10:58 pm
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

Code: [Select]
/*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;
}
 


Code: [Select]
#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
Title: Re: need some help with code
Post by: Jantje on Aug 26, 2013, 11:06 pm

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
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 26, 2013, 11:17 pm
ok fixed the ping function but now my danger thresh hold is not working and neither is my pan servo

Code: [Select]
#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;
}

Title: Re: need some help with code
Post by: Jantje on Aug 27, 2013, 12:50 am

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


go back to http://forum.arduino.cc/index.php?topic=183996.msg1365045#msg1365045 (http://forum.arduino.cc/index.php?topic=183996.msg1365045#msg1365045)
Jantje
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 27, 2013, 01:11 am
sweet it works now thanks
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 27, 2013, 02:55 am
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)
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 27, 2013, 09:36 pm
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

Code: [Select]
#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;
}

Title: Re: need some help with code
Post by: PaulS on Aug 28, 2013, 02:48 pm
Quote
but i am out of pins

Use a serial LCD, instead. Free up a bunch a pins.
Title: Re: need some help with code
Post by: wildbill on Aug 28, 2013, 02:59 pm
Quote
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.
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 28, 2013, 07:39 pm
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
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 29, 2013, 01:59 am
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
Title: Re: need some help with code
Post by: Jantje on Aug 29, 2013, 02:18 pm
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 :-) ).
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 :-D
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 :-D
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 29, 2013, 07:33 pm
well if the yun has better processing than the beaglebone black ill think about it
Title: Re: need some help with code
Post by: Jantje on Aug 30, 2013, 02:43 am

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
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 30, 2013, 02:46 am
from what ive read both beaglebone and raspberry pi will integrate with an arduino
Title: Re: need some help with code
Post by: MillerJLee98579 on Aug 31, 2013, 04:27 am
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

Code: [Select]
#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;
}

Title: Re: need some help with code
Post by: MillerJLee98579 on Sep 02, 2013, 01:12 am
oh my i feel like an idiot went back through my wiring and found a loose ground it works now