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

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
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3412
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

no there is nothing within 6 feet of the robot
Logged

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

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3412
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46062
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long ping()

Code:
  int distanceFwd = ping();
Why, when ping() returns a long, is the result stored in an int?
Logged

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

well until i can figure out why this isnt working im either going to give up or buy a premade arduino bot
Logged

Victoria, Australia
Offline Offline
God Member
*****
Karma: 24
Posts: 987
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

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

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
Logged

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

hmmmmm
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: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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

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

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

Logged

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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