Pages: [1] 2 3   Go Down
Author Topic: need some help with code  (Read 4229 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*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;
}
 
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

also all 3 servos im using are on a seperate power supply and not running off of the arduino
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't read anywhere what the problem actually was?
Logged

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*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
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup, the difference is the missing return statement.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so how would i work that in?
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put this at the end of your ping function:
Code:
return inches;
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i added the line you suggested and now it still doesnt display the distance on the screen and just spins in circles

Code:
/*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;
}
 
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

also after adding that line the screen refresh rate slowed down drastically
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3   Go Up
Jump to: