Help with robot code


so have made obstacle avoidance robot there seems problem not return value when ping sensor turns left or right

code: [select]
#include<servo.h>
#include <liquidcrystal.h>

liquidcrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingpin = 4; //declares ping pin
const int irpin = 0;  //sharp infrared sensor pin
const int rforward = 180; //full speed forward
const int rbackward = 0; //full speed reverse
const int lforward = rbackward; //this decides motor control
const int lbackward = rforward; //this decides motor control
const int rneutral = 90; //nuetral motor
const int lneutral = 90; //nuetral motor
const int dangerthresh = 2; //threshold obstacles (in)
int leftdistance, rightdistance; //distances on either side
servo panmotor;  //this "neck" of robot
servo leftmotor; //declare left servo motor
servo rightmotor; //declare right servo motor
long duration; //time takes recieve ping))) signal

void setup()
{
  serial.begin(9600); //allows serial monitoring
  lcd.begin(16, 2); //turn on lcd screen
  lcd.print("hello jason"); //threw in fun
  delay(250);
  lcd.clear();  //clears lcd
  lcd.print("activating kitt");  //threw in fun
  delay(250);
  lcd.clear();  //clears lcd
  rightmotor.attach(5); //attach right servo motor proper pin
  leftmotor.attach(6); //attach left servo motor proper pin
  panmotor.attach(3); //attach pan servo motor proper pin
  panmotor.write(90); //center "neck" of robot
  delay(50);
  lcd.print("kitt online");  //threw in fun
  delay(250);
  lcd.clear();  //clears lcd
 
}


void loop()
{
   
  int distancefwd = ping(); //tells robot distance forward
  if (distancefwd>dangerthresh) //if path clear
  {
    leftmotor.write(lforward); //move forward
    rightmotor.write(rforward); //move forward
  }
  else //if path blocked
  {
    leftmotor.write(lneutral);  //stop forward motion
    rightmotor.write(rneutral); //stop forward motion
    panmotor.write(0); // turn ping sesor right
    delay(1000);
    rightdistance = ping(); //scan right
    delay(1000);
    panmotor.write(180);  //turn ping sensor left
    delay(1000);
    leftdistance = ping(); //scan left
    delay(1000);
    panmotor.write(90); //return center
    delay(1000);
    comparedistance();  //determine distance on both sides
  }
}
void comparedistance()
{
  if (leftdistance>rightdistance) //if left less obstructed
  {
    leftmotor.write(lbackward); //turn left
    rightmotor.write(rforward); //turn left
    delay(100);
  }
  else if (rightdistance>leftdistance) //if right less obstructed
  {
    leftmotor.write(lforward);  //turn right
    rightmotor.write(rbackward); //turn right
    delay(1000);
  }
   else //if equally obstructed
  {
    leftmotor.write(lforward); //turn 180 degrees
    rightmotor.write(rbackward); //turn 180 degrees
    delay(1000);
  }
}
long ping()  //ping sensor programming library
{
  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 obstacle");  //displays ditance on lcd
  delay(100);
  lcd.clear();  //clears lcd
  delay(100);
  serial.print(inches);
  serial.print("in, ");
  serial.println(); // serial monitoring of ping signal
  return inches; //return measurement ping sensor
}
long microsecondstoinches(long microseconds)
{
  return microseconds / 74 / 2;
}



i have present bunch of kids tomorrow , realized error please help

do values @ if print left , right distances ?

also, note ping() returns long comparing value returned ints.


Arduino Forum > Using Arduino > Programming Questions > Help with robot code


arduino

Comments

Popular posts from this blog

Authentication error while password reset - Raspberry Pi Forums

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums